forked from felipekian/push.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.tests.js
More file actions
599 lines (495 loc) · 17.7 KB
/
push.tests.js
File metadata and controls
599 lines (495 loc) · 17.7 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
var BROWSER_CHROME = 'Chrome',
BROWSER_FIREFOX = 'Firefox',
BROWSER_EDGE = 'Edge',
BROWSER_OPERA = 'Opera',
TEST_TITLE = 'title',
TEST_BODY = 'body',
TEST_TIMEOUT = 1000,
TEST_TAG = 'foo',
TEST_TAG_2 = 'bar',
TEST_ICON = 'icon',
TEST_SW_DEFAULT = '/serviceWorker.min.js',
NOOP = function() {
return null;
};
describe('proper support detection', function() {
function isBrowser(browser) {
return platform.name.toLowerCase() === browser.toLowerCase();
}
function getVersion() {
return parseFloat(platform.version);
}
function isSupported() {
return Push.supported();
}
it('should detect Firefox support correctly', function() {
if (isBrowser(BROWSER_FIREFOX)) {
if (getVersion() > 21) expect(isSupported()).toBeTruthy();
else expect(isSupported()).toBeFalsy();
} else {
pending();
}
});
it('should detect Chrome support correctly', function() {
if (isBrowser(BROWSER_CHROME)) {
if (getVersion() > 4) expect(isSupported()).toBeTruthy();
else expect(isSupported()).toBeFalsy();
} else {
pending();
}
});
it('should detect Opera support correctly', function() {
if (isBrowser(BROWSER_OPERA)) {
if (getVersion() > 23) expect(isSupported()).toBeTruthy();
else expect(isSupported()).toBeFalsy();
} else {
pending();
}
});
it('should detect Edge support correctly', function() {
if (isBrowser(BROWSER_EDGE)) {
if (getVersion() > 14) expect(isSupported()).toBeTruthy();
else expect(isSupported()).toBeFalsy();
} else {
pending();
}
});
});
describe('adding plugins', function() {
it('reject invalid plugin manifests', function() {
var testPlugin = function() {
this.testFunc = function() {};
};
expect(Push.extend.bind(Push, testPlugin)).toThrow();
});
it('accept valid plugin manifests', function() {
var testPlugin = {
plugin: function() {
this.testFunc = function() {};
}
};
Push.extend(testPlugin);
expect(Push.testFunc).toBeDefined();
});
it('only allow object-based configs', function() {
spyOn(window.Push, 'config');
var testPlugin = {
config: null,
plugin: function() {
this.testFunc = function() {};
}
};
Push.extend(testPlugin);
expect(Push.config.calls.count()).toEqual(1); // config() is called one by default in extend()
var testPlugin2 = {
config: {},
plugin: function() {
this.testFunc = function() {};
}
};
Push.extend(testPlugin2);
expect(Push.config.calls.count()).toBeGreaterThan(1);
});
});
describe('changing configuration', function() {
it('returns the current configuration if no parameters passed', function() {
var output = {
serviceWorker: TEST_SW_DEFAULT,
fallback: function(payload) {}
};
var configKeys = Object.keys(Push.config());
Object.keys(output).forEach(function(k) {
expect(configKeys.includes(k)).toBeTruthy();
});
});
it('adds a configuration if one is specified', function() {
Push.config({
a: 1
});
expect(Push.config().a).toBeDefined();
expect(Push.config().a).toBe(1);
});
it('should be capable of performing a deep merge', function() {
var input1 = {
b: {
c: 1,
d: {
e: 2,
f: 3
}
}
};
var input2 = {
b: {
d: {
e: 2,
f: 4
},
g: 5
}
};
var output = {
c: 1,
d: {
e: 2,
f: 4
},
g: 5
};
Push.config(input1);
Push.config(input2);
expect(Push.config().b).toBeDefined();
expect(JSON.stringify(Push.config().b)).toBe(JSON.stringify(output));
});
});
if (Push.supported()) {
Push.config({
serviceWorker: TEST_SW_DEFAULT
});
function initRequestSpy(granted) {
var param_str, param_int;
param_str = granted ? Push.Permission.GRANTED : Push.Permission.DEFAULT;
param_int = granted ? 0 : 1;
/* Safari 6+, Legacy webkit browsers */
if (
window.webkitNotifications &&
window.webkitNotifications.checkPermission
) {
spyOn(window.webkitNotifications, 'requestPermission').and.callFake(
function(cb) {
cb(param_int);
}
);
} else if (
/* Chrome 23+ */
window.Notification &&
window.Notification.requestPermission
) {
spyOn(window.Notification, 'requestPermission').and.callFake(
function() {
return new Promise(function(resolve) {
resolve(param_str);
});
}
);
}
}
function getRequestObject() {
var obj = {};
/* Safari 6+, Legacy webkit browsers */
if (
window.webkitNotifications &&
window.webkitNotifications.checkPermission
)
return window.webkitNotifications.requestPermission;
/* Chrome 23+ */ else if (
window.Notification &&
window.Notification.requestPermission
)
return window.Notification.requestPermission;
return null;
}
describe('initialization', function() {
it('should create a new instance', function() {
expect(window.Push !== undefined).toBeTruthy();
});
it('isSupported should return a boolean', function() {
expect(typeof Push.supported()).toBe('boolean');
});
});
describe('permission', function() {
var callback; // Callback spy
beforeAll(function() {
jasmine.clock().uninstall();
jasmine.clock().install();
});
beforeEach(function() {
callback = jasmine.createSpy('callback');
});
it('should have permission stored as a string varant', function() {
expect(typeof Push.Permission.get()).toBe('string');
});
it('should update permission value if permission is denied and execute callback (deprecated)', function(done) {
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.DEFAULT
);
initRequestSpy(false);
Push.Permission.request(NOOP, function() {
callback();
expect(Push.Permission.has()).toBe(false);
expect(callback).toHaveBeenCalled();
done();
});
});
it('should update permission value if permission is denied and execute callback (with promise)', function(done) {
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.DEFAULT
);
initRequestSpy(false);
Push.Permission.request()
.then(NOOP)
.catch(function() {
callback();
expect(callback).toHaveBeenCalled();
expect(Push.Permission.has()).toBe(false);
done();
});
});
it('should request permission if permission is not granted', function() {
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.DEFAULT
);
initRequestSpy(false);
Push.create(TEST_TITLE).then(function() {
expect(getRequestObject()).toHaveBeenCalled();
});
});
it('should update permission value if permission is granted and execute callback (deprecated)', function() {
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.GRANTED
);
initRequestSpy(true);
Push.Permission.request(callback, NOOP);
jasmine.clock().tick(TEST_TIMEOUT);
expect(Push.Permission.has()).toBe(true);
expect(callback).toHaveBeenCalled();
});
it('should update permission value if permission is granted and execute callback (with promise)', function() {
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.GRANTED
);
initRequestSpy(true);
Push.Permission.request();
jasmine.clock().tick(TEST_TIMEOUT);
expect(Push.Permission.has()).toBe(true);
});
it('should not request permission if permission is already granted', function() {
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.GRANTED
);
initRequestSpy(true);
Push.Permission.request();
Push.create(TEST_TITLE)
.then(function() {
expect(getRequestObject()).not.toHaveBeenCalled();
})
.catch(function() {});
});
});
describe('creating notifications', function() {
beforeAll(function() {
jasmine.clock().uninstall();
jasmine.clock().install();
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.DEFAULT
);
initRequestSpy(true);
});
beforeEach(function() {
Push.clear();
});
it('should throw exception if no title is provided', function() {
expect(function() {
Push.create();
}).toThrow();
});
it('should return a valid notification wrapper', function(done) {
Push.create(TEST_TITLE).then(function(wrapper) {
expect(wrapper).not.toBe(undefined);
expect(wrapper.get).not.toBe(undefined);
expect(wrapper.close).not.toBe(undefined);
done();
});
});
it('should return promise successfully', function() {
var promise = Push.create(TEST_TITLE)
.then(function() {
expect(promise.then).not.toBe(undefined);
})
.catch(function() {});
});
it('should pass in all API options correctly', function(done) {
// Vibrate omitted because Firefox will default to using the Notification API, not service workers
// Timeout, requestPermission, and event listeners also omitted from this src :(
Push.create(TEST_TITLE, {
body: TEST_BODY,
icon: TEST_ICON,
tag: TEST_TAG,
silent: true
}).then(function(wrapper) {
var notification = wrapper.get();
// Some browsers, like Safari, choose to omit this info
if (notification.title)
expect(notification.title).toBe(TEST_TITLE);
if (notification.body)
expect(notification.body).toBe(TEST_BODY);
if (notification.icon)
expect(notification.icon).toContain(TEST_ICON); // Some browsers append the document location, so we gotta use toContain()
expect(notification.tag).toBe(TEST_TAG);
if (notification.hasOwnProperty('silent')) {
expect(notification.silent).toBe(true);
}
done();
});
});
it('should return the increase the notification count', function(done) {
expect(Push.count()).toBe(0);
Push.create(TEST_TITLE).then(function() {
expect(Push.count()).toBe(1);
done();
});
});
});
describe('event listeners', function() {
var callback, // callback spy
testListener = function(name, cb) {
var event = new Event(name),
options = {},
key,
promise;
key =
'on' +
name[0].toUpperCase() +
name.substr(1, name.length - 1);
options[key] = callback;
Push.create(TEST_TITLE, options)
.then(function(wrapper) {
var notification = wrapper.get();
notification.dispatchEvent(event);
expect(callback).toHaveBeenCalled();
cb();
})
.catch(function() {});
};
beforeAll(function() {
initRequestSpy(true);
});
beforeEach(function() {
callback = jasmine.createSpy('callback');
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.DEFAULT
);
});
it('should execute onClick listener correctly', function(done) {
testListener('click', done);
});
it('should execute onShow listener correctly', function(done) {
testListener('show', done);
});
it('should execute onError listener correctly', function(done) {
testListener('error', done);
});
it('should execute onClose listener correctly', function(done) {
testListener('close', done);
});
});
describe('closing notifications', function() {
var callback; // Callback spy
var closeSpy;
beforeAll(function() {
initRequestSpy(true);
jasmine.clock().uninstall();
jasmine.clock().install();
});
beforeEach(function() {
closeSpy = spyOn(window.Notification.prototype, 'close');
spyOn(Push.Permission, 'get').and.returnValue(
Push.Permission.DEFAULT
);
closeSpy.calls.reset();
Push.clear();
callback = jasmine.createSpy('callback');
});
it('should close notifications on close callback', function(done) {
Push.create(TEST_TITLE, {
onClose: callback
}).then(function(wrapper) {
var notification = wrapper.get();
expect(Push.count()).toBe(1);
notification.dispatchEvent(new Event('close'));
expect(Push.count()).toBe(0);
done();
});
});
it('should close notifications using wrapper', function(done) {
Push.create(TEST_TITLE, {
onClose: callback
}).then(function(wrapper) {
expect(Push.count()).toBe(1);
wrapper.close();
expect(closeSpy).toHaveBeenCalled();
expect(Push.count()).toBe(0);
done();
});
});
it('should close notifications using given timeout', function(done) {
Push.create(TEST_TITLE, {
timeout: TEST_TIMEOUT
}).then(function() {
jasmine.clock().tick(500);
expect(Push.count()).toBe(1);
expect(closeSpy).not.toHaveBeenCalled();
jasmine.clock().tick(TEST_TIMEOUT);
expect(closeSpy).toHaveBeenCalled();
expect(Push.count()).toBe(0);
done();
});
});
it('should close a notification given a tag', function(done) {
Push.create(TEST_TITLE, {
tag: TEST_TAG
}).then(function() {
expect(Push.count()).toBe(1);
expect(Push.close(TEST_TAG)).toBeTruthy();
expect(closeSpy).toHaveBeenCalled();
expect(Push.count()).toBe(0);
done();
});
});
it('should close all notifications when cleared', function(done) {
Promise.all([
Push.create(TEST_TITLE, {
tag: TEST_TAG
}),
Push.create('hello world!', {
tag: TEST_TAG_2
})
]).then(function() {
expect(Push.count()).toBeGreaterThan(0);
expect(Push.clear()).toBeTruthy();
expect(closeSpy).toHaveBeenCalled();
expect(Push.count()).toBe(0);
done();
});
});
});
} else {
describe('fallback functionality', function() {
it('should ensure fallback method fires correctly', function(done) {
var fallback = jasmine.createSpy('fallback');
Push.config({
fallback: fallback
});
Push.create(TEST_TITLE).then(function() {
expect(fallback).toHaveBeenCalled();
done();
});
});
it('should ensure all notification options are passed to the fallback', function(done) {
Push.config({
fallback: function(payload) {
expect(payload.title).toBe(TEST_TITLE);
expect(payload.body).toBe(TEST_BODY);
expect(payload.icon).toBe(TEST_ICON);
expect(payload.tag).toBe(TEST_TAG);
done();
}
});
Push.create(TEST_TITLE, {
body: TEST_BODY,
icon: TEST_ICON,
tag: TEST_TAG
}).catch(function() {});
});
});
}