source: trip-planner-front/node_modules/loglevel/test/multiple-logger-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: 5.7 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 describe("Multiple logger instances tests:", function() {
10
11 describe("log.getLogger()", function() {
12 it("returns a new logger that is not the default one", function(log) {
13 var newLogger = log.getLogger("newLogger");
14 expect(newLogger).not.toEqual(log);
15 expect(newLogger.trace).toBeDefined();
16 expect(newLogger.debug).toBeDefined();
17 expect(newLogger.info).toBeDefined();
18 expect(newLogger.warn).toBeDefined();
19 expect(newLogger.error).toBeDefined();
20 expect(newLogger.setLevel).toBeDefined();
21 expect(newLogger.setDefaultLevel).toBeDefined();
22 expect(newLogger.enableAll).toBeDefined();
23 expect(newLogger.disableAll).toBeDefined();
24 expect(newLogger.methodFactory).toBeDefined();
25 });
26
27 it("returns loggers without `getLogger()` and `noConflict()`", function(log) {
28 var newLogger = log.getLogger("newLogger");
29 expect(newLogger.getLogger).toBeUndefined();
30 expect(newLogger.noConflict).toBeUndefined();
31 });
32
33 it("returns the same instance when called repeatedly with the same name", function(log) {
34 var logger1 = log.getLogger("newLogger");
35 var logger2 = log.getLogger("newLogger");
36
37 expect(logger1).toEqual(logger2);
38 });
39
40 it("should throw if called with no name", function(log) {
41 expect(function() {
42 log.getLogger();
43 }).toThrow();
44 });
45
46 it("should throw if called with empty string for name", function(log) {
47 expect(function() {
48 log.getLogger("");
49 }).toThrow();
50 });
51
52 it("should throw if called with a non-string name", function(log) {
53 expect(function() { log.getLogger(true); }).toThrow();
54 expect(function() { log.getLogger({}); }).toThrow();
55 expect(function() { log.getLogger([]); }).toThrow();
56 expect(function() { log.getLogger(10); }).toThrow();
57 expect(function() { log.getLogger(function(){}); }).toThrow();
58 expect(function() { log.getLogger(null); }).toThrow();
59 expect(function() { log.getLogger(undefined); }).toThrow();
60 if (window.Symbol) {
61 expect(function() { log.getLogger(Symbol()); }).toThrow();
62 }
63 });
64 });
65
66 describe("inheritance", function() {
67 beforeEach(function() {
68 window.console = {"log" : jasmine.createSpy("console.log")};
69 this.addMatchers({
70 "toBeAtLevel" : testHelpers.toBeAtLevel
71 });
72 testHelpers.clearStoredLevels();
73 });
74
75 afterEach(function() {
76 window.console = originalConsole;
77 });
78
79 it("loggers are created with the same level as the default logger", function(log) {
80 log.setLevel("ERROR");
81 var newLogger = log.getLogger("newLogger");
82 expect(newLogger).toBeAtLevel("error");
83 });
84
85 it("if a logger's level is persisted, it uses that level rather than the default logger's level", function(log) {
86 testHelpers.setStoredLevel("error", "newLogger");
87 log.setLevel("TRACE");
88 var newLogger = log.getLogger("newLogger");
89 expect(newLogger).toBeAtLevel("error");
90 });
91
92 it("other loggers do not change when the default logger's level is changed", function(log) {
93 log.setLevel("TRACE");
94 var newLogger = log.getLogger("newLogger");
95 log.setLevel("ERROR");
96 expect(newLogger).toBeAtLevel("TRACE");
97 expect(log.getLogger("newLogger")).toBeAtLevel("TRACE");
98 });
99
100 it("loggers are created with the same methodFactory as the default logger", function(log) {
101 log.methodFactory = function(methodName, level) {
102 return function() {};
103 };
104
105 var newLogger = log.getLogger("newLogger");
106 expect(newLogger.methodFactory).toEqual(log.methodFactory);
107 });
108
109 it("loggers have independent method factories", function(log) {
110 var log1 = log.getLogger('logger1');
111 var log2 = log.getLogger('logger2');
112
113 var log1Spy = jasmine.createSpy('log1spy');
114 log1.methodFactory = function(methodName, level) {
115 return log1Spy;
116 };
117 log1.setLevel(log1.getLevel());
118
119 var log2Spy = jasmine.createSpy('log2spy');
120 log2.methodFactory = function(methodName, level) {
121 return log2Spy;
122 };
123 log2.setLevel(log2.getLevel());
124
125 log1.error('test1');
126 log2.error('test2');
127
128 expect(log1Spy).toHaveBeenCalledWith("test1");
129 expect(log2Spy).toHaveBeenCalledWith("test2");
130 });
131
132 it("new loggers correctly inherit a logging level of `0`", function(log) {
133 log.setLevel(0);
134 var newLogger = log.getLogger("newLogger");
135 expect(newLogger).toBeAtLevel("trace");
136 });
137 });
138 });
139});
Note: See TracBrowser for help on using the repository browser.