|
1 | | -import { describe, expect, test, vi } from 'vite-plus/test'; |
| 1 | +import { TestEnvironment } from "__test__/support/environment/TestEnvironment"; |
| 2 | +import { describe, expect, test, vi } from "vite-plus/test"; |
| 3 | +import { SubscriptionType } from "../subscriptions/constants"; |
| 4 | +import { getSubscriptionType, useSafariLegacyPush } from "./detect"; |
2 | 5 |
|
3 | | -let getOneSignalApiUrl: typeof import('src/shared/environment/detect').getOneSignalApiUrl; |
| 6 | +let getOneSignalApiUrl: typeof import("src/shared/environment/detect").getOneSignalApiUrl; |
4 | 7 |
|
5 | 8 | const resetModules = async () => { |
6 | 9 | vi.resetModules(); |
7 | | - getOneSignalApiUrl = (await import('src/shared/environment/detect')).getOneSignalApiUrl; |
| 10 | + getOneSignalApiUrl = (await import("src/shared/environment/detect")) |
| 11 | + .getOneSignalApiUrl; |
8 | 12 | }; |
9 | 13 |
|
10 | | -describe('Environment Helper', () => { |
11 | | - test('can get api url ', async () => { |
| 14 | +const FAKE_SAFARI_WEB_ID = |
| 15 | + "web.onesignal.auto.017d7a1b-f1ef-4fce-a00c-21a546b5491d"; |
| 16 | + |
| 17 | +const mockSafariPushNotification = ( |
| 18 | + permission: "default" | "granted" | "denied", |
| 19 | + deviceToken: string | null, |
| 20 | +) => { |
| 21 | + Object.defineProperty(window, "safari", { |
| 22 | + value: { |
| 23 | + pushNotification: { |
| 24 | + permission: () => ({ permission, deviceToken }), |
| 25 | + }, |
| 26 | + }, |
| 27 | + writable: true, |
| 28 | + configurable: true, |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +const clearSafariWindow = () => { |
| 33 | + Object.defineProperty(window, "safari", { |
| 34 | + value: undefined, |
| 35 | + writable: true, |
| 36 | + configurable: true, |
| 37 | + }); |
| 38 | +}; |
| 39 | + |
| 40 | +describe("Environment Helper", () => { |
| 41 | + test("can get api url ", async () => { |
12 | 42 | // staging |
13 | | - (global as any).__API_TYPE__ = 'staging'; |
14 | | - (global as any).__API_ORIGIN__ = 'onesignal-staging.com'; |
| 43 | + (global as any).__API_TYPE__ = "staging"; |
| 44 | + (global as any).__API_ORIGIN__ = "onesignal-staging.com"; |
15 | 45 | await resetModules(); |
16 | | - expect(getOneSignalApiUrl().toString()).toBe('https://onesignal-staging.com/api/v1/'); |
| 46 | + expect(getOneSignalApiUrl().toString()).toBe( |
| 47 | + "https://onesignal-staging.com/api/v1/", |
| 48 | + ); |
17 | 49 |
|
18 | 50 | // development - turbine endpoint |
19 | | - (global as any).__API_ORIGIN__ = 'localhost'; |
20 | | - (global as any).__API_TYPE__ = 'development'; |
| 51 | + (global as any).__API_ORIGIN__ = "localhost"; |
| 52 | + (global as any).__API_TYPE__ = "development"; |
21 | 53 | await resetModules(); |
22 | | - expect(getOneSignalApiUrl().toString()).toBe('http://localhost:3000/api/v1/'); |
| 54 | + expect(getOneSignalApiUrl().toString()).toBe( |
| 55 | + "http://localhost:3000/api/v1/", |
| 56 | + ); |
23 | 57 | expect( |
24 | 58 | getOneSignalApiUrl({ |
25 | | - action: 'outcomes', |
| 59 | + action: "outcomes", |
26 | 60 | }).toString(), |
27 | | - ).toBe('http://localhost:18080/api/v1/'); |
| 61 | + ).toBe("http://localhost:18080/api/v1/"); |
28 | 62 |
|
29 | 63 | // production |
30 | | - (global as any).__API_TYPE__ = 'production'; |
| 64 | + (global as any).__API_TYPE__ = "production"; |
31 | 65 | await resetModules(); |
32 | | - expect(getOneSignalApiUrl().toString()).toBe('https://api.onesignal.com/'); |
| 66 | + expect(getOneSignalApiUrl().toString()).toBe("https://api.onesignal.com/"); |
33 | 67 |
|
34 | 68 | // production - legacy |
35 | 69 | expect( |
36 | 70 | getOneSignalApiUrl({ |
37 | 71 | legacy: true, |
38 | 72 | }).toString(), |
39 | | - ).toBe('https://onesignal.com/api/v1/'); |
| 73 | + ).toBe("https://onesignal.com/api/v1/"); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe("useSafariLegacyPush", () => { |
| 78 | + beforeEach(() => { |
| 79 | + TestEnvironment.initialize(); |
| 80 | + clearSafariWindow(); |
| 81 | + }); |
| 82 | + |
| 83 | + test("returns false when window.safari is undefined", () => { |
| 84 | + expect(useSafariLegacyPush()).toBe(false); |
| 85 | + }); |
| 86 | + |
| 87 | + test("returns false when safari.pushNotification is undefined", () => { |
| 88 | + Object.defineProperty(window, "safari", { |
| 89 | + value: {}, |
| 90 | + writable: true, |
| 91 | + configurable: true, |
| 92 | + }); |
| 93 | + expect(useSafariLegacyPush()).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + test("returns false when safariWebId is not configured", () => { |
| 97 | + mockSafariPushNotification("granted", "abc123"); |
| 98 | + OneSignal.config!.safariWebId = undefined; |
| 99 | + expect(useSafariLegacyPush()).toBe(false); |
| 100 | + }); |
| 101 | + |
| 102 | + test('returns false when legacy permission is "default" (new user)', () => { |
| 103 | + mockSafariPushNotification("default", null); |
| 104 | + OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID; |
| 105 | + expect(useSafariLegacyPush()).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + test('returns false when legacy permission is "denied"', () => { |
| 109 | + mockSafariPushNotification("denied", null); |
| 110 | + OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID; |
| 111 | + expect(useSafariLegacyPush()).toBe(false); |
| 112 | + }); |
| 113 | + |
| 114 | + test('returns false when permission is "granted" but deviceToken is null', () => { |
| 115 | + mockSafariPushNotification("granted", null); |
| 116 | + OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID; |
| 117 | + expect(useSafariLegacyPush()).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + test("returns true when user has an active legacy subscription", () => { |
| 121 | + mockSafariPushNotification("granted", "aabbccdd11223344"); |
| 122 | + OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID; |
| 123 | + expect(useSafariLegacyPush()).toBe(true); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +describe("getSubscriptionType", () => { |
| 128 | + beforeEach(() => { |
| 129 | + TestEnvironment.initialize(); |
| 130 | + clearSafariWindow(); |
| 131 | + }); |
| 132 | + |
| 133 | + test("returns SafariLegacyPush for existing legacy Safari subscribers", () => { |
| 134 | + mockSafariPushNotification("granted", "aabbccdd11223344"); |
| 135 | + OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID; |
| 136 | + expect(getSubscriptionType()).toBe(SubscriptionType._SafariLegacyPush); |
| 137 | + }); |
| 138 | + |
| 139 | + test("returns SafariPush (VAPID) for new Safari users on Safari 16.4+", () => { |
| 140 | + mockSafariPushNotification("default", null); |
| 141 | + OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID; |
| 142 | + expect(getSubscriptionType()).not.toBe(SubscriptionType._SafariLegacyPush); |
| 143 | + }); |
| 144 | + |
| 145 | + test("does not return SafariLegacyPush when safari window exists but no legacy subscription", () => { |
| 146 | + mockSafariPushNotification("denied", null); |
| 147 | + OneSignal.config!.safariWebId = FAKE_SAFARI_WEB_ID; |
| 148 | + expect(getSubscriptionType()).not.toBe(SubscriptionType._SafariLegacyPush); |
40 | 149 | }); |
41 | 150 | }); |
0 commit comments