Skip to content

Commit f24e3dd

Browse files
committed
wip: test
1 parent 6ee87e4 commit f24e3dd

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

src/service-worker/ServiceWorker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -771,17 +771,17 @@ export class ServiceWorker {
771771
const defaultNotificationUrlFromConfig = config.origin;
772772
if (defaultNotificationUrlFromConfig) {
773773
launchUrl = defaultNotificationUrlFromConfig;
774+
775+
if (!dbDefaultNotificationUrl) {
776+
// intentionally not awaiting this promise to not block notification click handling
777+
await Database.setAppState({ defaultNotificationUrl: defaultNotificationUrlFromConfig }).catch(e => {
778+
Log.error("Failed to save default notification url to db", e);
779+
});
780+
}
774781
}
775782
} catch (e) {
776783
Log.error("Failed to get app config to determine default notification url", e);
777784
}
778-
779-
if (!!launchUrl && !dbDefaultNotificationUrl) {
780-
// intentionally not awaiting this promise to not block notification click handling
781-
Database.setAppState({ defaultNotificationUrl: launchUrl }).catch(e => {
782-
Log.error("Failed to save default notification url to db", e);
783-
});
784-
}
785785
}
786786

787787
// If the user clicked an action button, use the URL provided by the action button

test/support/tester/NockOneSignalHelper.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ export class NockOneSignalHelper {
3838
}
3939
});
4040
}
41+
42+
static nockGetConfig(appId: string, body: any): NockScopeWithResultPromise {
43+
return NockHelper.nockBase({
44+
method: "get",
45+
baseUrl: "https://onesignal.com",
46+
path: `/sync/${appId}/web`,
47+
reply: {
48+
status: 200,
49+
body: body
50+
}
51+
});
52+
}
4153

4254
static nockNotificationConfirmedDelivery(notificationId?: string): NockScopeWithResultPromise {
4355
return NockHelper.nockBase({

test/unit/context/sw/ServiceWorker.ts

Lines changed: 33 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,38 @@ 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+
t.is(serverConfig.config.origin, "http://localhost:3000");
273+
const webConfigNock = NockOneSignalHelper.nockGetConfig(appConfig.appId, serverConfig);
274+
275+
const notificationId = Random.getRandomUuid();
276+
277+
const callOrder: string[] = [];
278+
sandbox.stub(self.clients, "openWindow", function() {
279+
callOrder.push("openWindow");
280+
});
281+
282+
const notificationPutCall = NockOneSignalHelper.nockNotificationPut(notificationId);
283+
284+
const notificationEvent = mockNotificationNotificationEventInit(notificationId);
285+
await OSServiceWorker.onNotificationClicked(notificationEvent);
286+
287+
t.true(webConfigNock.nockScope.isDone());
288+
t.true(notificationPutCall.nockScope.isDone());
289+
t.deepEqual(callOrder, ["notificationPut", "openWindow"]);
290+
291+
// Assert the URL is set after the handler runs
292+
const urlAfter = await Database.get("Options", "defaultUrl");
293+
t.is(urlAfter, "http://localhost:3000");
294+
});
295+
263296
/***************************************************
264297
* sendConfirmedDelivery()
265298
****************************************************/

0 commit comments

Comments
 (0)