source: trip-planner-front/node_modules/loglevel/test/cookie-test.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.1 KB
Line 
1"use strict";
2
3define(['test/test-helpers'], function(testHelpers) {
4 var describeIf = testHelpers.describeIf;
5 var it = testHelpers.itWithFreshLog;
6
7 var originalConsole = window.console;
8 var originalDocument = window.document;
9
10 describeIf(testHelpers.isCookieStorageAvailable() && !testHelpers.isLocalStorageAvailable(),
11 "Cookie-only persistence tests:", function() {
12
13 beforeEach(function() {
14 window.console = {"log" : jasmine.createSpy("console.log")};
15 this.addMatchers({
16 "toBeAtLevel" : testHelpers.toBeAtLevel,
17 "toBeTheStoredLevel" : testHelpers.toBeTheLevelStoredByCookie
18 });
19 });
20
21 afterEach(function() {
22 window.console = originalConsole;
23 });
24
25 describe("If no level is saved", function() {
26 beforeEach(function() {
27 testHelpers.clearStoredLevels();
28 });
29
30 it("log level is set to warn by default", function(log) {
31 expect(log).toBeAtLevel("warn");
32 });
33
34 it("warn is persisted as the current level", function(log) {
35 expect("warn").toBeTheStoredLevel();
36 });
37
38 it("log can be set to info level", function(log) {
39 log.setLevel("info");
40 expect(log).toBeAtLevel("info");
41 });
42
43 it("log.setLevel() sets a cookie with the given level", function(log) {
44 log.setLevel("debug");
45 expect("debug").toBeTheStoredLevel();
46 });
47 });
48
49 describe("If info level is saved", function() {
50 beforeEach(function() {
51 testHelpers.setStoredLevel("info");
52 });
53
54 it("info is the default log level", function(log) {
55 expect(log).toBeAtLevel("info");
56 });
57
58 it("log can be changed to warn level", function(log) {
59 log.setLevel("warn");
60 expect(log).toBeAtLevel("warn");
61 });
62
63 it("log.setLevel() overwrites the saved level", function(log) {
64 log.setLevel("error");
65
66 expect("error").toBeTheStoredLevel();
67 expect("info").not.toBeTheStoredLevel();
68 });
69 });
70
71 describe("If the level is saved with other data", function() {
72 beforeEach(function() {
73 window.document.cookie = "qwe=asd";
74 window.document.cookie = "loglevel=ERROR";
75 window.document.cookie = "msg=hello world";
76 });
77
78 it("error is the default log level", function(log) {
79 expect(log).toBeAtLevel("error");
80 });
81
82 it("log can be changed to silent level", function(log) {
83 log.setLevel("silent");
84 expect(log).toBeAtLevel("silent");
85 });
86
87 it("log.setLevel() overrides the saved level only", function(log) {
88 log.setLevel("debug");
89
90 expect('debug').toBeTheStoredLevel();
91 expect(window.document.cookie).toContain("msg=hello world");
92 });
93 });
94
95 describe("If the level cookie is set incorrectly", function() {
96 beforeEach(function() {
97 testHelpers.setCookieStoredLevel('gibberish');
98 });
99
100 it("warn is the default log level", function(log) {
101 expect(log).toBeAtLevel("warn");
102 });
103
104 it("warn is persisted as the current level, overriding the invalid cookie", function(log) {
105 expect("warn").toBeTheStoredLevel();
106 });
107
108 it("log can be changed to info level", function(log) {
109 log.setLevel("info");
110 expect(log).toBeAtLevel("info");
111 });
112
113 it("log.setLevel() overrides the saved level with the new level", function(log) {
114 expect('debug').not.toBeTheStoredLevel();
115
116 log.setLevel("debug");
117
118 expect('debug').toBeTheStoredLevel();
119 });
120 });
121 });
122});
Note: See TracBrowser for help on using the repository browser.