Skip to content

Commit

Permalink
complete writing testing
Browse files Browse the repository at this point in the history
adding more tests - leveraged mocks to ensure that calls to open url or create popup were happening at the appropriate times
  • Loading branch information
Abby Wheelis committed Nov 3, 2023
1 parent 87de6c2 commit dcf35fa
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
19 changes: 19 additions & 0 deletions www/__mocks__/cordovaMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,22 @@ export const mockBEMServerCom = () => {
};
window['cordova'].plugins.BEMServerComm = mockBEMServerCom;
};

let _url_stash = '';

export const mockInAppBrowser = () => {
const mockInAppBrowser = {
open: (url: string, mode: string, options: {}) => {
_url_stash = url;
},
};
window['cordova'].InAppBrowser = mockInAppBrowser;
};

export const getURL = () => {
return _url_stash;
};

export const clearURL = () => {
_url_stash = '';
};
16 changes: 16 additions & 0 deletions www/__mocks__/globalMocks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export const mockLogger = () => {
window['Logger'] = { log: console.log };
};

let alerts = [];

export const mockAlert = () => {
window['alert'] = (message) => {
alerts.push(message);
};
};

export const clearAlerts = () => {
alerts = [];
};

export const getAlerts = () => {
return alerts;
};
47 changes: 45 additions & 2 deletions www/__tests__/remoteNotifyHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { EVENT_NAMES, publish } from '../js/customEventHandler';
import { initRemoteNotifyHandler } from '../js/splash/remoteNotifyHandler';
import { mockBEMUserCache, mockDevice, mockGetAppVersion } from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import {
clearURL,
getURL,
mockBEMUserCache,
mockDevice,
mockGetAppVersion,
mockInAppBrowser,
} from '../__mocks__/cordovaMocks';
import { clearAlerts, getAlerts, mockAlert, mockLogger } from '../__mocks__/globalMocks';

mockLogger();
mockDevice();
mockBEMUserCache();
mockGetAppVersion();
mockInAppBrowser();
mockAlert();

const db = window['cordova']?.plugins?.BEMUserCache;

beforeEach(() => {
clearURL();
clearAlerts();
});

it('does not adds a statEvent if not subscribed', async () => {
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, 'test data');
const storedMessages = await db.getAllMessages('stats/client_nav_event', false);
Expand All @@ -31,3 +45,32 @@ it('adds a statEvent if subscribed', async () => {
});
});

it('handles the url if subscribed', () => {
initRemoteNotifyHandler();
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {
additionalData: {
payload: { alert_type: 'website', spec: { url: 'https://this_is_a_test.com' } },
},
});
expect(getURL()).toBe('https://this_is_a_test.com');
});

it('handles the popup if subscribed', () => {
initRemoteNotifyHandler();
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {
additionalData: {
payload: {
alert_type: 'popup',
spec: { title: 'Hello', text: 'World' },
},
},
});
expect(getAlerts()).toEqual(expect.arrayContaining(['━━━━\nHello\n━━━━\nWorld']));
});

it('does nothing if subscribed and no data', () => {
initRemoteNotifyHandler();
publish(EVENT_NAMES.CLOUD_NOTIFICATION_EVENT, {});
expect(getURL()).toEqual('');
expect(getAlerts()).toEqual([]);
});

0 comments on commit dcf35fa

Please sign in to comment.