1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var cordova = require('./helper/cordova'),
|
8 | PushNotification = require('../www/push'),
|
9 | execSpy,
|
10 | execWin,
|
11 | options;
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | describe('phonegap-plugin-push', function () {
|
18 | beforeEach(function () {
|
19 | options = { android: {}, ios: {}, windows: {} };
|
20 | execWin = jasmine.createSpy();
|
21 | execSpy = spyOn(cordova.required, 'cordova/exec').andCallFake(execWin);
|
22 | });
|
23 |
|
24 | describe('PushNotification', function () {
|
25 | it('should exist', function () {
|
26 | expect(PushNotification).toBeDefined();
|
27 | expect(typeof PushNotification === 'object').toBe(true);
|
28 | });
|
29 |
|
30 | it('should contain a init function', function () {
|
31 | expect(PushNotification.init).toBeDefined();
|
32 | expect(typeof PushNotification.init === 'function').toBe(true);
|
33 | });
|
34 |
|
35 | it('should contain a hasPermission function', function () {
|
36 | expect(PushNotification.hasPermission).toBeDefined();
|
37 | expect(typeof PushNotification.hasPermission === 'function').toBe(true);
|
38 | });
|
39 |
|
40 | it('should contain a createChannel function', function () {
|
41 | expect(PushNotification.createChannel).toBeDefined();
|
42 | expect(typeof PushNotification.createChannel === 'function').toBe(true);
|
43 | });
|
44 |
|
45 | it('should contain a deleteChannel function', function () {
|
46 | expect(PushNotification.deleteChannel).toBeDefined();
|
47 | expect(typeof PushNotification.deleteChannel === 'function').toBe(true);
|
48 | });
|
49 |
|
50 | it('should contain a listChannels function', function () {
|
51 | expect(PushNotification.listChannels).toBeDefined();
|
52 | expect(typeof PushNotification.listChannels === 'function').toBe(true);
|
53 | });
|
54 |
|
55 | it('should contain a unregister function', function () {
|
56 | var push = PushNotification.init({});
|
57 | expect(push.unregister).toBeDefined();
|
58 | expect(typeof push.unregister === 'function').toBe(true);
|
59 | });
|
60 |
|
61 | it('should contain a getApplicationIconBadgeNumber function', function () {
|
62 | var push = PushNotification.init({});
|
63 | expect(push.getApplicationIconBadgeNumber).toBeDefined();
|
64 | expect(typeof push.getApplicationIconBadgeNumber === 'function').toBe(true);
|
65 | });
|
66 |
|
67 | it('should contain a setApplicationIconBadgeNumber function', function () {
|
68 | var push = PushNotification.init({});
|
69 | expect(push.setApplicationIconBadgeNumber).toBeDefined();
|
70 | expect(typeof push.setApplicationIconBadgeNumber === 'function').toBe(true);
|
71 | });
|
72 |
|
73 | it('should contain a clearAllNotifications function', function () {
|
74 | var push = PushNotification.init({});
|
75 | expect(push.clearAllNotifications).toBeDefined();
|
76 | expect(typeof push.clearAllNotifications === 'function').toBe(true);
|
77 | });
|
78 |
|
79 | it('should contain a subscribe function', function () {
|
80 | var push = PushNotification.init({});
|
81 | expect(push.subscribe).toBeDefined();
|
82 | expect(typeof push.subscribe === 'function').toBe(true);
|
83 | });
|
84 |
|
85 | it('should contain a unsubscribe function', function () {
|
86 | var push = PushNotification.init({});
|
87 | expect(push.unsubscribe).toBeDefined();
|
88 | expect(typeof push.unsubscribe === 'function').toBe(true);
|
89 | });
|
90 | });
|
91 |
|
92 | describe('PushNotification instance', function () {
|
93 | describe('cordova.exec', function () {
|
94 | it('should call cordova.exec on next process tick', function (done) {
|
95 | PushNotification.init(options);
|
96 | setTimeout(function () {
|
97 | expect(execSpy).toHaveBeenCalledWith(
|
98 | jasmine.any(Function),
|
99 | jasmine.any(Function),
|
100 | 'PushNotification',
|
101 | 'init',
|
102 | jasmine.any(Object)
|
103 | );
|
104 | done();
|
105 | }, 100);
|
106 | });
|
107 | });
|
108 |
|
109 | describe('on "registration" event', function () {
|
110 | it('should be emitted with an argument', function (done) {
|
111 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
112 | win({ registrationId: 1 });
|
113 | });
|
114 | var push = PushNotification.init(options);
|
115 | push.on('registration', function (data) {
|
116 | expect(data.registrationId).toEqual(1);
|
117 | done();
|
118 | });
|
119 | });
|
120 | });
|
121 |
|
122 | describe('on "notification" event', function () {
|
123 | beforeEach(function () {
|
124 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
125 | win({
|
126 | message: 'Message',
|
127 | title: 'Title',
|
128 | count: 1,
|
129 | sound: 'beep',
|
130 | image: 'Image',
|
131 | additionalData: {},
|
132 | });
|
133 | });
|
134 | });
|
135 |
|
136 | it('should be emitted on success', function (done) {
|
137 | var push = PushNotification.init(options);
|
138 | push.on('notification', function (data) {
|
139 | done();
|
140 | });
|
141 | });
|
142 |
|
143 | it('should provide the data.message argument', function (done) {
|
144 | var push = PushNotification.init(options);
|
145 | push.on('notification', function (data) {
|
146 | expect(data.message).toEqual('Message');
|
147 | done();
|
148 | });
|
149 | });
|
150 |
|
151 | it('should provide the data.title argument', function (done) {
|
152 | var push = PushNotification.init(options);
|
153 | push.on('notification', function (data) {
|
154 | expect(data.title).toEqual('Title');
|
155 | done();
|
156 | });
|
157 | });
|
158 |
|
159 | it('should provide the data.count argument', function (done) {
|
160 | var push = PushNotification.init(options);
|
161 | push.on('notification', function (data) {
|
162 | expect(data.count).toEqual(1);
|
163 | done();
|
164 | });
|
165 | });
|
166 |
|
167 | it('should provide the data.sound argument', function (done) {
|
168 | var push = PushNotification.init(options);
|
169 | push.on('notification', function (data) {
|
170 | expect(data.sound).toEqual('beep');
|
171 | done();
|
172 | });
|
173 | });
|
174 |
|
175 | it('should provide the data.image argument', function (done) {
|
176 | var push = PushNotification.init(options);
|
177 | push.on('notification', function (data) {
|
178 | expect(data.image).toEqual('Image');
|
179 | done();
|
180 | });
|
181 | });
|
182 |
|
183 | it('should provide the data.additionalData argument', function (done) {
|
184 | var push = PushNotification.init(options);
|
185 | push.on('notification', function (data) {
|
186 | expect(data.additionalData).toEqual({});
|
187 | done();
|
188 | });
|
189 | });
|
190 | });
|
191 |
|
192 | describe('on "error" event', function () {
|
193 | it('should be emitted with an Error', function (done) {
|
194 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
195 | fail('something went wrong');
|
196 | });
|
197 | var push = PushNotification.init(options);
|
198 | push.on('error', function (e) {
|
199 | expect(e).toEqual(jasmine.any(Error));
|
200 | expect(e.message).toEqual('something went wrong');
|
201 | done();
|
202 | });
|
203 | });
|
204 | });
|
205 |
|
206 | describe('off "notification" event', function () {
|
207 | it('should exist and be registered a callback handle', function (done) {
|
208 | var push = PushNotification.init(options),
|
209 | eventHandler = function () {};
|
210 |
|
211 | push.on('notification', eventHandler);
|
212 |
|
213 | push.off('notification', eventHandler);
|
214 |
|
215 | expect(push.handlers.notification.indexOf(eventHandler)).toEqual(-1);
|
216 | done();
|
217 | });
|
218 | });
|
219 |
|
220 | describe('off "registration" event', function () {
|
221 | it('should exist and be registered a callback handle', function (done) {
|
222 | var push = PushNotification.init(options),
|
223 | eventHandler = function () {};
|
224 |
|
225 | push.on('registration', eventHandler);
|
226 |
|
227 | push.off('registration', eventHandler);
|
228 |
|
229 | expect(push.handlers.registration.indexOf(eventHandler)).toEqual(-1);
|
230 | done();
|
231 | });
|
232 | });
|
233 |
|
234 | describe('off "error" event', function () {
|
235 | it('should exist and be registered a callback handle', function (done) {
|
236 | var push = PushNotification.init(options),
|
237 | eventHandler = function () {};
|
238 |
|
239 | push.on('error', eventHandler);
|
240 | push.off('error', eventHandler);
|
241 |
|
242 | expect(push.handlers.error.indexOf(eventHandler)).toEqual(-1);
|
243 | done();
|
244 | });
|
245 | });
|
246 |
|
247 | describe('unregister method', function () {
|
248 | it('should clear "registration" event handlers', function (done) {
|
249 | var push = PushNotification.init(options),
|
250 | eventHandler = function () {};
|
251 |
|
252 | expect(push.handlers.registration.length).toEqual(0);
|
253 |
|
254 | push.on('registration', eventHandler);
|
255 |
|
256 | expect(push.handlers.registration.length).toEqual(1);
|
257 | expect(push.handlers.registration.indexOf(eventHandler)).toBeGreaterThan(-1);
|
258 |
|
259 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
260 | win();
|
261 | });
|
262 | push.unregister(function () {
|
263 | expect(push.handlers.registration.length).toEqual(0);
|
264 | expect(push.handlers.registration.indexOf(eventHandler)).toEqual(-1);
|
265 | done();
|
266 | });
|
267 | });
|
268 |
|
269 | it('should clear "notification" event handlers', function (done) {
|
270 | var push = PushNotification.init(options),
|
271 | eventHandler = function () {};
|
272 |
|
273 | expect(push.handlers.notification.length).toEqual(0);
|
274 |
|
275 | push.on('notification', eventHandler);
|
276 |
|
277 | expect(push.handlers.notification.length).toEqual(1);
|
278 | expect(push.handlers.notification.indexOf(eventHandler)).toBeGreaterThan(-1);
|
279 |
|
280 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
281 | win();
|
282 | });
|
283 | push.unregister(function () {
|
284 | expect(push.handlers.notification.length).toEqual(0);
|
285 | expect(push.handlers.notification.indexOf(eventHandler)).toEqual(-1);
|
286 | done();
|
287 | });
|
288 | });
|
289 |
|
290 | it('should clear "error" event handlers', function (done) {
|
291 | var push = PushNotification.init(options),
|
292 | eventHandler = function () {};
|
293 |
|
294 | expect(push.handlers.error.length).toEqual(0);
|
295 |
|
296 | push.on('error', eventHandler);
|
297 |
|
298 | expect(push.handlers.error.length).toEqual(1);
|
299 | expect(push.handlers.error.indexOf(eventHandler)).toBeGreaterThan(-1);
|
300 |
|
301 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
302 | win();
|
303 | });
|
304 | push.unregister(function () {
|
305 | expect(push.handlers.error.length).toEqual(0);
|
306 | expect(push.handlers.error.indexOf(eventHandler)).toEqual(-1);
|
307 | done();
|
308 | });
|
309 | });
|
310 | });
|
311 |
|
312 | describe('unregister topics method', function () {
|
313 | it('should not clear "registration" event handlers', function (done) {
|
314 | var push = PushNotification.init(options),
|
315 | eventHandler = function () {};
|
316 |
|
317 | expect(push.handlers.registration.length).toEqual(0);
|
318 |
|
319 | push.on('registration', eventHandler);
|
320 |
|
321 | expect(push.handlers.registration.length).toEqual(1);
|
322 | expect(push.handlers.registration.indexOf(eventHandler)).toBeGreaterThan(-1);
|
323 |
|
324 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
325 | win();
|
326 | });
|
327 | push.unregister(
|
328 | function () {
|
329 | expect(push.handlers.registration.length).toEqual(1);
|
330 | expect(push.handlers.registration.indexOf(eventHandler)).toBeGreaterThan(-1);
|
331 | done();
|
332 | },
|
333 | function () {},
|
334 | ['foo', 'bar']
|
335 | );
|
336 | });
|
337 |
|
338 | it('should not clear "notification" event handlers', function (done) {
|
339 | var push = PushNotification.init(options),
|
340 | eventHandler = function () {};
|
341 |
|
342 | expect(push.handlers.notification.length).toEqual(0);
|
343 |
|
344 | push.on('notification', eventHandler);
|
345 |
|
346 | expect(push.handlers.notification.length).toEqual(1);
|
347 | expect(push.handlers.notification.indexOf(eventHandler)).toBeGreaterThan(-1);
|
348 |
|
349 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
350 | win();
|
351 | });
|
352 | push.unregister(
|
353 | function () {
|
354 | expect(push.handlers.notification.length).toEqual(1);
|
355 | expect(push.handlers.notification.indexOf(eventHandler)).toBeGreaterThan(-1);
|
356 | done();
|
357 | },
|
358 | function () {},
|
359 | ['foo', 'bar']
|
360 | );
|
361 | });
|
362 |
|
363 | it('should not clear "error" event handlers', function (done) {
|
364 | var push = PushNotification.init(options),
|
365 | eventHandler = function () {};
|
366 |
|
367 | expect(push.handlers.error.length).toEqual(0);
|
368 |
|
369 | push.on('error', eventHandler);
|
370 |
|
371 | expect(push.handlers.error.length).toEqual(1);
|
372 | expect(push.handlers.error.indexOf(eventHandler)).toBeGreaterThan(-1);
|
373 |
|
374 | execSpy.andCallFake(function (win, fail, service, id, args) {
|
375 | win();
|
376 | });
|
377 | push.unregister(
|
378 | function () {
|
379 | expect(push.handlers.error.length).toEqual(1);
|
380 | expect(push.handlers.error.indexOf(eventHandler)).toBeGreaterThan(-1);
|
381 | done();
|
382 | },
|
383 | function () {},
|
384 | ['foo', 'bar']
|
385 | );
|
386 | });
|
387 | });
|
388 |
|
389 | describe('subscribe topic method', function () {
|
390 | describe('cordova.exec', function () {
|
391 | it('should call cordova.exec on next process tick', function (done) {
|
392 | var push = PushNotification.init(options);
|
393 | push.subscribe('foo', function () {}, function () {});
|
394 | setTimeout(function () {
|
395 | expect(execSpy).toHaveBeenCalledWith(
|
396 | jasmine.any(Function),
|
397 | jasmine.any(Function),
|
398 | 'PushNotification',
|
399 | 'subscribe',
|
400 | jasmine.any(Object)
|
401 | );
|
402 | done();
|
403 | }, 100);
|
404 | });
|
405 | });
|
406 | });
|
407 |
|
408 | describe('unsubscribe topic method', function () {
|
409 | describe('cordova.exec', function () {
|
410 | it('should call cordova.exec on next process tick', function (done) {
|
411 | var push = PushNotification.init(options);
|
412 | push.unsubscribe('foo', function () {}, function () {});
|
413 | setTimeout(function () {
|
414 | expect(execSpy).toHaveBeenCalledWith(
|
415 | jasmine.any(Function),
|
416 | jasmine.any(Function),
|
417 | 'PushNotification',
|
418 | 'unsubscribe',
|
419 | jasmine.any(Object)
|
420 | );
|
421 | done();
|
422 | }, 100);
|
423 | });
|
424 | });
|
425 | });
|
426 | });
|
427 | });
|