diff --git a/www/__mocks__/pushNotificationMocks.ts b/www/__mocks__/pushNotificationMocks.ts index 66bd550ca..c6f8548ed 100644 --- a/www/__mocks__/pushNotificationMocks.ts +++ b/www/__mocks__/pushNotificationMocks.ts @@ -1,5 +1,5 @@ let notifSettings; -let onList = {}; +let onList : any = {}; export const mockPushNotification = () => { window['PushNotification'] = { diff --git a/www/__tests__/pushNotifySettings.test.ts b/www/__tests__/pushNotifySettings.test.ts index 06621087c..7d35a6c11 100644 --- a/www/__tests__/pushNotifySettings.test.ts +++ b/www/__tests__/pushNotifySettings.test.ts @@ -1,8 +1,9 @@ import { EVENT_NAMES, publish } from '../js/customEventHandler'; +import { markIntroDone } from '../js/onboarding/onboardingHelper'; import { initPushNotify } from '../js/splash/pushNotifySettings'; import { mockCordova, mockBEMUserCache } from '../__mocks__/cordovaMocks'; import { mockLogger } from '../__mocks__/globalMocks'; -import { mockPushNotification } from '../__mocks__/pushNotificationMocks'; +import { clearNotifMock, getOnList, mockPushNotification } from '../__mocks__/pushNotificationMocks'; mockCordova(); mockLogger(); @@ -18,10 +19,64 @@ global.fetch = (url: string) => new Promise((rs, rj) => { })); }) as any; -it('initializes the push notifications', () => { - initPushNotify(); +it('intro done does nothing if not registered', () => { + clearNotifMock(); + expect(getOnList()).toStrictEqual({}); + publish(EVENT_NAMES.INTRO_DONE_EVENT, "test data"); + expect(getOnList()).toStrictEqual({}); +}) - publish(EVENT_NAMES.CONSENTED_EVENT, "test data"); +it('intro done initializes the push notifications', () => { + clearNotifMock(); + expect(getOnList()).toStrictEqual({}); + + initPushNotify(); publish(EVENT_NAMES.INTRO_DONE_EVENT, "test data"); + // setTimeout(() => {}, 100); + expect(getOnList()).toStrictEqual(expect.objectContaining({ + notification: expect.any(Function), + error: expect.any(Function), + registration: expect.any(Function) + })); +}) + +it('cloud event does nothing if not registered', () => { + publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {additionalData: {'content-avaliable': 1}}); + //how to test did nothing? +}) + +it('cloud event handles notification if registered', () => { + initPushNotify(); publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {additionalData: {'content-avaliable': 1}}); + //how to test did something? +}) + +it('consent event does nothing if not registered', () => { + clearNotifMock(); + expect(getOnList()).toStrictEqual({}); + publish(EVENT_NAMES.CONSENTED_EVENT, "test data"); + expect(getOnList()).toStrictEqual({}); +}) + +// it('consent event registers if intro done', () => { +// clearNotifMock(); +// expect(getOnList()).toStrictEqual({}); +// initPushNotify(); +// markIntroDone(); +// // setTimeout(() => {}, 100); +// publish(EVENT_NAMES.CONSENTED_EVENT, "test data"); +// setTimeout(() => {}, 200); +// expect(getOnList()).toStrictEqual(expect.objectContaining({ +// notification: expect.any(Function), +// error: expect.any(Function), +// registration: expect.any(Function) +// })); +// }) + +it('consent event does not register if intro not done', () => { + clearNotifMock(); + expect(getOnList()).toStrictEqual({}); + initPushNotify(); + publish(EVENT_NAMES.CONSENTED_EVENT, "test data"); + expect(getOnList()).toStrictEqual({}); //nothing, intro not done }) \ No newline at end of file diff --git a/www/js/splash/pushNotifySettings.ts b/www/js/splash/pushNotifySettings.ts index 29c036a85..a7a4ec001 100644 --- a/www/js/splash/pushNotifySettings.ts +++ b/www/js/splash/pushNotifySettings.ts @@ -217,6 +217,7 @@ export const initPushNotify = function () { .then(isConsented) .then(function (consentState) { if (consentState == true) { + logDebug("already consented, signing up for remote push"); registerPush(); } else { logDebug("no consent yet, waiting to sign up for remote push");