Skip to content

Commit e91fb42

Browse files
committed
Test for config call
1 parent 2c741e2 commit e91fb42

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

test/support/tester/NockOneSignalHelper.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ export class NockOneSignalHelper {
3939
});
4040
}
4141

42+
static nockGetConfig(appId: string, body: any): NockScopeWithResultPromise {
43+
return NockHelper.nockBase({
44+
method: "get",
45+
baseUrl: "https://onesignal.com",
46+
path: `/api/v1/sync/${appId}/web`,
47+
reply: {
48+
status: 200,
49+
body: body
50+
}
51+
});
52+
}
53+
4254
static nockNotificationConfirmedDelivery(notificationId?: string): NockScopeWithResultPromise {
4355
return NockHelper.nockBase({
4456
method: "put",

test/unit/context/sw/ServiceWorker.ts

Lines changed: 26 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

@@ -189,6 +190,31 @@ test('onNotificationClicked - notification click sends PUT api/v1/notification',
189190
});
190191
});
191192

193+
test('onNotificationClicked - get web config before open if empty in database', async t => {
194+
await Database.remove("Options", "defaultUrl");
195+
196+
// Assert the URL is empty in the database
197+
const urlBefore = await Database.get("Options", "defaultUrl");
198+
t.true(urlBefore === undefined || urlBefore === null);
199+
200+
const serverConfig = TestEnvironment.getFakeServerAppConfig(ConfigIntegrationKind.Custom, false, null, appConfig.appId);
201+
t.is(serverConfig.config.origin, "http://localhost:3000");
202+
const webConfigNock = NockOneSignalHelper.nockGetConfig(appConfig.appId, serverConfig);
203+
204+
const notificationId = Random.getRandomUuid();
205+
const notificationPutCall = NockOneSignalHelper.nockNotificationPut(notificationId);
206+
const notificationEvent = mockNotificationNotificationEventInit(notificationId);
207+
208+
await OSServiceWorker.onNotificationClicked(notificationEvent);
209+
210+
t.true(webConfigNock.nockScope.isDone());
211+
t.true(notificationPutCall.nockScope.isDone());
212+
213+
// Assert the URL is set after the handler runs
214+
const urlAfter = await Database.get("Options", "defaultUrl");
215+
t.is(urlAfter, "http://localhost:3000");
216+
});
217+
192218
test('onNotificationClicked - notification click count omitted when appId is null', async t => {
193219
await TestEnvironment.initializeForServiceWorker();
194220

0 commit comments

Comments
 (0)