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