source: trip-planner-front/node_modules/loglevel/test/level-setting-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: 9.1 KB
Line 
1"use strict";
2
3var logMethods = [
4 "trace",
5 "debug",
6 "info",
7 "warn",
8 "error"
9];
10
11function getConsoleMethod(logMethodName) {
12 if (logMethodName === 'debug') {
13 return console.log;
14 } else {
15 return console[logMethodName];
16 }
17}
18
19define(['../lib/loglevel'], function(log) {
20 var originalConsole = window.console;
21
22 describe("Basic log levels changing tests:", function() {
23 beforeEach(function() {
24 window.console = {};
25
26 for (var ii = 0; ii < logMethods.length; ii++) {
27 window.console[logMethods[ii]] = jasmine.createSpy(logMethods[ii]);
28 }
29
30 window.console.log = jasmine.createSpy('log');
31 });
32
33 afterEach(function() {
34 window.console = originalConsole;
35 });
36
37 describe("log.enableAll()", function() {
38 it("enables all log methods", function() {
39 log.enableAll(false);
40
41 for (var ii = 0; ii < logMethods.length; ii++) {
42 var method = logMethods[ii];
43 log[method]("a log message");
44
45 expect(getConsoleMethod(method)).toHaveBeenCalled();
46 }
47 });
48 });
49
50 describe("log.disableAll()", function() {
51 it("disables all log methods", function() {
52 log.disableAll(false);
53
54 for (var ii = 0; ii < logMethods.length; ii++) {
55 var method = logMethods[ii];
56 log[method]("a log message");
57
58 expect(getConsoleMethod(method)).not.toHaveBeenCalled();
59 }
60 });
61 });
62
63 describe("log.setLevel() throws errors if given", function() {
64 it("no level argument", function() {
65 expect(function() {
66 log.setLevel();
67 }).toThrow("log.setLevel() called with invalid level: undefined");
68 });
69
70 it("a null level argument", function() {
71 expect(function() {
72 log.setLevel(null);
73 }).toThrow("log.setLevel() called with invalid level: null");
74 });
75
76 it("an undefined level argument", function() {
77 expect(function() {
78 log.setLevel(undefined);
79 }).toThrow("log.setLevel() called with invalid level: undefined");
80 });
81
82 it("an invalid log level index", function() {
83 expect(function() {
84 log.setLevel(-1);
85 }).toThrow("log.setLevel() called with invalid level: -1");
86 });
87
88 it("an invalid log level name", function() {
89 expect(function() {
90 log.setLevel("InvalidLevelName");
91 }).toThrow("log.setLevel() called with invalid level: InvalidLevelName");
92 });
93 });
94
95 describe("setting log level by name", function() {
96 function itCanSetLogLevelTo(level) {
97 it("can set log level to " + level, function() {
98 log.setLevel(level, false);
99
100 log[level]("log message");
101 expect(getConsoleMethod(level)).toHaveBeenCalled();
102 });
103 }
104
105 itCanSetLogLevelTo("trace");
106 itCanSetLogLevelTo("debug");
107 itCanSetLogLevelTo("info");
108 itCanSetLogLevelTo("warn");
109 itCanSetLogLevelTo("error");
110 });
111
112 describe("log level settings", function() {
113 describe("log.trace", function() {
114 it("is enabled at trace level", function() {
115 log.setLevel(log.levels.TRACE);
116
117 log.trace("a log message");
118 expect(console.trace).toHaveBeenCalled();
119 });
120
121 it("is disabled at debug level", function() {
122 log.setLevel(log.levels.DEBUG);
123
124 log.trace("a log message");
125 expect(console.trace).not.toHaveBeenCalled();
126 });
127
128 it("is disabled at silent level", function() {
129 log.setLevel(log.levels.SILENT);
130
131 log.trace("a log message");
132 expect(console.trace).not.toHaveBeenCalled();
133 });
134 });
135
136 describe("log.debug", function() {
137 it("is enabled at trace level", function() {
138 log.setLevel(log.levels.TRACE);
139
140 log.debug("a log message");
141 expect(console.log).toHaveBeenCalled();
142 });
143
144 it("is enabled at debug level", function() {
145 log.setLevel(log.levels.DEBUG);
146
147 log.debug("a log message");
148 expect(console.log).toHaveBeenCalled();
149 });
150
151 it("is disabled at info level", function() {
152 log.setLevel(log.levels.INFO);
153
154 log.debug("a log message");
155 expect(console.log).not.toHaveBeenCalled();
156 });
157
158 it("is disabled at silent level", function() {
159 log.setLevel(log.levels.SILENT);
160
161 log.debug("a log message");
162 expect(console.log).not.toHaveBeenCalled();
163 });
164 });
165
166 describe("log.log", function() {
167 it("is enabled at trace level", function() {
168 log.setLevel(log.levels.TRACE);
169
170 log.log("a log message");
171 expect(console.log).toHaveBeenCalled();
172 });
173
174 it("is enabled at debug level", function() {
175 log.setLevel(log.levels.DEBUG);
176
177 log.log("a log message");
178 expect(console.log).toHaveBeenCalled();
179 });
180
181 it("is disabled at info level", function() {
182 log.setLevel(log.levels.INFO);
183
184 log.log("a log message");
185 expect(console.log).not.toHaveBeenCalled();
186 });
187
188 it("is disabled at silent level", function() {
189 log.setLevel(log.levels.SILENT);
190
191 log.log("a log message");
192 expect(console.log).not.toHaveBeenCalled();
193 });
194 });
195
196 describe("log.info", function() {
197 it("is enabled at debug level", function() {
198 log.setLevel(log.levels.DEBUG);
199
200 log.info("a log message");
201 expect(console.info).toHaveBeenCalled();
202 });
203
204 it("is enabled at info level", function() {
205 log.setLevel(log.levels.INFO);
206
207 log.info("a log message");
208 expect(console.info).toHaveBeenCalled();
209 });
210
211 it("is disabled at warn level", function() {
212 log.setLevel(log.levels.WARN);
213
214 log.info("a log message");
215 expect(console.info).not.toHaveBeenCalled();
216 });
217
218 it("is disabled at silent level", function() {
219 log.setLevel(log.levels.SILENT);
220
221 log.info("a log message");
222 expect(console.info).not.toHaveBeenCalled();
223 });
224 });
225
226 describe("log.warn", function() {
227 it("is enabled at info level", function() {
228 log.setLevel(log.levels.INFO);
229
230 log.warn("a log message");
231 expect(console.warn).toHaveBeenCalled();
232 });
233
234 it("is enabled at warn level", function() {
235 log.setLevel(log.levels.WARN);
236
237 log.warn("a log message");
238 expect(console.warn).toHaveBeenCalled();
239 });
240
241 it("is disabled at error level", function() {
242 log.setLevel(log.levels.ERROR);
243
244 log.warn("a log message");
245 expect(console.warn).not.toHaveBeenCalled();
246 });
247
248 it("is disabled at silent level", function() {
249 log.setLevel(log.levels.SILENT);
250
251 log.warn("a log message");
252 expect(console.warn).not.toHaveBeenCalled();
253 });
254 });
255
256 describe("log.error", function() {
257 it("is enabled at warn level", function() {
258 log.setLevel(log.levels.WARN);
259
260 log.error("a log message");
261 expect(console.error).toHaveBeenCalled();
262 });
263
264 it("is enabled at error level", function() {
265 log.setLevel(log.levels.ERROR);
266
267 log.error("a log message");
268 expect(console.error).toHaveBeenCalled();
269 });
270
271 it("is disabled at silent level", function() {
272 log.setLevel(log.levels.SILENT);
273
274 log.error("a log message");
275 expect(console.error).not.toHaveBeenCalled();
276 });
277 });
278 });
279 });
280});
281
Note: See TracBrowser for help on using the repository browser.