|
| 1 | +import { APP_ID } from '__test__/support/constants'; |
| 2 | +import TestContext from '__test__/support/environment/TestContext'; |
| 3 | +import { TestEnvironment } from '__test__/support/environment/TestEnvironment'; |
| 4 | +import ContextSW from '../models/ContextSW'; |
| 5 | +import { RawPushSubscription } from '../models/RawPushSubscription'; |
| 6 | +import { |
| 7 | + SubscriptionManager, |
| 8 | + SubscriptionManagerConfig, |
| 9 | +} from './SubscriptionManager'; |
| 10 | + |
| 11 | +const subConfig: SubscriptionManagerConfig = { |
| 12 | + appId: APP_ID, |
| 13 | + vapidPublicKey: '', |
| 14 | + onesignalVapidPublicKey: '', |
| 15 | +}; |
| 16 | + |
| 17 | +const getRawSubscription = (): RawPushSubscription => { |
| 18 | + const rawSubscription = new RawPushSubscription(); |
| 19 | + rawSubscription.w3cAuth = 'auth'; |
| 20 | + rawSubscription.w3cP256dh = 'p256dh'; |
| 21 | + rawSubscription.w3cEndpoint = new URL('https://example.com/endpoint'); |
| 22 | + rawSubscription.safariDeviceToken = 'safariDeviceToken'; |
| 23 | + return rawSubscription; |
| 24 | +}; |
| 25 | + |
| 26 | +describe('SubscriptionManager', () => { |
| 27 | + beforeAll(async () => { |
| 28 | + await TestEnvironment.initialize(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('updatePushSubscriptionModelWithRawSubscription', () => { |
| 32 | + test('should create the push subscription model if it does not exist', async () => { |
| 33 | + const context = new ContextSW(TestContext.getFakeMergedConfig()); |
| 34 | + const subscriptionManager = new SubscriptionManager(context, subConfig); |
| 35 | + const rawSubscription = getRawSubscription(); |
| 36 | + |
| 37 | + let subModels = |
| 38 | + await OneSignal.coreDirector.subscriptionModelStore.list(); |
| 39 | + expect(subModels.length).toBe(0); |
| 40 | + await subscriptionManager._updatePushSubscriptionModelWithRawSubscription( |
| 41 | + rawSubscription, |
| 42 | + ); |
| 43 | + |
| 44 | + subModels = await OneSignal.coreDirector.subscriptionModelStore.list(); |
| 45 | + expect(subModels.length).toBe(1); |
| 46 | + expect(subModels[0].toJSON()).toEqual({ |
| 47 | + device_model: '', |
| 48 | + device_os: 56, |
| 49 | + enabled: true, |
| 50 | + notification_types: 1, |
| 51 | + sdk: '1', |
| 52 | + token: rawSubscription.w3cEndpoint?.toString(), |
| 53 | + type: 'ChromePush', |
| 54 | + web_auth: rawSubscription.w3cAuth, |
| 55 | + web_p256: rawSubscription.w3cP256dh, |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | +}); |
0 commit comments