Skip to content

Commit

Permalink
simplify event name storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Abby Wheelis committed Nov 6, 2023
1 parent dcf35fa commit 9b1665a
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
18 changes: 9 additions & 9 deletions www/__tests__/pushNotifySettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DateTime } from 'luxon';
import { EVENT_NAMES, publish } from '../js/customEventHandler';
import { EVENTS, publish } from '../js/customEventHandler';
import { INTRO_DONE_KEY, readIntroDone } from '../js/onboarding/onboardingHelper';
import { storageSet } from '../js/plugin/storage';
import { initPushNotify } from '../js/splash/pushNotifySettings';
Expand Down Expand Up @@ -42,15 +42,15 @@ afterEach(() => {

it('intro done does nothing if not registered', () => {
expect(getOnList()).toStrictEqual({});
publish(EVENT_NAMES.INTRO_DONE_EVENT, 'test data');
publish(EVENTS.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');
publish(EVENTS.INTRO_DONE_EVENT, 'test data');
expect(getOnList()).toStrictEqual(
expect.objectContaining({
notification: expect.any(Function),
Expand All @@ -62,7 +62,7 @@ it('intro done initializes the push notifications', () => {

it('cloud event does nothing if not registered', () => {
expect(window['cordova'].platformId).toEqual('ios');
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {
publish(EVENTS.CLOUD_NOTIFICATION_EVENT, {
additionalData: { 'content-available': 1, payload: { notId: 3 } },
});
expect(getCalled()).toBeNull();
Expand All @@ -71,8 +71,8 @@ it('cloud event does nothing if not registered', () => {
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, {
publish(EVENTS.INTRO_DONE_EVENT, 'intro done');
publish(EVENTS.CLOUD_NOTIFICATION_EVENT, {
additionalData: { 'content-available': 1, payload: { notId: 3 } },
});
await new Promise((r) => setTimeout(r, 1000));
Expand All @@ -81,7 +81,7 @@ it('cloud event handles notification if registered', async () => {

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

Expand All @@ -99,7 +99,7 @@ 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(EVENTS.CONSENTED_EVENT, 'test data');
//have to wait a beat since event response is async
await new Promise((r) => setTimeout(r, 1000));
expect(getOnList()).toStrictEqual(
Expand All @@ -114,6 +114,6 @@ it('consent event registers if intro done', async () => {
it('consent event does not register if intro not done', () => {
expect(getOnList()).toStrictEqual({});
initPushNotify();
publish(EVENT_NAMES.CONSENTED_EVENT, 'test data');
publish(EVENTS.CONSENTED_EVENT, 'test data');
expect(getOnList()).toStrictEqual({}); //nothing, intro not done
});
12 changes: 6 additions & 6 deletions www/__tests__/remoteNotifyHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EVENT_NAMES, publish } from '../js/customEventHandler';
import { EVENTS, publish } from '../js/customEventHandler';
import { initRemoteNotifyHandler } from '../js/splash/remoteNotifyHandler';
import {
clearURL,
Expand All @@ -25,15 +25,15 @@ beforeEach(() => {
});

it('does not adds a statEvent if not subscribed', async () => {
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, 'test data');
publish(EVENTS.CLOUD_NOTIFICATION_EVENT, 'test data');
const storedMessages = await db.getAllMessages('stats/client_nav_event', false);
expect(storedMessages).toEqual([]);
});

it('adds a statEvent if subscribed', async () => {
initRemoteNotifyHandler();
await new Promise((r) => setTimeout(r, 500)); //wait for subscription
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, 'test data');
publish(EVENTS.CLOUD_NOTIFICATION_EVENT, 'test data');
await new Promise((r) => setTimeout(r, 500)); //wait for event handling
const storedMessages = await db.getAllMessages('stats/client_nav_event', false);
expect(storedMessages).toContainEqual({
Expand All @@ -47,7 +47,7 @@ it('adds a statEvent if subscribed', async () => {

it('handles the url if subscribed', () => {
initRemoteNotifyHandler();
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {
publish(EVENTS.CLOUD_NOTIFICATION_EVENT, {
additionalData: {
payload: { alert_type: 'website', spec: { url: 'https://this_is_a_test.com' } },
},
Expand All @@ -57,7 +57,7 @@ it('handles the url if subscribed', () => {

it('handles the popup if subscribed', () => {
initRemoteNotifyHandler();
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {
publish(EVENTS.CLOUD_NOTIFICATION_EVENT, {
additionalData: {
payload: {
alert_type: 'popup',
Expand All @@ -70,7 +70,7 @@ it('handles the popup if subscribed', () => {

it('does nothing if subscribed and no data', () => {
initRemoteNotifyHandler();
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {});
publish(EVENTS.CLOUD_NOTIFICATION_EVENT, {});
expect(getURL()).toEqual('');
expect(getAlerts()).toEqual([]);
});
14 changes: 7 additions & 7 deletions www/__tests__/storeDeviceSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
mockGetAppVersion,
} from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import { EVENT_NAMES, publish } from '../js/customEventHandler';
import { EVENTS, publish } from '../js/customEventHandler';
import { markIntroDone } from '../js/onboarding/onboardingHelper';

mockBEMUserCache();
Expand Down Expand Up @@ -71,14 +71,14 @@ it('verifies my subscrition clearing', async () => {
initStoreDeviceSettings();
await new Promise((r) => setTimeout(r, 500));
teardownDeviceSettings();
publish(EVENT_NAMES.INTRO_DONE_EVENT, 'test data');
publish(EVENTS.INTRO_DONE_EVENT, 'test data');
let user = await getUser();
expect(user).toBeUndefined();
});

it('does not store if not subscribed', async () => {
publish(EVENT_NAMES.INTRO_DONE_EVENT, 'test data');
publish(EVENT_NAMES.CONSENTED_EVENT, 'test data');
publish(EVENTS.INTRO_DONE_EVENT, 'test data');
publish(EVENTS.CONSENTED_EVENT, 'test data');
await new Promise((r) => setTimeout(r, 500)); //time to carry out event handling
let user = await getUser();
expect(user).toBeUndefined();
Expand All @@ -87,7 +87,7 @@ it('does not store if not subscribed', async () => {
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(EVENTS.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({
Expand All @@ -101,7 +101,7 @@ it('stores device settings after consent if intro done', async () => {
await new Promise((r) => setTimeout(r, 500)); //time to check consent and subscribe
markIntroDone();
await new Promise((r) => setTimeout(r, 500));
publish(EVENT_NAMES.CONSENTED_EVENT, 'test data');
publish(EVENTS.CONSENTED_EVENT, 'test data');
await new Promise((r) => setTimeout(r, 500)); //time to carry out event handling
let user = await getUser();
expect(user).toMatchObject({
Expand All @@ -113,7 +113,7 @@ it('stores device settings after consent if intro done', async () => {
it('does not store device settings after consent if intro not done', async () => {
initStoreDeviceSettings();
await new Promise((r) => setTimeout(r, 500)); //time to check consent and subscribe
publish(EVENT_NAMES.CONSENTED_EVENT, 'test data');
publish(EVENTS.CONSENTED_EVENT, 'test data');
await new Promise((r) => setTimeout(r, 500)); //time to carry out event handling
let user = await getUser();
expect(user).toBeUndefined();
Expand Down
2 changes: 1 addition & 1 deletion www/js/customEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { logDebug } from './plugin/logger';
/**
* central source for event names
*/
export const EVENT_NAMES = {
export const EVENTS = {
CLOUD_NOTIFICATION_EVENT: 'cloud:push:notification',
CONSENTED_EVENT: 'data_collection_consented',
INTRO_DONE_EVENT: 'intro_done',
Expand Down
4 changes: 2 additions & 2 deletions www/js/onboarding/onboardingHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DateTime } from 'luxon';
import { getConfig, resetDataAndRefresh } from '../config/dynamicConfig';
import { storageGet, storageSet } from '../plugin/storage';
import { logDebug } from '../plugin/logger';
import { EVENT_NAMES, publish } from '../customEventHandler';
import { EVENTS, publish } from '../customEventHandler';
import { readConsentState, isConsented } from '../splash/startprefs';

export const INTRO_DONE_KEY = 'intro_done';
Expand Down Expand Up @@ -91,6 +91,6 @@ export async function markIntroDone() {
return storageSet(INTRO_DONE_KEY, currDateTime).then(() => {
//handle "on intro" events
logDebug('intro done, publishing event');
publish(EVENT_NAMES.INTRO_DONE_EVENT, currDateTime);
publish(EVENTS.INTRO_DONE_EVENT, currDateTime);
});
}
10 changes: 5 additions & 5 deletions www/js/splash/pushNotifySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import { updateUser } from '../commHelper';
import { logDebug, displayError } from '../plugin/logger';
import { publish, subscribe, EVENT_NAMES } from '../customEventHandler';
import { publish, subscribe, EVENTS } from '../customEventHandler';
import { isConsented, readConsentState } from './startprefs';
import { readIntroDone } from '../onboarding/onboardingHelper';

Expand Down Expand Up @@ -55,7 +55,7 @@ const startupInit = function () {
logDebug('No additional data defined, nothing to parse');
}
}
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, data);
publish(EVENTS.CLOUD_NOTIFICATION_EVENT, data);
});
};

Expand Down Expand Up @@ -233,9 +233,9 @@ export const initPushNotify = function () {
}
});

subscribe(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, (event) => onCloudEvent(event, event.detail));
subscribe(EVENT_NAMES.CONSENTED_EVENT, (event) => onConsentEvent(event, event.detail));
subscribe(EVENT_NAMES.INTRO_DONE_EVENT, (event) => onIntroEvent(event, event.detail));
subscribe(EVENTS.CLOUD_NOTIFICATION_EVENT, (event) => onCloudEvent(event, event.detail));
subscribe(EVENTS.CONSENTED_EVENT, (event) => onConsentEvent(event, event.detail));
subscribe(EVENTS.INTRO_DONE_EVENT, (event) => onIntroEvent(event, event.detail));

logDebug('pushnotify startup done');
};
4 changes: 2 additions & 2 deletions www/js/splash/remoteNotifyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* it only supports redirection to a specific app page. If the local
* notification handling gets more complex, we should consider decoupling it as well.
*/
import { EVENT_NAMES, subscribe } from '../customEventHandler';
import { EVENTS, subscribe } from '../customEventHandler';
import { addStatEvent, statKeys } from '../plugin/clientStats';
import { displayErrorMsg, logDebug } from '../plugin/logger';

Expand Down Expand Up @@ -84,5 +84,5 @@ const onCloudNotifEvent = (event) => {
* subscribes to cloud notification event
*/
export const initRemoteNotifyHandler = function () {
subscribe(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, onCloudNotifEvent);
subscribe(EVENTS.CLOUD_NOTIFICATION_EVENT, onCloudNotifEvent);
};
4 changes: 2 additions & 2 deletions www/js/splash/startprefs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { storageGet, storageSet } from '../plugin/storage';
import { logInfo, logDebug, displayErrorMsg } from '../plugin/logger';
import { EVENT_NAMES, publish } from '../customEventHandler';
import { EVENTS, publish } from '../customEventHandler';

// data collection consented protocol: string, represents the date on
// which the consented protocol was approved by the IRB
Expand Down Expand Up @@ -35,7 +35,7 @@ export function markConsented() {
// mark in local variable as well
_curr_consented = { ..._req_consent };
// publish event
publish(EVENT_NAMES.CONSENTED_EVENT, _req_consent);
publish(EVENTS.CONSENTED_EVENT, _req_consent);
})
.catch((error) => {
displayErrorMsg(error, 'Error while while wrting consent to storage');
Expand Down
10 changes: 5 additions & 5 deletions www/js/splash/storeDeviceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isConsented, readConsentState } from './startprefs';
import i18next from 'i18next';
import { displayError, logDebug } from '../plugin/logger';
import { readIntroDone } from '../onboarding/onboardingHelper';
import { subscribe, EVENT_NAMES, unsubscribe } from '../customEventHandler';
import { subscribe, EVENTS, unsubscribe } from '../customEventHandler';

/**
* @function Gathers information about the user's device and stores it
Expand Down Expand Up @@ -80,13 +80,13 @@ export const initStoreDeviceSettings = function () {
} else {
logDebug('no consent yet, waiting to store device settings in profile');
}
subscribe(EVENT_NAMES.CONSENTED_EVENT, onConsentEvent);
subscribe(EVENT_NAMES.INTRO_DONE_EVENT, onIntroEvent);
subscribe(EVENTS.CONSENTED_EVENT, onConsentEvent);
subscribe(EVENTS.INTRO_DONE_EVENT, onIntroEvent);
});
logDebug('storedevicesettings startup done');
};

export const teardownDeviceSettings = function () {
unsubscribe(EVENT_NAMES.CONSENTED_EVENT, onConsentEvent);
unsubscribe(EVENT_NAMES.INTRO_DONE_EVENT, onIntroEvent);
unsubscribe(EVENTS.CONSENTED_EVENT, onConsentEvent);
unsubscribe(EVENTS.INTRO_DONE_EVENT, onIntroEvent);
};

0 comments on commit 9b1665a

Please sign in to comment.