Skip to content

Commit b27ee19

Browse files
committed
Test for updating config with config call
1 parent 6ee87e4 commit b27ee19

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

test/unit/context/sw/ServiceWorker.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import OneSignalUtils from '../../../../src/utils/OneSignalUtils';
1818
import { setupFakePlayerId } from '../../../support/tester/utils';
1919
import * as awaitableTimeout from '../../../../src/utils/AwaitableTimeout';
2020
import { NockOneSignalHelper } from '../../../../test/support/tester/NockOneSignalHelper';
21+
import { ConfigIntegrationKind } from '../../../../src/models/AppConfig';
2122

2223
declare var self: MockServiceWorkerGlobalScope;
2324

@@ -260,6 +261,44 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => {
260261
t.deepEqual(callOrder, ["notificationPut", "openWindow"]);
261262
});
262263

264+
test('onNotificationClicked - get web config before open if empty in database', async t => {
265+
await Database.remove("Options", "defaultUrl");
266+
267+
// Assert the URL is empty in the database
268+
const urlBefore = await Database.get("Options", "defaultUrl");
269+
t.true(urlBefore === undefined || urlBefore === null);
270+
271+
const serverConfig = TestEnvironment.getFakeServerAppConfig(ConfigIntegrationKind.Custom, false, null, appConfig.appId);
272+
nock("https://api.onesignal.com")
273+
.get(`sync/${appConfig.appId}/web`)
274+
.reply(200, () => {
275+
return serverConfig;
276+
});
277+
278+
const notificationId = Random.getRandomUuid();
279+
280+
const callOrder: string[] = [];
281+
sandbox.stub(self.clients, "openWindow", function() {
282+
callOrder.push("openWindow");
283+
});
284+
285+
nock("https://onesignal.com")
286+
.put(`/api/v1/notifications/${notificationId}`)
287+
.reply(200, (_uri: string, _requestBody: string) => {
288+
callOrder.push("notificationPut");
289+
return { success: true };
290+
});
291+
292+
const notificationEvent = mockNotificationNotificationEventInit(notificationId);
293+
await OSServiceWorker.onNotificationClicked(notificationEvent);
294+
295+
t.deepEqual(callOrder, ["notificationPut", "openWindow"]);
296+
297+
// Assert the URL is set after the handler runs
298+
const urlAfter = await Database.get("Options", "defaultUrl");
299+
t.is(urlAfter, "http://localhost:3000");
300+
});
301+
263302
/***************************************************
264303
* sendConfirmedDelivery()
265304
****************************************************/

0 commit comments

Comments
 (0)