-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.spec.js
256 lines (201 loc) · 6.18 KB
/
handlers.spec.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*eslint-env mocha*/
"use strict";
const handlers = require("./handlers");
const LEVELS = require("./levels");
const sinon = require("sinon");
require("should");
describe("NullHandler", function() {
describe("#setLevel", function() {
it("should have by default level ALL", function() {
const h = new handlers.NullHandler();
h._level.should.be.equal(LEVELS.ALL);
});
it("should accept level priority", function() {
const h = new handlers.NullHandler();
h.setLevel(LEVELS.INFO);
h._level.should.be.equal(LEVELS.INFO);
});
it("should accept string version of level", function() {
const h = new handlers.NullHandler();
h.setLevel("INFO");
h._level.should.be.equal(LEVELS.INFO);
});
it("should throw if it is not number", function() {
const h = new handlers.NullHandler();
(function() {
h.setLevel({});
}.should.throw());
});
it("should throw if such level/priority does not exist", function() {
const h = new handlers.NullHandler();
(function() {
h.setLevel("FATAL");
}.should.throw());
});
});
describe("#handle", function() {
it("should call _handle if record level have the same or more priority", function() {
const h = new handlers.NullHandler();
let called = false;
h._handle = function(r) {
called = true;
r.test.should.be.equal("a");
};
h.handle({ level: LEVELS.INFO, test: "a" });
called.should.be.true();
});
it("should not call _handle if record level have lower priority", function() {
const h = new handlers.NullHandler();
h.setLevel(LEVELS.INFO);
let called = false;
h._handle = function() {
called = true;
};
h.handle({ level: LEVELS.TRACE, test: "a" });
called.should.be.false();
});
});
});
describe("BaseHandler", function() {
it("should have some default format", function() {
const h = new handlers.BaseHandler();
h.should.have.property("formatRecord").which.is.Function();
});
describe("#setFormat", function() {
it("should compile format if passed string", function() {
const h = new handlers.BaseHandler();
const f = h.formatRecord;
h.setFormat("test");
h.should.have.property("formatRecord").which.is.not.equal(f);
});
it("should set format function if passed function", function() {
const h = new handlers.BaseHandler();
const f = h.formatRecord;
const ff = function() {};
h.setFormat(ff);
h.should.have
.property("formatRecord")
.which.is.not.equal(f)
.and.equal(ff);
});
it("should throw if passed not string and not function", function() {
const h = new handlers.BaseHandler();
(function() {
h.setFormat(10);
}.should.throw());
});
});
});
describe("ConsoleHandler", function() {
let origStdout;
let origStderr;
beforeEach(function() {
origStdout = process.stdout.write;
origStderr = process.stderr.write;
});
it("should pass record to stdout if level <= INFO", function() {
let called = false;
process.stdout.write = function() {
called = true;
};
process.stderr.write = function() {
throw new Error("Should not be called");
};
const h = new handlers.ConsoleHandler();
h.setFormat("test");
h.handle({ level: LEVELS.TRACE });
called.should.be.true();
called = false;
h.handle({ level: LEVELS.DEBUG });
called.should.be.true();
called = false;
h.handle({ level: LEVELS.INFO });
called.should.be.true();
called = false;
process.stdout.write = origStdout;
process.stderr.write = origStderr;
});
it("should pass record to stderr if level > INFO", function() {
let called = false;
process.stdout.write = function() {
throw new Error("Should not be called");
};
process.stderr.write = function() {
called = true;
};
const h = new handlers.ConsoleHandler();
h.setFormat("test");
h.handle({ level: LEVELS.WARN });
called.should.be.true();
called = false;
h.handle({ level: LEVELS.ERROR });
called.should.be.true();
called = false;
process.stdout.write = origStdout;
process.stderr.write = origStderr;
});
});
describe("StreamHandler", function() {
describe("#setStream", function() {
it("should set stream", function() {
const h = new handlers.StreamHandler();
const s = {};
h.setStream(s);
h._stream.should.be.equal(s);
});
});
describe("#setShouldFormat", function() {
it("should set if need format before write", function() {
const h = new handlers.StreamHandler();
h.setShouldFormat(false);
h._shouldFormat.should.be.equal(false);
});
it("should have by default to true", function() {
const h = new handlers.StreamHandler();
h._shouldFormat.should.be.equal(true);
});
it("should format record when true", function() {
const h = new handlers.StreamHandler();
h.setFormat("test");
h.setStream({
write: function(r) {
r.should.be.String();
}
});
h.handle({ level: LEVELS.INFO });
});
it("should format record when false", function() {
const h = new handlers.StreamHandler();
h.setShouldFormat(false);
const rr = { level: LEVELS.INFO };
h.setStream({
write: function(r) {
r.should.be.equal(rr);
}
});
h.handle(rr);
});
});
});
describe("MultiHandler", function() {
describe("#addHandler", function() {
it("should add handlers to settings", function() {
const s = new handlers.MultiHandler();
const spy = sinon.spy();
s.addHandler({ handle: spy });
s.handle({ level: 10 });
spy.called.should.be.true();
});
});
describe("#handle", function() {
it("should delegate call to own handlers", function() {
const s = new handlers.MultiHandler();
const spy1 = sinon.spy();
const spy2 = sinon.spy();
s.addHandler({ handle: spy1 }).addHandler({ handle: spy2 });
s.handle({ level: 10 });
spy1.called.should.be.true();
spy2.called.should.be.true();
});
});
});