UNPKG

5.04 kBJavaScriptView Raw
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("new loggers correctly inherit a logging level of `0`", function(log) {
110 log.setLevel(0);
111 var newLogger = log.getLogger("newLogger");
112 expect(newLogger).toBeAtLevel("trace");
113 });
114 });
115 });
116});