forked from Nickersoft/push.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_tests.js
276 lines (239 loc) · 8.81 KB
/
push_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
var
TEST_TITLE = 'title',
TEST_BODY = 'body',
TEST_TIMEOUT = 1000,
TEST_TIMEOUT_LONG = 50000,
TEST_TAG = 'foo',
TEST_TAG_2 = 'bar',
TEST_ICON = 'icon',
TEST_ICON_ARRAY = { x16: TEST_ICON, x32: TEST_ICON },
TEST_SW = 'customServiceWorker.js',
TEST_SW_DEFAULT = "serviceWorker.js"
NOOP = function () {}; // NO OPerator (empty function)
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.isSupported).toBe('boolean');
});
});
describe('permission', function () {
var callback; // Callback spy
beforeEach(function () {
callback = jasmine.createSpy('callback');
});
it('should have permission stored as a string constant', function () {
expect(typeof Push.Permission.get()).toBe('string');
});
it('should update permission value if permission is denied and execute callback', function (done) {
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.DENIED);
});
Push.Permission.request(NOOP, callback);
setTimeout(function () {
expect(Push.Permission.has()).toBe(false);
expect(callback).toHaveBeenCalled();
done();
}, 500);
});
it('should request permission if permission is not granted', function () {
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.DENIED);
});
Push.Permission.request();
Push.create(TEST_TITLE);
expect(window.Notification.requestPermission).toHaveBeenCalled();
});
it('should update permission value if permission is granted and execute callback', function (done) {
spyOn(Push.Permission, 'get').and.returnValue(Push.Permission.GRANTED);
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.GRANTED);
});
Push.Permission.request(callback, NOOP);
setTimeout(function () {
expect(Push.Permission.has()).toBe(true);
expect(callback).toHaveBeenCalled();
done();
}, 500);
});
it('should not request permission if permission is already granted', function () {
spyOn(Push.Permission, 'get').and.returnValue(Push.Permission.GRANTED);
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.GRANTED);
});
Push.Permission.request();
Push.create(TEST_TITLE);
expect(window.Notification.requestPermission).not.toHaveBeenCalled();
});
});
describe('creating notifications', function () {
beforeAll(function () {
jasmine.clock().install();
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.GRANTED);
});
});
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) {
var promise = Push.create(TEST_TITLE);
promise.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);
expect(promise.then).not.toBe(undefined);
});
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 test :(
var promise = Push.create(TEST_TITLE, {
body: TEST_BODY,
icon: TEST_ICON,
tag: TEST_TAG,
serviceWorker: TEST_SW,
silent: true
});
promise.then(function(wrapper) {
var notification = wrapper.get();
expect(notification.title).toBe(TEST_TITLE);
expect(notification.body).toBe(TEST_BODY);
expect(notification.icon).toBe(TEST_ICON);
expect(notification.tag).toBe(TEST_TAG);
expect(notification.silent).toBe(undefined);
expect(Push.__lastWorkerPath()).toBe(TEST_SW);
done();
});
});
it('should use "serviceWorker.js" as a service worker path if one is not specified', function(done) {
var promise = Push.create(TEST_TITLE);
promise.then(function(wrapper) {
expect(Push.__lastWorkerPath()).toBe(TEST_SW_DEFAULT);
done();
});
});
it('should return the increase the notification count', function () {
var count;
expect(Push.count()).toBe(0);
Push.create(TEST_TITLE);
expect(Push.count()).toBe(1);
});
});
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;
promise = Push.create(TEST_TITLE, options);
promise.then(function(wrapper) {
var notification = wrapper.get();
notification.dispatchEvent(event);
expect(callback).toHaveBeenCalled();
cb();
});
};
beforeAll(function () {
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.GRANTED);
});
});
beforeEach(function () {
callback = jasmine.createSpy('callback');
});
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 () {
beforeAll(function () {
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.GRANTED);
});
});
beforeEach(function () {
spyOn(window.Notification.prototype, 'close');
Push.clear();
});
it('should close notifications on close callback', function (done) {
var promise;
promise = Push.create(TEST_TITLE, {
onClose: callback
});
expect(Push.count()).toBe(1);
promise.then(function(wrapper) {
var notification = wrapper.get();
notification.dispatchEvent(new Event('close'));
expect(Push.count()).toBe(0);
done();
});
});
it('should close notifications using wrapper', function (done) {
var promise;
promise = Push.create(TEST_TITLE, {
onClose: callback
});
expect(Push.count()).toBe(1);
promise.then(function(wrapper) {
wrapper.close();
expect(window.Notification.prototype.close).toHaveBeenCalled();
expect(Push.count()).toBe(0);
done();
});
});
it('should close notifications using given timeout', function () {
Push.create(TEST_TITLE, {
timeout: TEST_TIMEOUT
});
expect(Push.count()).toBe(1);
expect(window.Notification.prototype.close).not.toHaveBeenCalled();
jasmine.clock().tick(TEST_TIMEOUT);
expect(window.Notification.prototype.close).toHaveBeenCalled();
expect(Push.count()).toBe(0);
});
it('should close a notification given a tag', function () {
var count, promise;
Push.create(TEST_TITLE, {
tag: TEST_TAG
});
expect(Push.count()).toBe(1);
expect(Push.close(TEST_TAG)).toBeTruthy();
expect(window.Notification.prototype.close).toHaveBeenCalled();
expect(Push.count()).toBe(0);
});
it('should close all notifications when cleared', function () {
Push.create(TEST_TITLE, {
tag: TEST_TAG
});
Push.create('hello world!', {
tag: TEST_TAG_2
});
expect(Push.count()).toBe(2);
expect(Push.clear()).toBeTruthy();
expect(window.Notification.prototype.close).toHaveBeenCalled();
expect(Push.count()).toBe(0);
});
});