Skip to content

Commit f041aa1

Browse files
committed
remove stub dom environment
1 parent 1b716f2 commit f041aa1

18 files changed

Lines changed: 376 additions & 176 deletions

__test__/setupTests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import intlTelInput from 'intl-tel-input';
2+
import OneSignalApi from 'src/shared/api/OneSignalApi';
3+
import { ConfigIntegrationKind } from 'src/shared/config/constants';
14
import { clearAll } from 'src/shared/database/client';
25
import { DEFAULT_USER_AGENT } from './constants';
6+
import TestContext from './support/environment/TestContext';
37
import { server } from './support/mocks/server';
48

9+
window.intlTelInput = intlTelInput;
10+
511
beforeAll(() =>
612
server.listen({
713
onUnhandledRequest: 'warn',
@@ -44,3 +50,12 @@ Object.defineProperty(navigator, 'userAgent', {
4450
value: DEFAULT_USER_AGENT,
4551
writable: true,
4652
});
53+
54+
export const mockJsonp = () => {
55+
const serverConfig = TestContext.getFakeServerAppConfig(
56+
ConfigIntegrationKind.Custom,
57+
);
58+
vi.spyOn(OneSignalApi, 'jsonpLib').mockImplementation((_, fn) => {
59+
fn(null, serverConfig);
60+
});
61+
};

__test__/support/environment/TestEnvironment.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { ONESIGNAL_ID } from '__test__/constants';
2+
import { mockJsonp } from '__test__/setupTests';
23
import type {
34
AppUserConfig,
45
ConfigIntegrationKindValue,
56
ServerAppConfig,
67
} from 'src/shared/config/types';
78
import type { RecursivePartial } from 'src/shared/context/types';
89
import { updateIdentityModel } from '../helpers/setup';
9-
import {
10-
initOSGlobals,
11-
stubDomEnvironment,
12-
stubNotification,
13-
} from './TestEnvironmentHelpers';
10+
import { initOSGlobals, stubNotification } from './TestEnvironmentHelpers';
1411

1512
export interface TestEnvironmentConfig {
1613
userConfig?: AppUserConfig;
@@ -24,15 +21,31 @@ export interface TestEnvironmentConfig {
2421
overrideServerConfig?: RecursivePartial<ServerAppConfig>;
2522
integration?: ConfigIntegrationKindValue;
2623
}
24+
Object.defineProperty(document, 'readyState', {
25+
value: 'complete',
26+
writable: true,
27+
});
2728

2829
export class TestEnvironment {
2930
static async initialize(config: TestEnvironmentConfig = {}) {
31+
mockJsonp();
3032
const oneSignal = await initOSGlobals(config);
3133
OneSignal.coreDirector.operationRepo.queue = [];
3234

3335
updateIdentityModel('onesignal_id', ONESIGNAL_ID);
3436

35-
await stubDomEnvironment(config);
37+
// Set URL if provided
38+
if (config.url) {
39+
Object.defineProperty(window, 'location', {
40+
value: new URL(config.url),
41+
writable: true,
42+
});
43+
}
44+
45+
OneSignal._didLoadITILibrary = true;
46+
window.isSecureContext = true;
47+
48+
// await stubDomEnvironment(config);
3649
config.environment = 'dom';
3750
stubNotification(config);
3851
return oneSignal;

__test__/support/environment/TestEnvironmentHelpers.ts

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { type DOMWindow, JSDOM, ResourceLoader } from 'jsdom';
21
import CoreModule from 'src/core/CoreModule';
32
import { SubscriptionModel } from 'src/core/models/SubscriptionModel';
43
import { ModelChangeTags } from 'src/core/types/models';
@@ -14,15 +13,8 @@ import OneSignal from '../../../src/onesignal/OneSignal';
1413
import { ONESIGNAL_EVENTS } from '../../../src/onesignal/OneSignalEvents';
1514
import UserNamespace from '../../../src/onesignal/UserNamespace';
1615
import Context from '../../../src/page/models/Context';
17-
import { getSlidedownElement } from '../../../src/page/slidedown/SlidedownElement';
1816
import 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';
17+
import { DEVICE_OS, ONESIGNAL_ID, SUB_ID_3 } from '../../constants';
2618
import MockNotification from '../mocks/MockNotification';
2719
import TestContext from './TestContext';
2820
import { type TestEnvironmentConfig } from './TestEnvironment';
@@ -60,55 +52,6 @@ export function stubNotification(config: TestEnvironmentConfig) {
6052
}
6153
}
6254

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;
110-
}
111-
11255
export const createPushSub = ({
11356
id = SUB_ID_3,
11457
token = 'push-token',

__test__/support/helpers/requests.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
import { http, HttpResponse } from 'msw';
22
import type { ISubscription, IUserProperties } from 'src/core/types/api';
3-
import { ConfigIntegrationKind } from 'src/shared/config/constants';
3+
import type { NotificationIcons } from 'src/shared/notifications/types';
44
import { APP_ID, ONESIGNAL_ID, SUB_ID } from '../../constants';
5-
import TestContext from '../environment/TestContext';
65
import { server } from '../mocks/server';
76

87
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98
// configs
10-
const serverConfig = TestContext.getFakeServerAppConfig(
11-
ConfigIntegrationKind.Custom,
12-
);
13-
14-
export const mockServerConfig = () => {
15-
return http.get('**/sync/*/web', ({ request }) => {
16-
const url = new URL(request.url);
17-
const callbackParam = url.searchParams.get('callback');
18-
return new HttpResponse(
19-
`${callbackParam}(${JSON.stringify(serverConfig)})`,
20-
{
21-
headers: {
22-
'Content-Type': 'application/javascript',
23-
},
24-
},
25-
);
26-
});
27-
};
289
export const mockPageStylesCss = () => {
2910
return http.get(
3011
'https://onesignal.com/sdks/web/v16/OneSignalSDK.page.styles.css',
@@ -397,3 +378,16 @@ export const setSendCustomEventResponse = () =>
397378
status: 200,
398379
callback: sendCustomEventFn,
399380
});
381+
382+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
383+
// icons
384+
export const getNotificationIcons = () =>
385+
server.use(
386+
http.get('**/apps/:appId/icon', () => {
387+
return HttpResponse.json({
388+
chrome: 'https://onesignal.com/icon.png',
389+
firefox: 'https://onesignal.com/icon.png',
390+
safari: 'https://onesignal.com/icon.png',
391+
} satisfies NotificationIcons);
392+
}),
393+
);

__test__/support/helpers/setup.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { db } from 'src/shared/database/client';
77
import type {
88
IdentitySchema,
99
PropertiesSchema,
10+
SubscriptionSchema,
1011
} from 'src/shared/database/types';
1112

1213
export const setIsPushEnabled = async (isPushEnabled: boolean) => {
@@ -35,6 +36,15 @@ export const getPropertiesItem = async (
3536
return properties;
3637
};
3738

39+
export const getDbSubscriptions = async (length: number) => {
40+
let subscriptions: SubscriptionSchema[] = [];
41+
await vi.waitUntil(async () => {
42+
subscriptions = await db.getAll('subscriptions');
43+
return subscriptions.length === length;
44+
});
45+
return subscriptions;
46+
};
47+
3848
export const setupIdentityModel = async (
3949
{
4050
onesignalID,

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
// uncomment to test login
5050
// OneSignalDeferred.push(async function (OneSignal) {
5151
// await OneSignal.login('new-web-sdk');
52+
// OneSignal.User.addEmail('test@test.com');
5253
// });
5354
</script>
5455
</head>

0 commit comments

Comments
 (0)