-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
executable file
·234 lines (189 loc) · 6.54 KB
/
test.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
var rewire = require('rewire');
var assert = require('assert');
var sinon = require('sinon');
var activity = rewire('./index.js');
describe('activity', function() {
var getActivity = activity.__get__('getActivity');
var writeActivity = activity.__get__('writeActivity');
before(function() {
activity.setOutputHandlers(sinon.spy());
});
describe('create', function() {
it('throws if given wrong input', function() {
assert.throws(function() {
activity.create();
});
assert.throws(function() {
activity.create(2828);
});
assert.throws(function() {
activity.create({});
});
});
it('returns a uuid', function() {
var ovenActivity = activity.create('Warming up the oven.');
assert(typeof ovenActivity === 'number');
});
it('returns an unique uuid', function() {
var ovenActivity = activity.create('Warming up the oven.');
var waterActivity = activity.create('Boiling the water.');
assert(ovenActivity !== waterActivity);
});
it('creates activity without any timestamps', function() {
var timelessId = activity.create('Timeless.');
var timelessActivity = getActivity(timelessId);
assert(timelessActivity.timestamps.length === 0);
});
});
describe('start', function() {
it('creates activity with one timestmap', function() {
var testId = activity.start('Marathon, go!');
var testActivity = getActivity(testId);
assert(testActivity.timestamps.length === 1);
});
});
describe('mark', function() {
it('adds timestamp to activity', function() {
var testId = activity.create('Marco, polo.');
var testActivity = getActivity(testId);
assert(testActivity.timestamps.length === 0);
var timestamp = activity.mark(testId);
assert(testActivity.timestamps.length === 1);
assert(testActivity.timestamps[0] === timestamp);
assert(typeof timestamp === 'number');
});
});
describe('destroy', function() {
it('deletes activity and returns it', function() {
var testId = activity.create('Zuhl.');
var testActivity = getActivity(testId);
assert(testActivity.timestamps.length === 0);
var destroyedActivity = activity.destroy(testId);
assert(testActivity === destroyedActivity);
assert(testActivity.timestamps.length === 0);
});
});
describe('end', function() {
it('throws if id doesn\'t exist', function() {
assert.throws(function() {
activity.end(-1);
});
});
it('deletes activity after writing it', function() {
var id = activity.start('Message');
activity.end(id);
assert.throws(function() {
activity.end(id);
});
});
it('returns Activity object', function() {
var id = activity.start('Message');
var existingActivity = getActivity(id);
var endedActivity = activity.end(id);
assert(endedActivity === existingActivity);
});
});
describe('write', function() {
it('throws if no output handlers defined', function() {
activity.setOutputHandlers();
assert.throws(function() {
activity.start('foo');
});
});
it('calls template and handlers with right args', function() {
var handler = sinon.spy();
activity.setOutputHandlers(handler, handler);
var writeData = {foo: 'bar'};
var templateVal = 'Whee';
var template = sinon.spy(function() {
return templateVal;
});
writeActivity(template, writeData);
assert.equal(template.callCount, 2);
assert.equal(handler.callCount, 2);
assert(template.alwaysCalledWithExactly(writeData));
assert(handler.alwaysCalledWithExactly(templateVal));
});
});
describe('disable', function() {
it('prevents writing to output', function() {
var handler = sinon.spy();
activity.setOutputHandlers(handler);
activity.disable();
activity.start('foo');
assert.equal(handler.callCount, 0);
});
});
describe('enable', function() {
it('allows writing to output', function() {
var handler = sinon.spy();
activity.setOutputHandlers(handler);
activity.disable();
activity.start('foo');
assert.equal(handler.callCount, 0);
activity.enable();
activity.start('foo');
assert.equal(handler.callCount, 1);
});
});
describe('setOutputHandlers', function() {
it('accepts an array', function() {
var handlers = [1, 2, 3];
var newHandlers = activity.setOutputHandlers(handlers);
assert.equal(handlers.length, newHandlers.length);
assert.deepEqual(handlers, newHandlers);
});
it('accepts multiple arguments', function() {
var handlers = [1, 2, 3];
var newHandlers = activity.setOutputHandlers(1, 2, 3);
assert.equal(handlers.length, newHandlers.length);
assert.deepEqual(handlers, newHandlers);
});
});
describe('addOutputHandler', function() {
it('adds a new handler to the end of the handlers array', function() {
var handlers = activity.setOutputHandlers([sinon.spy()]);
assert.equal(handlers.length, 1);
var newHandler = function() {};
activity.addOutputHandler(newHandler);
assert.equal(handlers.length, 2);
assert.equal(handlers[1], newHandler);
});
});
describe('setStartFormatter', function() {
it('overwrites previous start formatter', function() {
activity.setOutputHandlers(sinon.spy());
var formatter = sinon.spy();
activity.setStartFormatter(formatter);
var message = 'Happy Birthday Dearie';
activity.start(message);
var firstCall = formatter.firstCall;
assert.equal(firstCall.args.length, 1);
assert.deepEqual(Object.keys(firstCall.args[0]), [
'id',
'message',
'timestamps'
]);
assert.equal(firstCall.args[0].timestamps.length, 1);
});
});
describe('setEndFormatter', function() {
it('overwrites previous end formatter', function() {
activity.setOutputHandlers(sinon.spy());
var formatter = sinon.spy();
activity.setEndFormatter(formatter);
var message = 'Happy Birthday Dearie';
activity.end(activity.start(message));
var firstCall = formatter.firstCall;
assert.equal(firstCall.args.length, 1);
assert.deepEqual(Object.keys(firstCall.args[0]), [
'id',
'message',
'timestamps'
]);
assert.equal(firstCall.args[0].timestamps.length, 2);
var timestamps = firstCall.args[0].timestamps;
assert(timestamps[1] >= timestamps[1]);
});
});
});