1- import { type DOMWindow , JSDOM , ResourceLoader } from 'jsdom' ;
21import CoreModule from 'src/core/CoreModule' ;
32import { SubscriptionModel } from 'src/core/models/SubscriptionModel' ;
43import { ModelChangeTags } from 'src/core/types/models' ;
5- import { db } from 'src/shared/database/client' ;
64import { setPushToken } from 'src/shared/database/subscription' ;
7- import {
8- NotificationType ,
9- SubscriptionType ,
10- } from 'src/shared/subscriptions/constants' ;
5+ import { SubscriptionType } from 'src/shared/subscriptions/constants' ;
116import { CoreModuleDirector } from '../../../src/core/CoreModuleDirector' ;
127import NotificationsNamespace from '../../../src/onesignal/NotificationsNamespace' ;
138import OneSignal from '../../../src/onesignal/OneSignal' ;
149import { ONESIGNAL_EVENTS } from '../../../src/onesignal/OneSignalEvents' ;
1510import UserNamespace from '../../../src/onesignal/UserNamespace' ;
1611import Context from '../../../src/page/models/Context' ;
17- import { getSlidedownElement } from '../../../src/page/slidedown/SlidedownElement' ;
1812import Emitter from '../../../src/shared/libraries/Emitter' ;
19- import { CUSTOM_LINK_CSS_CLASSES } from '../../../src/shared/slidedown/constants' ;
20- import {
21- DEFAULT_USER_AGENT ,
22- DEVICE_OS ,
23- ONESIGNAL_ID ,
24- SUB_ID_3 ,
25- } from '../../constants' ;
13+ import { BASE_SUB , ONESIGNAL_ID , SUB_ID_3 } from '../../constants' ;
2614import MockNotification from '../mocks/MockNotification' ;
2715import TestContext from './TestContext' ;
2816import { type TestEnvironmentConfig } from './TestEnvironment' ;
2917
3018declare const global : any ;
3119
32- export async function initOSGlobals ( config : TestEnvironmentConfig = { } ) {
20+ export function initOSGlobals ( config : TestEnvironmentConfig = { } ) {
3321 global . OneSignal = OneSignal ;
3422 global . OneSignal . EVENTS = ONESIGNAL_EVENTS ;
3523 global . OneSignal . config = TestContext . getFakeMergedConfig ( config ) ;
@@ -53,60 +41,6 @@ export function stubNotification(config: TestEnvironmentConfig) {
5341 global . Notification . permission = config . permission
5442 ? config . permission
5543 : global . Notification . permission ;
56-
57- // window is only defined in dom environment, not in SW
58- if ( config . environment === 'dom' ) {
59- global . window . Notification = global . Notification ;
60- }
61- }
62-
63- export async function stubDomEnvironment ( config : TestEnvironmentConfig ) {
64- if ( ! config ) {
65- config = { } ;
66- }
67-
68- let url = 'https://localhost:3001/webpush/sandbox?https=1' ;
69-
70- if ( config . url ) {
71- url = config . url . toString ( ) ;
72- global . location = url ;
73- }
74-
75- let html = '<!doctype html><html><head></head><body></body></html>' ;
76-
77- if ( config . addPrompts ) {
78- html = `<!doctype html><html><head>\
79- <div class="${ CUSTOM_LINK_CSS_CLASSES . containerClass } "></div>\
80- <div class="${ CUSTOM_LINK_CSS_CLASSES . containerClass } "></div>\
81- </head><body>\
82- ${ getSlidedownElement ( { } ) . outerHTML } </div></body></html>` ;
83- }
84-
85- const resourceLoader = new ResourceLoader ( {
86- userAgent : config . userAgent
87- ? config . userAgent . toString ( )
88- : DEFAULT_USER_AGENT . toString ( ) ,
89- } ) ;
90-
91- // global document object must be defined for `getSlidedownElement` to work correctly.
92- // this line initializes the document object
93- const dom = new JSDOM ( html , {
94- resources : resourceLoader ,
95- url : url ,
96- contentType : 'text/html' ,
97- runScripts : 'dangerously' ,
98- pretendToBeVisual : true ,
99- } ) ;
100-
101- const windowDef = dom . window ;
102- ( windowDef as any ) . location = url ;
103-
104- const windowTop : DOMWindow = windowDef ;
105- dom . reconfigure ( { url, windowTop } ) ;
106- global . window = windowDef ;
107- global . window . isSecureContext = true ;
108- global . document = windowDef . document ;
109- return dom ;
11044}
11145
11246export const createPushSub = ( {
@@ -120,14 +54,10 @@ export const createPushSub = ({
12054} = { } ) => {
12155 const pushSubscription = new SubscriptionModel ( ) ;
12256 pushSubscription . initializeFromJson ( {
123- device_model : '' ,
124- device_os : DEVICE_OS ,
125- enabled : true ,
57+ ...BASE_SUB ,
12658 id,
127- notification_types : NotificationType . Subscribed ,
12859 onesignalId,
12960 token,
130- sdk : __VERSION__ ,
13161 type : SubscriptionType . ChromePush ,
13262 } ) ;
13363 return pushSubscription ;
@@ -160,15 +90,8 @@ export const setupSubModelStore = async ({
16090 await setPushToken ( pushModel . token ) ;
16191 OneSignal . coreDirector . subscriptionModelStore . replaceAll (
16292 [ pushModel ] ,
163- ModelChangeTags . NO_PROPOGATE ,
93+ ModelChangeTags . NO_PROPAGATE ,
16494 ) ;
16595
166- await vi . waitUntil ( async ( ) => {
167- const subscription = ( await db . getAll ( 'subscriptions' ) ) [ 0 ] ;
168- return (
169- subscription . id === pushModel . id && subscription . token === pushModel . token
170- ) ;
171- } ) ;
172-
17396 return pushModel ;
17497} ;
0 commit comments