source: trip-planner-front/node_modules/loglevel/test/local-storage-test.js@ 6c1585f

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

initial commit

  • Property mode set to 100644
File size: 7.0 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
9 describeIf(testHelpers.isLocalStorageAvailable(), "Local storage persistence tests:", function() {
10
11 beforeEach(function() {
12 window.console = {"log" : jasmine.createSpy("console.log")};
13 this.addMatchers({
14 "toBeAtLevel" : testHelpers.toBeAtLevel,
15 "toBeTheStoredLevel" : testHelpers.toBeTheLevelStoredByLocalStorage,
16 "toBeTheLevelStoredByLocalStorage": testHelpers.toBeTheLevelStoredByLocalStorage,
17 "toBeTheLevelStoredByCookie": testHelpers.toBeTheLevelStoredByCookie
18 });
19
20 testHelpers.clearStoredLevels();
21 });
22
23 afterEach(function() {
24 window.console = originalConsole;
25 });
26
27 describe("If no level is saved", function() {
28 it("log level is set to warn by default", function(log) {
29 expect(log).toBeAtLevel("warn");
30 });
31
32 it("warn is not persisted as the current level", function(log) {
33 expect("warn").not.toBeTheStoredLevel();
34 });
35
36 it("log can be set to info level", function(log) {
37 log.setLevel("info");
38 expect(log).toBeAtLevel("info");
39 });
40
41 it("log.setLevel() sets a cookie with the given level", function(log) {
42 log.setLevel("debug");
43 expect("debug").toBeTheStoredLevel();
44 });
45
46 it("log.setLevel() does not set a cookie if `persist` argument is false", function(log) {
47 log.setLevel("debug", false);
48 expect("debug").not.toBeTheStoredLevel();
49 });
50 });
51
52 describe("If trace level is saved", function () {
53 beforeEach(function () {
54 testHelpers.setStoredLevel("trace");
55 });
56
57 it("trace is the default log level", function (log) {
58 expect(log).toBeAtLevel("trace");
59 });
60 });
61
62 describe("If debug level is saved", function () {
63 beforeEach(function () {
64 testHelpers.setStoredLevel("debug");
65 });
66
67 it("debug is the default log level", function (log) {
68 expect(log).toBeAtLevel("debug");
69 });
70 });
71
72 describe("If info level is saved", function() {
73 beforeEach(function() {
74 testHelpers.setStoredLevel("info");
75 });
76
77 it("info is the default log level", function(log) {
78 expect(log).toBeAtLevel("info");
79 });
80
81 it("log can be changed to warn level", function(log) {
82 log.setLevel("warn");
83 expect(log).toBeAtLevel("warn");
84 });
85
86 it("log.setLevel() overwrites the saved level", function(log) {
87 log.setLevel("error");
88
89 expect("error").toBeTheStoredLevel();
90 expect("info").not.toBeTheStoredLevel();
91 });
92
93 it("log.setLevel() does not overwrite the saved level if `persist` argument is false", function(log) {
94 log.setLevel("error", false);
95
96 expect("info").toBeTheStoredLevel();
97 expect("error").not.toBeTheStoredLevel();
98 });
99 });
100
101 describe("If warn level is saved", function () {
102 beforeEach(function () {
103 testHelpers.setStoredLevel("warn");
104 });
105
106 it("warn is the default log level", function (log) {
107 expect(log).toBeAtLevel("warn");
108 });
109 });
110
111 describe("If error level is saved", function () {
112 beforeEach(function () {
113 testHelpers.setStoredLevel("error");
114 });
115
116 it("error is the default log level", function (log) {
117 expect(log).toBeAtLevel("error");
118 });
119 });
120
121
122 describe("If the level is saved with other data", function() {
123 beforeEach(function() {
124 window.localStorage['qwe'] = "asd";
125 window.localStorage['loglevel'] = "ERROR";
126 window.localStorage['msg'] = "hello world";
127 });
128
129 it("error is the default log level", function(log) {
130 expect(log).toBeAtLevel("error");
131 });
132
133 it("log can be changed to silent level", function(log) {
134 log.setLevel("silent");
135 expect(log).toBeAtLevel("silent");
136 });
137
138 it("log.setLevel() overrides the saved level only", function(log) {
139 log.setLevel("debug");
140
141 expect('debug').toBeTheStoredLevel();
142 expect(window.localStorage['msg']).toBe("hello world");
143 });
144 });
145
146 describe("If the level is stored incorrectly", function() {
147 beforeEach(function() {
148 testHelpers.setLocalStorageStoredLevel('gibberish');
149 });
150
151 it("warn is the default log level", function(log) {
152 expect(log).toBeAtLevel("warn");
153 });
154
155 it("warn is not persisted as the current level", function(log) {
156 expect("warn").not.toBeTheStoredLevel();
157 });
158
159 it("log can be changed to info level", function(log) {
160 log.setLevel("info");
161 expect(log).toBeAtLevel("info");
162 });
163
164 it("log.setLevel() overrides the saved level with the new level", function(log) {
165 expect('debug').not.toBeTheStoredLevel();
166
167 log.setLevel("debug");
168
169 expect('debug').toBeTheStoredLevel();
170 });
171 });
172
173 describeIf(testHelpers.isCookieStorageAvailable() && testHelpers.isLocalStorageAvailable(),
174 "if localStorage and cookies are both available", function () {
175
176 it("the level stored in cookies is ignored if a local storage level is set", function () {
177 testHelpers.setCookieStoredLevel("info");
178 testHelpers.setLocalStorageStoredLevel("debug");
179
180 testHelpers.withFreshLog(function (log) {
181 expect(log).toBeAtLevel("debug");
182 });
183 });
184
185 it("the level stored in cookies is used if no local storage level is set", function () {
186 testHelpers.setCookieStoredLevel("info");
187 window.localStorage.clear();
188
189 testHelpers.withFreshLog(function (log) {
190 expect(log).toBeAtLevel("info");
191 });
192 });
193
194 it("the local storage level is set and the cookie level is not", function (log) {
195 log.setLevel("error");
196 expect("error").toBeTheLevelStoredByLocalStorage();
197 expect("error").not.toBeTheLevelStoredByCookie();
198 });
199 });
200 });
201});
Note: See TracBrowser for help on using the repository browser.