|
1 | | -import { DateTime } from 'luxon'; |
2 | | -import { EVENTS, publish } from '../js/customEventHandler'; |
3 | | -import { INTRO_DONE_KEY, readIntroDone } from '../js/onboarding/onboardingHelper'; |
4 | | -import { storageSet } from '../js/plugin/storage'; |
5 | | -import { initPushNotify } from '../js/splash/pushNotifySettings'; |
6 | | -import { clearNotifMock, getOnList, getCalled } from '../__mocks__/pushNotificationMocks'; |
7 | | - |
8 | | -global.fetch = (url: string) => |
9 | | - new Promise((rs, rj) => { |
10 | | - setTimeout(() => |
11 | | - rs({ |
12 | | - json: () => |
13 | | - new Promise((rs, rj) => { |
14 | | - let myJSON = { |
15 | | - emSensorDataCollectionProtocol: { |
16 | | - protocol_id: '2014-04-6267', |
17 | | - approval_date: '2016-07-14', |
18 | | - }, |
19 | | - }; |
20 | | - setTimeout(() => rs(myJSON), 100); |
21 | | - }), |
22 | | - }), |
23 | | - ); |
24 | | - }) as any; |
| 1 | +import { initPushNotify, push } from '../js/splash/pushNotifySettings'; |
| 2 | +import { clearNotifMock, getListenerList, mockPushEvent } from '../__mocks__/pushNotificationMocks'; |
| 3 | +import { markConsented } from '../js/splash/startprefs'; |
| 4 | +import { waitFor } from '@testing-library/react-native'; |
25 | 5 |
|
26 | 6 | afterEach(() => { |
27 | 7 | clearNotifMock(); |
28 | 8 | }); |
29 | 9 |
|
30 | | -it('intro done does nothing if not registered', () => { |
31 | | - expect(getOnList()).toStrictEqual({}); |
32 | | - publish(EVENTS.INTRO_DONE_EVENT, 'test data'); |
33 | | - expect(getOnList()).toStrictEqual({}); |
34 | | -}); |
| 10 | +describe('pushNotifySettings', () => { |
| 11 | + describe('initPushNotify', () => { |
| 12 | + it('does not set up listeners if consent not given', async () => { |
| 13 | + expect(getListenerList()).toStrictEqual({}); |
| 14 | + await initPushNotify(); |
| 15 | + expect(getListenerList()).toStrictEqual({}); |
| 16 | + }); |
35 | 17 |
|
36 | | -it('intro done initializes the push notifications', () => { |
37 | | - expect(getOnList()).toStrictEqual({}); |
| 18 | + it('sets up listeners if consent given', async () => { |
| 19 | + await markConsented(); |
| 20 | + await initPushNotify(); |
| 21 | + expect(getListenerList()).toStrictEqual( |
| 22 | + expect.objectContaining({ |
| 23 | + notification: expect.any(Function), |
| 24 | + error: expect.any(Function), |
| 25 | + registration: expect.any(Function), |
| 26 | + }), |
| 27 | + ); |
| 28 | + }); |
38 | 29 |
|
39 | | - initPushNotify(); |
40 | | - publish(EVENTS.INTRO_DONE_EVENT, 'test data'); |
41 | | - expect(getOnList()).toStrictEqual( |
42 | | - expect.objectContaining({ |
43 | | - notification: expect.any(Function), |
44 | | - error: expect.any(Function), |
45 | | - registration: expect.any(Function), |
46 | | - }), |
47 | | - ); |
48 | | -}); |
| 30 | + it('handles visible notification', async () => { |
| 31 | + const InAppBrowserOpenSpy = jest.spyOn(window['cordova'].InAppBrowser, 'open'); |
| 32 | + await initPushNotify(); |
| 33 | + mockPushEvent('notification', { |
| 34 | + additionalData: { |
| 35 | + payload: { alert_type: 'website', spec: { url: 'https://foo.bar' } }, |
| 36 | + }, |
| 37 | + }); |
| 38 | + expect(InAppBrowserOpenSpy).toHaveBeenCalledWith( |
| 39 | + 'https://foo.bar', |
| 40 | + '_blank', |
| 41 | + 'location=yes,clearcache=no,toolbar=yes,hideurlbar=yes', |
| 42 | + ); |
| 43 | + }); |
49 | 44 |
|
50 | | -it('cloud event does nothing if not registered', () => { |
51 | | - expect(window['cordova'].platformId).toEqual('ios'); |
52 | | - publish(EVENTS.CLOUD_NOTIFICATION_EVENT, { |
53 | | - additionalData: { 'content-available': 1, payload: { notId: 3 } }, |
54 | | - }); |
55 | | - expect(getCalled()).toBeNull(); |
56 | | -}); |
| 45 | + it('handles silent notification', async () => { |
| 46 | + const BEMDataCollectionHandleSilentPushSpy = jest.spyOn( |
| 47 | + window['cordova'].plugins.BEMDataCollection, |
| 48 | + 'handleSilentPush', |
| 49 | + ); |
| 50 | + await initPushNotify(); |
| 51 | + const pushFinishSpy = jest.spyOn(push, 'finish'); |
57 | 52 |
|
58 | | -it('cloud event handles notification if registered', async () => { |
59 | | - expect(window['cordova'].platformId).toEqual('ios'); |
60 | | - initPushNotify(); |
61 | | - publish(EVENTS.INTRO_DONE_EVENT, 'intro done'); |
62 | | - publish(EVENTS.CLOUD_NOTIFICATION_EVENT, { |
63 | | - additionalData: { 'content-available': 1, payload: { notId: 3 } }, |
| 53 | + const NOTIF_ID = 98765; |
| 54 | + mockPushEvent('notification', { |
| 55 | + additionalData: { |
| 56 | + 'content-available': 1, |
| 57 | + payload: { notId: NOTIF_ID }, |
| 58 | + }, |
| 59 | + }); |
| 60 | + await waitFor(() => { |
| 61 | + expect(pushFinishSpy).toHaveBeenCalledWith(expect.anything(), expect.anything(), NOTIF_ID); |
| 62 | + expect(BEMDataCollectionHandleSilentPushSpy).toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + }); |
64 | 65 | }); |
65 | | - await new Promise((r) => setTimeout(r, 1000)); |
66 | | - expect(getCalled()).toEqual(3); |
67 | | -}); |
68 | | - |
69 | | -it('consent event does nothing if not registered', () => { |
70 | | - expect(getOnList()).toStrictEqual({}); |
71 | | - publish(EVENTS.CONSENTED_EVENT, 'test data'); |
72 | | - expect(getOnList()).toStrictEqual({}); |
73 | | -}); |
74 | | - |
75 | | -it('consent event registers if intro done', async () => { |
76 | | - //make sure the mock is clear |
77 | | - expect(getOnList()).toStrictEqual({}); |
78 | | - |
79 | | - //initialize the pushNotify, to subscribe to events |
80 | | - initPushNotify(); |
81 | | - |
82 | | - //mark the intro as done |
83 | | - const currDateTime = DateTime.now().toISO(); |
84 | | - let marked = await storageSet(INTRO_DONE_KEY, currDateTime); |
85 | | - let introDone = await readIntroDone(); |
86 | | - expect(introDone).toBeTruthy(); |
87 | | - |
88 | | - //publish consent event and check results |
89 | | - publish(EVENTS.CONSENTED_EVENT, 'test data'); |
90 | | - //have to wait a beat since event response is async |
91 | | - await new Promise((r) => setTimeout(r, 1000)); |
92 | | - expect(getOnList()).toStrictEqual( |
93 | | - expect.objectContaining({ |
94 | | - notification: expect.any(Function), |
95 | | - error: expect.any(Function), |
96 | | - registration: expect.any(Function), |
97 | | - }), |
98 | | - ); |
99 | | -}); |
100 | | - |
101 | | -it('consent event does not register if intro not done', () => { |
102 | | - expect(getOnList()).toStrictEqual({}); |
103 | | - initPushNotify(); |
104 | | - publish(EVENTS.CONSENTED_EVENT, 'test data'); |
105 | | - expect(getOnList()).toStrictEqual({}); //nothing, intro not done |
106 | 66 | }); |
0 commit comments