Skip to content

Commit

Permalink
prettify recent changes
Browse files Browse the repository at this point in the history
a lot of the changes on my branch had not passed through the prettier workflow yet, this should hopefully be the last prettifying change on this branch, as I will keep up with the formatting as I edit from here forward
  • Loading branch information
Abby Wheelis committed Nov 2, 2023
1 parent 8764701 commit 4393362
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 225 deletions.
18 changes: 9 additions & 9 deletions www/__mocks__/pushNotificationMocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let notifSettings;
let onList : any = {};
let onList: any = {};
let called = null;

export const mockPushNotification = () => {
Expand All @@ -12,27 +12,27 @@ export const mockPushNotification = () => {
},
finish: (content: any, errorFcn: Function, notID: any) => {
called = notID;
}
},
};
},
};
}
};

export const clearNotifMock = function () {
notifSettings = {};
onList = {};
called = null;
}
};

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

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

export const fakeEvent = function (eventName : string) {
export const fakeEvent = function (eventName: string) {
//fake the event by executing whatever we have stored for it
onList[eventName]();
}
};
24 changes: 12 additions & 12 deletions www/__tests__/customEventHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { publish, subscribe, unsubscribe } from "../js/customEventHandler";
import { mockLogger } from "../__mocks__/globalMocks";
import { publish, subscribe, unsubscribe } from '../js/customEventHandler';
import { mockLogger } from '../__mocks__/globalMocks';

mockLogger();

it('subscribes and publishes to an event', () => {
const listener = jest.fn();
subscribe("test", listener);
publish("test", "test data");
subscribe('test', listener);
publish('test', 'test data');
expect(listener).toHaveBeenCalledWith(
expect.objectContaining({
type: "test",
detail: "test data",
})
type: 'test',
detail: 'test data',
}),
);
})
});

it('can unsubscribe', () => {
const listener = jest.fn();
subscribe("test", listener);
unsubscribe("test", listener);
publish("test", "test data");
subscribe('test', listener);
unsubscribe('test', listener);
publish('test', 'test data');
expect(listener).not.toHaveBeenCalled();
})
});
90 changes: 56 additions & 34 deletions www/__tests__/pushNotifySettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,85 @@ import { storageSet } from '../js/plugin/storage';
import { initPushNotify } from '../js/splash/pushNotifySettings';
import { mockCordova, mockBEMUserCache, mockBEMDataCollection } from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import { clearNotifMock, getOnList, mockPushNotification, getCalled } 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" }, };
setTimeout(() => rs(myJSON), 100);
})
}));
}) as any;
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',
},
};
setTimeout(() => rs(myJSON), 100);
}),
}),
);
}) as any;

afterEach(() => {
clearNotifMock();
});

it('intro done does nothing if not registered', () => {
expect(getOnList()).toStrictEqual({});
publish(EVENT_NAMES.INTRO_DONE_EVENT, "test data");
publish(EVENT_NAMES.INTRO_DONE_EVENT, 'test data');
expect(getOnList()).toStrictEqual({});
})
});

it('intro done initializes the push notifications', () => {
expect(getOnList()).toStrictEqual({});

initPushNotify();
publish(EVENT_NAMES.INTRO_DONE_EVENT, "test data");
expect(getOnList()).toStrictEqual(expect.objectContaining({
notification: expect.any(Function),
error: expect.any(Function),
registration: expect.any(Function)
}));
})
publish(EVENT_NAMES.INTRO_DONE_EVENT, 'test data');
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', () => {
expect(window['cordova'].platformId).toEqual('ios');
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {additionalData: {'content-available': 1, 'payload' : {'notId' : 3}}});
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {
additionalData: { 'content-available': 1, payload: { notId: 3 } },
});
expect(getCalled()).toBeNull();
})
});

it('cloud event handles notification if registered', async () => {
expect(window['cordova'].platformId).toEqual('ios');
initPushNotify();
publish(EVENT_NAMES.INTRO_DONE_EVENT, "intro done");
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {additionalData: {'content-available': 1, 'payload' : {'notId' : 3}}});
publish(EVENT_NAMES.INTRO_DONE_EVENT, 'intro done');
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {
additionalData: { 'content-available': 1, payload: { notId: 3 } },
});
await new Promise((r) => setTimeout(r, 1000));
expect(getCalled()).toEqual(3);
})
});

it('consent event does nothing if not registered', () => {
expect(getOnList()).toStrictEqual({});
publish(EVENT_NAMES.CONSENTED_EVENT, "test data");
publish(EVENT_NAMES.CONSENTED_EVENT, 'test data');
expect(getOnList()).toStrictEqual({});
})
});

it('consent event registers if intro done', async () => {
//make sure the mock is clear
Expand All @@ -79,19 +99,21 @@ it('consent event registers if intro done', async () => {
expect(introDone).toBeTruthy();

//publish consent event and check results
publish(EVENT_NAMES.CONSENTED_EVENT, "test data");
publish(EVENT_NAMES.CONSENTED_EVENT, 'test data');
//have to wait a beat since event response is async
await new Promise((r) => setTimeout(r, 1000));
expect(getOnList()).toStrictEqual(expect.objectContaining({
notification: expect.any(Function),
error: expect.any(Function),
registration: expect.any(Function)
}));
})
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', () => {
expect(getOnList()).toStrictEqual({});
initPushNotify();
publish(EVENT_NAMES.CONSENTED_EVENT, "test data");
publish(EVENT_NAMES.CONSENTED_EVENT, 'test data');
expect(getOnList()).toStrictEqual({}); //nothing, intro not done
})
});
54 changes: 33 additions & 21 deletions www/__tests__/storeDeviceSettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@

