1- import { APP_ID } from '__test__/support/constants' ;
1+ import { APP_ID , DUMMY_EXTERNAL_ID } from '__test__/support/constants' ;
22import TestContext from '__test__/support/environment/TestContext' ;
33import { TestEnvironment } from '__test__/support/environment/TestEnvironment' ;
4+ import { setupSubModelStore } from '__test__/support/environment/TestEnvironmentHelpers' ;
5+ import {
6+ createUserFn ,
7+ setCreateUserResponse ,
8+ } from '__test__/support/helpers/requests' ;
9+ import {
10+ getSubscriptionFn ,
11+ MockServiceWorker ,
12+ } from '__test__/support/mocks/MockServiceWorker' ;
413import ContextSW from '../models/ContextSW' ;
514import { RawPushSubscription } from '../models/RawPushSubscription' ;
15+ import { IDManager } from './IDManager' ;
616import {
717 SubscriptionManager ,
818 SubscriptionManagerConfig ,
@@ -24,26 +34,38 @@ const getRawSubscription = (): RawPushSubscription => {
2434} ;
2535
2636describe ( 'SubscriptionManager' , ( ) => {
27- beforeAll ( async ( ) => {
37+ beforeEach ( async ( ) => {
38+ vi . resetModules ( ) ;
2839 await TestEnvironment . initialize ( ) ;
2940 } ) ;
3041
3142 describe ( 'updatePushSubscriptionModelWithRawSubscription' , ( ) => {
3243 test ( 'should create the push subscription model if it does not exist' , async ( ) => {
44+ setCreateUserResponse ( ) ;
3345 const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
3446 const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
3547 const rawSubscription = getRawSubscription ( ) ;
3648
3749 let subModels =
3850 await OneSignal . coreDirector . subscriptionModelStore . list ( ) ;
3951 expect ( subModels . length ) . toBe ( 0 ) ;
52+
53+ // mimicing the event helper checkAndTriggerSubscriptionChanged
54+ await OneSignal . database . setPushToken (
55+ rawSubscription . w3cEndpoint ?. toString ( ) ,
56+ ) ;
57+
4058 await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
4159 rawSubscription ,
4260 ) ;
4361
4462 subModels = await OneSignal . coreDirector . subscriptionModelStore . list ( ) ;
4563 expect ( subModels . length ) . toBe ( 1 ) ;
64+
65+ const id = subModels [ 0 ] . id ;
66+ expect ( IDManager . isLocalId ( id ) ) . toBe ( true ) ;
4667 expect ( subModels [ 0 ] . toJSON ( ) ) . toEqual ( {
68+ id,
4769 device_model : '' ,
4870 device_os : 56 ,
4971 enabled : true ,
@@ -54,6 +76,122 @@ describe('SubscriptionManager', () => {
5476 web_auth : rawSubscription . w3cAuth ,
5577 web_p256 : rawSubscription . w3cP256dh ,
5678 } ) ;
79+
80+ await vi . waitUntil ( ( ) => createUserFn . mock . calls . length > 0 ) ;
81+ expect ( createUserFn ) . toHaveBeenCalledWith ( {
82+ identity : { } ,
83+ properties : {
84+ language : 'en' ,
85+ timezone_id : 'America/Los_Angeles' ,
86+ } ,
87+ refresh_device_metadata : true ,
88+ subscriptions : [
89+ {
90+ device_model : '' ,
91+ device_os : 56 ,
92+ enabled : true ,
93+ notification_types : 1 ,
94+ sdk : '1' ,
95+ token : rawSubscription . w3cEndpoint ?. toString ( ) ,
96+ type : 'ChromePush' ,
97+ web_auth : rawSubscription . w3cAuth ,
98+ web_p256 : rawSubscription . w3cP256dh ,
99+ } ,
100+ ] ,
101+ } ) ;
102+ } ) ;
103+
104+ test ( 'should create user if push subscription model does not have an id' , async ( ) => {
105+ const generatePushSubscriptionModelSpy = vi . spyOn (
106+ OneSignal . coreDirector ,
107+ 'generatePushSubscriptionModel' ,
108+ ) ;
109+ const rawSubscription = getRawSubscription ( ) ;
110+ getSubscriptionFn . mockResolvedValue ( {
111+ endpoint : rawSubscription . w3cEndpoint ?. toString ( ) ,
112+ } ) ;
113+ setCreateUserResponse ( {
114+ externalId : 'some-external-id' ,
115+ } ) ;
116+
117+ const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
118+ const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
119+
120+ // create push sub with no id
121+ const identityModel = OneSignal . coreDirector . getIdentityModel ( ) ;
122+ identityModel . onesignalId = IDManager . createLocalId ( ) ;
123+ identityModel . externalId = 'some-external-id' ;
124+
125+ await setupSubModelStore ( {
126+ id : '' ,
127+ token : rawSubscription . w3cEndpoint ?. toString ( ) ,
128+ onesignalId : identityModel . onesignalId ,
129+ } ) ;
130+
131+ await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
132+ rawSubscription ,
133+ ) ;
134+
135+ // should not call generatePushSubscriptionModelSpy
136+ expect ( generatePushSubscriptionModelSpy ) . not . toHaveBeenCalled ( ) ;
137+
138+ expect ( createUserFn ) . toHaveBeenCalledWith ( {
139+ identity : {
140+ external_id : 'some-external-id' ,
141+ } ,
142+ properties : {
143+ language : 'en' ,
144+ timezone_id : 'America/Los_Angeles' ,
145+ } ,
146+ refresh_device_metadata : true ,
147+ subscriptions : [
148+ {
149+ device_model : '' ,
150+ device_os : 56 ,
151+ enabled : true ,
152+ notification_types : 1 ,
153+ sdk : '1' ,
154+ token : rawSubscription . w3cEndpoint ?. toString ( ) ,
155+ type : 'ChromePush' ,
156+ } ,
157+ ] ,
158+ } ) ;
159+ } ) ;
160+
161+ test ( 'should update the push subscription model if it already exists' , async ( ) => {
162+ setCreateUserResponse ( ) ;
163+ const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
164+ const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
165+ const rawSubscription = getRawSubscription ( ) ;
166+
167+ await OneSignal . database . setPushToken (
168+ rawSubscription . w3cEndpoint ?. toString ( ) ,
169+ ) ;
170+
171+ const pushModel = await setupSubModelStore ( {
172+ id : '123' ,
173+ token : rawSubscription . w3cEndpoint ?. toString ( ) ,
174+ onesignalId : DUMMY_EXTERNAL_ID ,
175+ } ) ;
176+ pushModel . web_auth = 'old-web-auth' ;
177+ pushModel . web_p256 = 'old-web-p256' ;
178+
179+ await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
180+ rawSubscription ,
181+ ) ;
182+
183+ const updatedPushModel =
184+ ( await OneSignal . coreDirector . getPushSubscriptionModel ( ) ) ! ;
185+ expect ( updatedPushModel . token ) . toBe (
186+ rawSubscription . w3cEndpoint ?. toString ( ) ,
187+ ) ;
188+ expect ( updatedPushModel . web_auth ) . toBe ( rawSubscription . w3cAuth ) ;
189+ expect ( updatedPushModel . web_p256 ) . toBe ( rawSubscription . w3cP256dh ) ;
57190 } ) ;
58191 } ) ;
59192} ) ;
193+
194+ Object . defineProperty ( global . navigator , 'serviceWorker' , {
195+ value : new MockServiceWorker ( ) ,
196+ writable : true ,
197+ } ) ;
0 commit comments