Skip to content

Commit 33010c7

Browse files
authored
Merge pull request #165 from OneSignal/cd_update
sync with web-shim-codegen v3.0.4
2 parents 8660fb0 + f951175 commit 33010c7

File tree

8 files changed

+1178
-252
lines changed

8 files changed

+1178
-252
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
example/

.eslintrc.cjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
root: true,
23
env: {
34
browser: true,
45
es6: true,
@@ -12,7 +13,10 @@ module.exports = {
1213
},
1314
},
1415
},
15-
extends: ['plugin:react/recommended'],
16+
extends: [
17+
'plugin:react/recommended',
18+
'plugin:@typescript-eslint/recommended',
19+
],
1620
parser: '@typescript-eslint/parser',
1721
parserOptions: {
1822
ecmaFeatures: {
@@ -25,13 +29,14 @@ module.exports = {
2529
rules: {
2630
'react/jsx-filename-extension': [1, { extensions: ['.tsx', '.jsx'] }],
2731
'react/jsx-props-no-spreading': 0,
32+
'@typescript-eslint/no-unused-vars': ['error'],
33+
'@typescript-eslint/no-explicit-any': 'warn',
2834
'prefer-destructuring': 0,
2935
'no-param-reassign': 0,
3036
'import/extensions': 0,
3137
'dot-notation': 0,
3238
'no-continue': 0,
3339
'no-unused-vars': 'off',
34-
'@typescript-eslint/no-unused-vars': ['error'],
3540
'no-unused-expressions': [
3641
'error',
3742
{ allowShortCircuit: true, allowTernary: true },

index.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import OneSignal from './index';
2+
3+
const originalDocument = global.document;
4+
const documentSpy = vi.spyOn(global, 'document', 'get');
5+
6+
const APP_ID = '123456';
7+
8+
const init = vi.fn();
9+
// @ts-expect-error - mocking OneSignal class that comes from the cdn
10+
window.OneSignal = {
11+
init,
12+
};
13+
window.OneSignalDeferred = [];
14+
Object.defineProperty(window.OneSignalDeferred, 'push', {
15+
value: (cb: (OneSignal: typeof window.OneSignal) => void) => {
16+
cb(window.OneSignal);
17+
},
18+
});
19+
20+
describe('React OneSignal', () => {
21+
test('init method', async () => {
22+
// no document error
23+
documentSpy.mockReturnValue(undefined);
24+
await expect(OneSignal.init({ appId: APP_ID })).rejects.toThrow(
25+
'Document is not defined.',
26+
);
27+
documentSpy.mockImplementation(() => originalDocument);
28+
29+
// no appId error
30+
// @ts-expect-error - appId is required but purposely not provided for this test
31+
await expect(OneSignal.init({})).rejects.toThrow(
32+
'You need to provide your OneSignal appId.',
33+
);
34+
35+
// init error
36+
init.mockRejectedValue(new Error('init error'));
37+
await expect(OneSignal.init({ appId: APP_ID })).rejects.toThrow(
38+
'init error',
39+
);
40+
41+
// init success
42+
init.mockResolvedValue(undefined);
43+
await expect(OneSignal.init({ appId: APP_ID })).resolves.not.toThrow();
44+
45+
// already initialized error
46+
await expect(OneSignal.init({ appId: APP_ID })).rejects.toThrow(
47+
'OneSignal is already initialized.',
48+
);
49+
});
50+
});

index.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const ONESIGNAL_SDK_ID = 'onesignal-sdk';
2-
const ONE_SIGNAL_SCRIPT_SRC = "https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js";
2+
const ONE_SIGNAL_SCRIPT_SRC =
3+
'https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js';
34

45
// true if the script is successfully loaded from CDN.
56
let isOneSignalInitialized = false;
@@ -39,7 +40,7 @@ function addSDKScript() {
3940
// This is important for users who may block cdn.onesignal.com w/ adblock.
4041
script.onerror = () => {
4142
handleOnError();
42-
}
43+
};
4344

4445
document.head.appendChild(script);
4546
}
@@ -57,20 +58,26 @@ function isPushNotificationsSupported() {
5758

5859
function isMacOSSafariInIframe(): boolean {
5960
// Fallback detection for Safari on macOS in an iframe context
60-
return window.top !== window && // isContextIframe
61-
navigator.vendor === "Apple Computer, Inc." && // isSafari
62-
navigator.platform === "MacIntel"; // isMacOS
61+
return (
62+
window.top !== window && // isContextIframe
63+
navigator.vendor === 'Apple Computer, Inc.' && // isSafari
64+
navigator.platform === 'MacIntel'
65+
); // isMacOS
6366
}
6467

6568
function supportsSafariPush(): boolean {
66-
return (window.safari && typeof window.safari.pushNotification !== "undefined") ||
67-
isMacOSSafariInIframe();
69+
return (
70+
(window.safari && typeof window.safari.pushNotification !== 'undefined') ||
71+
isMacOSSafariInIframe()
72+
);
6873
}
6974

7075
// Does the browser support the standard Push API
7176
function supportsVapidPush(): boolean {
72-
return typeof PushSubscriptionOptions !== "undefined" &&
73-
PushSubscriptionOptions.prototype.hasOwnProperty("applicationServerKey");
77+
return (
78+
typeof PushSubscriptionOptions !== 'undefined' &&
79+
PushSubscriptionOptions.prototype.hasOwnProperty('applicationServerKey')
80+
);
7481
}
7582
/* E N D */
7683

@@ -82,7 +89,7 @@ function supportsVapidPush(): boolean {
8289
*/
8390
const isPushSupported = (): boolean => {
8491
return isPushNotificationsSupported();
85-
}
92+
};
8693

8794
/**
8895
* @PublicApi
@@ -93,19 +100,21 @@ const init = (options: IInitObject): Promise<void> => {
93100
}
94101

95102
if (!options || !options.appId) {
96-
throw new Error('You need to provide your OneSignal appId.');
103+
return Promise.reject('You need to provide your OneSignal appId.');
97104
}
98105

99106
if (!document) {
100107
return Promise.reject(`Document is not defined.`);
101108
}
102109

103-
return new Promise<void>((resolve) => {
110+
return new Promise<void>((resolve, reject) => {
104111
window.OneSignalDeferred?.push((OneSignal) => {
105-
OneSignal.init(options).then(() => {
106-
isOneSignalInitialized = true;
107-
resolve();
108-
});
112+
OneSignal.init(options)
113+
.then(() => {
114+
isOneSignalInitialized = true;
115+
resolve();
116+
})
117+
.catch(reject);
109118
});
110119
});
111120
};
@@ -117,7 +126,7 @@ export interface IOneSignalTagCategory { tag: string; label: string; checked?: b
117126
export type PushSubscriptionNamespaceProperties = { id: string | null | undefined; token: string | null | undefined; optedIn: boolean; };
118127
export type SubscriptionChangeEvent = { previous: PushSubscriptionNamespaceProperties; current: PushSubscriptionNamespaceProperties; };
119128
export type NotificationEventName = 'click' | 'foregroundWillDisplay' | 'dismiss' | 'permissionChange' | 'permissionPromptDisplay';
120-
export type SlidedownEventName = 'slidedownShown';
129+
export type SlidedownEventName = 'slidedownAllowClick' | 'slidedownCancelClick' | 'slidedownClosed' | 'slidedownQueued' | 'slidedownShown';
121130
export type OneSignalDeferredLoadedCallback = (onesignal: IOneSignalOneSignal) => void;
122131
export interface IOSNotification {
123132
/**

0 commit comments

Comments
 (0)