import { readConsentState, markConsented } from '../js/splash/startprefs';
import { storageClear } from '../js/plugin/storage';
import { getUser } from '../js/commHelper';
import { initStoreDeviceSettings, teardownDeviceSettings } from '../js/splash/storeDeviceSettings';
import { mockBEMServerCom, mockBEMUserCache, mockCordova, mockDevice, mockGetAppVersion } from '../__mocks__/cordovaMocks';
import {
mockBEMServerCom,
mockBEMUserCache,
mockCordova,
mockDevice,
mockGetAppVersion,
} from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import { EVENT_NAMES, publish, unsubscribe } from '../js/customEventHandler';

Expand All @@ -14,14 +19,23 @@ mockLogger();
mockGetAppVersion();
mockBEMServerCom();

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" } };
setTimeout(() => rs(myJSON), 100);
})
}));
}) as any;
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',
},
};
setTimeout(() => rs(myJSON), 100);
}),
}),
);
}) as any;

afterEach(async () => {
await storageClear({ local: true, native: true });
Expand All @@ -37,29 +51,27 @@ it('stores device settings when intialized after consent', async () => {
await new Promise((r) => setTimeout(r, 500));
let user = await getUser();
expect(user).toMatchObject({
client_os_version: '14.0.0',
client_app_version: '1.2.3'
})
client_os_version: '14.0.0',
client_app_version: '1.2.3',
});
});

it('does not store if not subscribed', async () => {
publish(EVENT_NAMES.INTRO_DONE_EVENT, "test data");
publish(EVENT_NAMES.CONSENTED_EVENT, "test data");
publish(EVENT_NAMES.INTRO_DONE_EVENT, 'test data');
publish(EVENT_NAMES.CONSENTED_EVENT, 'test data');
await new Promise((r) => setTimeout(r, 500)); //time to carry out event handling
let user = await getUser();
expect(user).toBeUndefined();
});


it('stores device settings after intro done', async () => {
initStoreDeviceSettings();
await new Promise((r) => setTimeout(r, 500)); //time to check consent and subscribe
publish(EVENT_NAMES.INTRO_DONE_EVENT, "test data");
publish(EVENT_NAMES.INTRO_DONE_EVENT, 'test data');
await new Promise((r) => setTimeout(r, 500)); //time to carry out event handling
let user = await getUser();
expect(user).toMatchObject({
client_os_version: '14.0.0',
client_app_version: '1.2.3'
})
client_os_version: '14.0.0',
client_app_version: '1.2.3',
});
});

24 changes: 12 additions & 12 deletions www/js/customEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* having the ability to broadcast and emit events prevents files from being tightly coupled
* if we want something else to happen when an event is emitted, we can just listen for it
* instead of having to change the code at the point the event is emitted
*
*
* looser coupling = point of broadcast doesn't 'know' what is triggered by that event
* leads to more extensible code
* consistent event names help us know what happens when
*
*
* code based on: https://blog.logrocket.com/using-custom-events-react/
*/

Expand All @@ -18,27 +18,27 @@ import { logDebug } from './plugin/logger';
*/
export const EVENT_NAMES = {
CLOUD_NOTIFICATION_EVENT: 'cloud:push:notification',
CONSENTED_EVENT: "data_collection_consented",
INTRO_DONE_EVENT: "intro_done",
}
CONSENTED_EVENT: 'data_collection_consented',
INTRO_DONE_EVENT: 'intro_done',
};

/**
* @function starts listening to an event
* @param eventName {string} the name of the event
* @param eventName {string} the name of the event
* @param listener event listener, function to execute on event
*/
export function subscribe(eventName: string, listener) {
logDebug("adding " + eventName + " listener");
logDebug('adding ' + eventName + ' listener');
document.addEventListener(eventName, listener);
}

/**
* @function stops listening to an event
* @param eventName {string} the name of the event
* @param eventName {string} the name of the event
* @param listener event listener, function to execute on event
*/
export function unsubscribe(eventName: string, listener){
logDebug("removing " + eventName + " listener");
export function unsubscribe(eventName: string, listener) {
logDebug('removing ' + eventName + ' listener');
document.removeEventListener(eventName, listener);
}

Expand All @@ -49,7 +49,7 @@ export function unsubscribe(eventName: string, listener){
* @param data any additional data to be added to event
*/
export function publish(eventName: string, data) {
logDebug("publishing " + eventName + " with data " + JSON.stringify(data));
logDebug('publishing ' + eventName + ' with data ' + JSON.stringify(data));
const event = new CustomEvent(eventName, { detail: data });
document.dispatchEvent(event);
}
}
Loading

0 comments on commit 4393362

Please sign in to comment.