Skip to content

Commit 01d4e3e

Browse files
committed
test: add mockVapidSupport() and clearVapidSupport() helpers
Existing tests now explicitly mock VAPID support in beforeEach to simulate Safari 16.4+ Adds two new test cases for Safari < 16.4: - useSafariLegacyPush returns true even for new users when VAPID isn't available - getSubscriptionType returns _SafariLegacyPush when VAPID isn't available
1 parent fe0093d commit 01d4e3e

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

src/shared/environment/detect.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ const clearSafariWindow = () => {
3737
});
3838
};
3939

40+
const mockVapidSupport = () => {
41+
(globalThis as any).PushSubscriptionOptions = {
42+
prototype: { applicationServerKey: undefined },
43+
};
44+
};
45+
46+
const clearVapidSupport = () => {
47+
delete (globalThis as any).PushSubscriptionOptions;
48+
};
49+
4050
describe("Environment Helper", () => {
4151
test("can get api url ", async () => {
4252
// staging
@@ -78,6 +88,11 @@ describe("useSafariLegacyPush", () => {
7888
beforeEach(() => {
7989
TestEnvironment.initialize();
8090
clearSafariWindow();
91+
mockVapidSupport();
92+
});
93+
94+
afterEach(() => {
95+
clearVapidSupport();
8196
});
8297

8398
test("returns false when window.safari is undefined", () => {
@@ -99,7 +114,7 @@ describe("useSafariLegacyPush", () => {
99114
expect(useSafariLegacyPush()).toBe(false);
100115
});
101116

102-
test('returns false when legacy permission is "default" (new user)', () => {
117+
test('returns false when legacy permission is "default" (new user on Safari 16.4+)', () => {
103118
mockSafariPushNotification("default", null);
104119
OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID;
105120
expect(useSafariLegacyPush()).toBe(false);
@@ -122,12 +137,24 @@ describe("useSafariLegacyPush", () => {
122137
OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID;
123138
expect(useSafariLegacyPush()).toBe(true);
124139
});
140+
141+
test("returns true on Safari < 16.4 (no VAPID support) even for new users", () => {
142+
clearVapidSupport();
143+
mockSafariPushNotification("default", null);
144+
OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID;
145+
expect(useSafariLegacyPush()).toBe(true);
146+
});
125147
});
126148

127149
describe("getSubscriptionType", () => {
128150
beforeEach(() => {
129151
TestEnvironment.initialize();
130152
clearSafariWindow();
153+
mockVapidSupport();
154+
});
155+
156+
afterEach(() => {
157+
clearVapidSupport();
131158
});
132159

133160
test("returns SafariLegacyPush for existing legacy Safari subscribers", () => {
@@ -147,4 +174,11 @@ describe("getSubscriptionType", () => {
147174
OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID;
148175
expect(getSubscriptionType()).not.toBe(SubscriptionType._SafariLegacyPush);
149176
});
177+
178+
test("returns SafariLegacyPush on Safari < 16.4 (no VAPID support)", () => {
179+
clearVapidSupport();
180+
mockSafariPushNotification("default", null);
181+
OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID;
182+
expect(getSubscriptionType()).toBe(SubscriptionType._SafariLegacyPush);
183+
});
150184
});

0 commit comments

Comments
 (0)