Skip to content

Commit

Permalink
test cloud event
Browse files Browse the repository at this point in the history
added in test for the cloud event when subscribed and when not subscribed. additional mocking was required to handle the silent push functions
  • Loading branch information
Abby Wheelis committed Nov 1, 2023
1 parent 82e864b commit 0eb7b4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
12 changes: 12 additions & 0 deletions www/__mocks__/cordovaMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ export const mockBEMDataCollection = () => {
setTimeout(() => {
_storage['config/consent'] = consentDoc;
}, 100)
},
getConfig: () => {
return new Promise<any>((rs, rj) => {
rs({ 'ios_use_remote_push_for_sync': true });
});
},
handleSilentPush: () => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
rs();
}, 100)
);
}
}
window['cordova'] ||= {};
Expand Down
9 changes: 9 additions & 0 deletions www/__mocks__/pushNotificationMocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let notifSettings;
let onList : any = {};
let called = null;

export const mockPushNotification = () => {
window['PushNotification'] = {
Expand All @@ -8,6 +9,9 @@ export const mockPushNotification = () => {
return {
on: (event: string, callback: Function) => {
onList[event] = callback;
},
finish: (content: any, errorFcn: Function, notID: any) => {
called = notID;
}
};
},
Expand All @@ -17,12 +21,17 @@ export const mockPushNotification = () => {
export const clearNotifMock = function () {
notifSettings = {};
onList = {};
called = null;
}

export const getOnList = function () {
return onList;
}

export const getCalled = function() {
return called;
}

export const fakeEvent = function (eventName : string) {
//fake the event by executing whatever we have stored for it
onList[eventName]();
Expand Down
23 changes: 15 additions & 8 deletions www/__tests__/pushNotifySettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { DateTime } from 'luxon';
import { EVENT_NAMES, publish } from '../js/customEventHandler';
import { INTRO_DONE_KEY, markIntroDone, readIntroDone } from '../js/onboarding/onboardingHelper';
import { INTRO_DONE_KEY, readIntroDone } from '../js/onboarding/onboardingHelper';
import { storageSet } from '../js/plugin/storage';
import { initPushNotify } from '../js/splash/pushNotifySettings';
import { mockCordova, mockBEMUserCache } from '../__mocks__/cordovaMocks';
import { mockCordova, mockBEMUserCache, mockBEMDataCollection } from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import { clearNotifMock, getOnList, mockPushNotification } from '../__mocks__/pushNotificationMocks';
import { clearNotifMock, getOnList, mockPushNotification, getCalled } from '../__mocks__/pushNotificationMocks';

mockCordova();
mockLogger();
mockPushNotification();
mockBEMUserCache();
mockBEMDataCollection();

global.fetch = (url: string) => new Promise((rs, rj) => {
setTimeout(() => rs({
json: () => new Promise((rs, rj) => {
let myJSON = { "emSensorDataCollectionProtocol": { "protocol_id": "2014-04-6267", "approval_date": "2016-07-14" } };
let myJSON = { "emSensorDataCollectionProtocol": { "protocol_id": "2014-04-6267", "approval_date": "2016-07-14" }, };
setTimeout(() => rs(myJSON), 100);
})
}));
Expand Down Expand Up @@ -43,14 +44,20 @@ it('intro done initializes the push notifications', () => {
})

it('cloud event does nothing if not registered', () => {
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {additionalData: {'content-avaliable': 1}});
//how to test did nothing?
expect(window['cordova'].platformId).toEqual('ios');
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {additionalData: {'content-available': 1, 'payload' : {'notID' : 3}}});
expect(getCalled()).toBeNull();
})

it('cloud event handles notification if registered', () => {
clearNotifMock();
expect(window['cordova'].platformId).toEqual('ios');
initPushNotify();
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {additionalData: {'content-avaliable': 1}});
//how to test did something?
publish(EVENT_NAMES.INTRO_DONE_EVENT, "intro done");
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {additionalData: {'content-available': 1, 'payload' : {'notID' : 3}}});
setTimeout(() => {
expect(getCalled()).toEqual(3);
}, 300)
})

it('consent event does nothing if not registered', () => {
Expand Down

0 comments on commit 0eb7b4b

Please sign in to comment.