Matteo Scandolo | 1a5bf20 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | describe('The xos.helper module', function(){ |
| 5 | describe('The xosNotification service', () => { |
| 6 | |
| 7 | let service, scope; |
| 8 | |
| 9 | const options = {icon: 'icon', body: 'message'}; |
| 10 | |
| 11 | let notificationMock = { |
| 12 | requestPermission: () => { |
| 13 | return { |
| 14 | then: cb => cb('granted') |
| 15 | } |
| 16 | }, |
| 17 | permission: 'granted' |
| 18 | } |
| 19 | |
| 20 | |
| 21 | // load the application module |
| 22 | beforeEach(module('xos.helpers', ($provide) => { |
| 23 | $provide.value('Notification', notificationMock); |
| 24 | })); |
| 25 | |
| 26 | // inject the cartService |
| 27 | beforeEach(inject(function (_xosNotification_, $rootScope) { |
| 28 | // The injector unwraps the underscores (_) from around the parameter names when matching |
| 29 | service = _xosNotification_; |
| 30 | scope = $rootScope; |
| 31 | spyOn(service, 'sendNotification'); |
| 32 | spyOn(service, 'checkPermission').and.callThrough(); |
| 33 | spyOn(notificationMock, 'requestPermission').and.callThrough(); |
| 34 | })); |
| 35 | |
| 36 | it('should exist', () => { |
| 37 | expect(service).toBeDefined(); |
| 38 | }); |
| 39 | |
| 40 | describe('when permission are granted', () => { |
| 41 | it('should send the notification', () => { |
| 42 | service.notify('Test', options); |
| 43 | expect(service.sendNotification).toHaveBeenCalledWith('Test', options); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | describe('when permission are not granted', () => { |
| 48 | beforeEach(() => { |
| 49 | notificationMock.permission = false; |
| 50 | }); |
| 51 | |
| 52 | it('should request permission', () => { |
| 53 | service.notify('Test', options); |
| 54 | expect(service.checkPermission).toHaveBeenCalled(); |
| 55 | scope.$apply(); // this resolve the promise |
| 56 | expect(service.sendNotification).toHaveBeenCalledWith('Test', options); |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | })(); |