forked from gfk-ba/meteor-notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications_tests.js
executable file
·349 lines (278 loc) · 12.4 KB
/
notifications_tests.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*jshint -W098*/
/*global describe, it, before, beforeEach, after, afterEach, sinon, beforeAll, chai */
var instance, sandbox, notificationsCollection;
var setupFn = function () {
if (sandbox) {
//Munit doesn't call the afterEach when a test fails so we do cleanup here to prevent cascading fails
//See for more info: https://github.com/spacejamio/meteor-munit/issues/24
tearDownFn();
}
instance = Notifications;
sandbox = sinon.sandbox.create();
notificationsCollection = instance._getNotificationsCollection();
};
var tearDownFn = function () {
sandbox.restore();
instance._notificationTimeout = undefined;
notificationsCollection.remove({});
sandbox = undefined;
};
var testNotification, timedNotification, clock, testId;
var expect = chai.expect;
var assert = chai.assert;
describe('#addNotification', function () {
beforeEach(function () {
setupFn();
});
it('Should call _add', function () {
var _add = sandbox.stub(instance, '_add');
instance.addNotification('Title', 'test123');
expect(_add.callCount).to.equal(1);
});
it('Should return the _id of the notification', function () {
var testID = 123;
var _add = sandbox.stub(instance, '_add').returns(testID);
expect(instance.addNotification('Title', 'test123')).to.equal(testID);
});
it('Should use the defaultOptions to construct the object to pass to _add', function () {
var _add = sandbox.stub(instance, '_add');
var expected = _.clone(instance.defaultOptions);
var testMessage = 'test123';
var testTitle = 'Title';
expected.title = testTitle;
expected.message = testMessage;
expected.type = instance.defaultOptions.type;
expected.userCloseable = instance.defaultOptions.userCloseable;
expected.clickBodyToClose = instance.defaultOptions.clickBodyToClose;
expected.closed = undefined;
expected.onExpires = undefined;
delete expected.timeout;
instance.addNotification(testTitle, testMessage);
expect(_add).to.have.been.calledWith(expected);
});
it('Should use defaultOptionsByType if exists for type', function () {
var _add = sandbox.stub(instance, '_add');
var expected = _.clone(instance.defaultOptions);
var testMessage = 'test123';
var testTitle = 'Title';
Notifications.defaultOptionsByType[Notifications.TYPES.WARNING] = {};
Notifications.defaultOptionsByType[Notifications.TYPES.WARNING].userCloseable = false;
Notifications.defaultOptionsByType[Notifications.TYPES.WARNING].clickBodyToClose = false;
Notifications.defaultOptionsByType[Notifications.TYPES.WARNING].closed = 'blabla';
Notifications.defaultOptionsByType[Notifications.TYPES.WARNING].onExpires = undefined;
delete expected.timeout;
expected.title = testTitle;
expected.message = testMessage;
expected.type = Notifications.TYPES.WARNING;
expected.userCloseable = false;
expected.clickBodyToClose = false;
expected.closed = 'blabla';
expected.onExpires = undefined;
instance.warn(testTitle, testMessage);
expect(_add).to.have.been.calledWith(expected);
});
it('Called with options - it should call _add', function () {
var testOptions = {
type: instance.TYPES.ERROR,
userCloseable: false
};
var _add = sandbox.stub(instance, '_add');
instance.addNotification('Title', 'test123', testOptions);
expect(_add.callCount).to.equal(1);
});
it('Called with options - Should use the options to construct the object to pass to _add', function () {
var testOptions = {
type: instance.TYPES.ERROR,
userCloseable: false,
clickBodyToClose: true,
closed: 'test123',
onExpires: 'test456'
};
var _add = sandbox.stub(instance, '_add');
var expected = _.clone(instance.defaultOptions);
var testMessage = 'test123';
var testTitle = 'Title';
expected.title = testTitle;
expected.message = testMessage;
expected.type = testOptions.type;
expected.userCloseable = testOptions.userCloseable;
expected.clickBodyToClose = testOptions.clickBodyToClose;
expected.closed = 'test123';
expected.onExpires = 'test456';
delete expected.timeout;
instance.addNotification(testTitle, testMessage, _.clone(testOptions));
expect(_add).to.have.been.calledWith(expected);
});
it('Called with options - Options has a timeout - Should add an expires timestamp to the notification given to _add', function () {
sandbox.useFakeTimers();
var timedOptions = {
type: instance.TYPES.ERROR,
userCloseable: false,
timeout: 2000
};
var _add = sandbox.stub(instance, '_add');
instance.addNotification('Title', 'test123', _.clone(timedOptions));
var expires = _add.args[0][0].expires;
expect(expires).to.equal(new Date().getTime() + timedOptions.timeout);
});
});
describe('#getNotificationClass', function () {
beforeEach(function () {
setupFn();
});
it('Should return the className for the given type', function () {
expect(instance.getNotificationClass(instance.TYPES.ERROR)).to.equal('error');
expect(instance.getNotificationClass(instance.TYPES.WARNING)).to.equal('warning');
expect(instance.getNotificationClass(instance.TYPES.INFO)).to.equal('info');
expect(instance.getNotificationClass(instance.TYPES.SUCCESS)).to.equal('success');
});
});
describe('#remove', function () {
var testNotification;
beforeEach(function () {
setupFn();
testNotification = {};
testNotification.message = 'test100';
testNotification.type = instance.defaultOptions.type;
testNotification.userCloseable = instance.defaultOptions.userCloseable;
instance._add(testNotification);
instance._add(testNotification);
instance._add(testNotification);
testNotification._id = testId = 'unique';
instance._add(testNotification);
});
it('Should remove the notification with the given _id', function () {
var size = notificationsCollection.find().count();
instance.remove({_id: testId});
expect(notificationsCollection.find().count()).to.equal(size - 1);
expect(notificationsCollection.findOne({_id: testId})).to.equal(undefined);
});
});
describe('#_add', function () {
var testNotification, timedNotification;
beforeEach(function () {
setupFn();
testNotification = {
message: 'test100',
type: instance.defaultOptions.type,
userCloseable: instance.defaultOptions.userCloseable
};
timedNotification = _.clone(testNotification);
timedNotification.expires = new Date().getTime() + 2000;
});
it('Should call _add', function () {
var collectionSize = notificationsCollection.find().count();
instance._add(testNotification);
expect(notificationsCollection.find().count()).to.equal(collectionSize + 1);
});
it('Should call _createTimeout', function () {
var _createTimeout = sandbox.stub(instance, '_createTimeout');
instance._add(timedNotification);
expect(_createTimeout.callCount).to.equal(1);
});
it('When given notification has a expires timestamp - When _notifactionTimeout is truthy - Given expires is higher than any existing timestamp - Should NOT remove the existing timeout', function () {
instance._notificationTimeout = 'test';
notificationsCollection.insert(timedNotification);
var slowNotification = _.clone(timedNotification);
slowNotification.expires += 250;
instance._add(_.clone(slowNotification));
expect(instance._notificationTimeout).to.equal('test');
});
it('When given notification has a expires timestamp - When _notifactionTimeout is truthy - Given expires timestamp is lower than any existing timestamp - Should call _createTimeout', function () {
var fastNotification = _.clone(timedNotification);
fastNotification.expires -= 250;
instance._notificationTimeout = 'test';
notificationsCollection.insert(timedNotification);
var _createTimeout = sandbox.stub(instance, '_createTimeout');
instance._add(fastNotification);
expect(_createTimeout.callCount).to.equal(1);
});
});
describe('#_createTimeout', function () {
var timedNotification, clock;
beforeEach(function () {
setupFn();
clock = sandbox.useFakeTimers();
timedNotification = {};
timedNotification.message = 'test100';
timedNotification.type = instance.defaultOptions.type;
timedNotification.userCloseable = instance.defaultOptions.userCloseable;
});
it('Should set _notificationTimeout', function () {
timedNotification.expires = (new Date().getTime()) + 2000;
notificationsCollection.insert(timedNotification);
instance._createTimeout();
expect(instance._notificationTimeout).to.not.equal(undefined);
});
it('Should remove the notification when the timeout expires', function () {
timedNotification.expires = (new Date().getTime()) + 2000;
notificationsCollection.insert(timedNotification);
var collectionSize = notificationsCollection.find().count();
instance._createTimeout();
clock.tick(1999);
expect(notificationsCollection.find().count()).to.equal(collectionSize);
clock.tick(2000);
expect(notificationsCollection.find().count()).to.equal(collectionSize - 1);
});
it('Called with very short timeout - Should remove the notification when the timeout expires', function () {
timedNotification.expires = (new Date().getTime()) + 10;
notificationsCollection.insert(timedNotification);
var collectionSize = notificationsCollection.find().count();
instance._createTimeout();
clock.tick(9);
expect(notificationsCollection.find().count()).to.equal(collectionSize);
clock.tick(10);
expect(notificationsCollection.find().count()).to.equal(collectionSize - 1);
});
it('Should callback onExpires when the timeout expires', function () {
timedNotification.expires = (new Date().getTime()) + 2000;
var flag = null;
timedNotification.onExpires = function(notification) {
flag = notification._id;
}
var notificationId = notificationsCollection.insert(timedNotification);
var collectionSize = notificationsCollection.find().count();
instance._createTimeout();
clock.tick(1999);
expect(notificationsCollection.find().count()).to.equal(collectionSize);
expect(flag).to.equal(null);
clock.tick(2000);
expect(notificationsCollection.find().count()).to.equal(collectionSize - 1);
expect(flag).to.equal(notificationId);
});
});
describe('#getDefaultOptions', function () {
beforeEach(function () {
setupFn();
});
it('Should return defaultOptions when type is not given', function () {
expect(instance.getDefaultOptions()).to.eql(Notifications.defaultOptions);
});
it('Should return defaultOptions of the type when type is given', function () {
Notifications.defaultOptionsByType[Notifications.TYPES.WARNING] = {
foo: 'bar'
};
expect(instance.getDefaultOptions(Notifications.TYPES.WARNING)).to.eql(Notifications.defaultOptionsByType[Notifications.TYPES.WARNING]);
});
});
describe('#onExpires', function () {
beforeEach(function () {
setupFn();
clock = sandbox.useFakeTimers();
});
it('Should call onExpires callback on timeout expiry', function () {
var flag = null;
var notificationId = instance.info('Title', 'test123', {
timeout: 2000,
onExpires: function(notification) {
flag = notification._id;
}
});
clock.tick(1999);
expect(notificationsCollection.find().count()).to.equal(1);
assert(flag === null, "onExpires fired prematurely");
clock.tick(2000);
assert(flag === notificationId, "onExpires should have fired");
expect(notificationsCollection.find().count()).to.equal(0);
});
});