source: trip-planner-front/node_modules/loglevel/test/local-storage-test.js@ 84d0fbb

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

primeNG components

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[6a3a178]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 });
[e29cc2e]99
100 it("log.resetLevel() clears the saved level", function(log) {
101 log.resetLevel();
102
103 expect(undefined).toBeTheStoredLevel();
104 expect("info").not.toBeTheStoredLevel();
105 });
[6a3a178]106 });
107
108 describe("If warn level is saved", function () {
109 beforeEach(function () {
110 testHelpers.setStoredLevel("warn");
111 });
112
113 it("warn is the default log level", function (log) {
114 expect(log).toBeAtLevel("warn");
115 });
116 });
117
118 describe("If error level is saved", function () {
119 beforeEach(function () {
120 testHelpers.setStoredLevel("error");
121 });
122
123 it("error is the default log level", function (log) {
124 expect(log).toBeAtLevel("error");
125 });
126 });
127
128
129 describe("If the level is saved with other data", function() {
130 beforeEach(function() {
131 window.localStorage['qwe'] = "asd";
132 window.localStorage['loglevel'] = "ERROR";
133 window.localStorage['msg'] = "hello world";
134 });
135
136 it("error is the default log level", function(log) {
137 expect(log).toBeAtLevel("error");
138 });
139
140 it("log can be changed to silent level", function(log) {
141 log.setLevel("silent");
142 expect(log).toBeAtLevel("silent");
143 });
144
145 it("log.setLevel() overrides the saved level only", function(log) {
146 log.setLevel("debug");
147
148 expect('debug').toBeTheStoredLevel();
149 expect(window.localStorage['msg']).toBe("hello world");
150 });
151 });
152
153 describe("If the level is stored incorrectly", function() {
154 beforeEach(function() {
155 testHelpers.setLocalStorageStoredLevel('gibberish');
156 });
157
158 it("warn is the default log level", function(log) {
159 expect(log).toBeAtLevel("warn");
160 });
161
162 it("warn is not persisted as the current level", function(log) {
163 expect("warn").not.toBeTheStoredLevel();
164 });
165
166 it("log can be changed to info level", function(log) {
167 log.setLevel("info");
168 expect(log).toBeAtLevel("info");
169 });
170
171 it("log.setLevel() overrides the saved level with the new level", function(log) {
172 expect('debug').not.toBeTheStoredLevel();
173
174 log.setLevel("debug");
175
176 expect('debug').toBeTheStoredLevel();
177 });
178 });
179
180 describeIf(testHelpers.isCookieStorageAvailable() && testHelpers.isLocalStorageAvailable(),
181 "if localStorage and cookies are both available", function () {
182
183 it("the level stored in cookies is ignored if a local storage level is set", function () {
184 testHelpers.setCookieStoredLevel("info");
185 testHelpers.setLocalStorageStoredLevel("debug");
186
187 testHelpers.withFreshLog(function (log) {
188 expect(log).toBeAtLevel("debug");
189 });
190 });
191
192 it("the level stored in cookies is used if no local storage level is set", function () {
193 testHelpers.setCookieStoredLevel("info");
194 window.localStorage.clear();
195
196 testHelpers.withFreshLog(function (log) {
197 expect(log).toBeAtLevel("info");
198 });
199 });
200
201 it("the local storage level is set and the cookie level is not", function (log) {
202 log.setLevel("error");
203 expect("error").toBeTheLevelStoredByLocalStorage();
204 expect("error").not.toBeTheLevelStoredByCookie();
205 });
206 });
207 });
208});
Note: See TracBrowser for help on using the repository browser.