Skip to content

Commit f8314f1

Browse files
authored
test: add tests for onesignal module (#1855)
1 parent 15c3946 commit f8314f1

File tree

16 files changed

+1717
-40
lines changed

16 files changed

+1717
-40
lines changed

.oxlintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"categories": {},
55
"rules": {
66
"for-direction": "warn",
7+
"jest/no-focused-tests": "error",
78
"no-async-promise-executor": "warn",
89
"no-caller": "warn",
910
"no-class-assign": "warn",

__mocks__/react-native.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { vi } from 'vitest';
2+
3+
const mockRNOneSignal = {
4+
initialize: vi.fn(),
5+
login: vi.fn(),
6+
logout: vi.fn(),
7+
setPrivacyConsentRequired: vi.fn(),
8+
setPrivacyConsentGiven: vi.fn(),
9+
setLogLevel: vi.fn(),
10+
setAlertLevel: vi.fn(),
11+
enterLiveActivity: vi.fn(),
12+
exitLiveActivity: vi.fn(),
13+
setPushToStartToken: vi.fn(),
14+
removePushToStartToken: vi.fn(),
15+
setupDefaultLiveActivity: vi.fn(),
16+
startDefaultLiveActivity: vi.fn(),
17+
addPushSubscriptionObserver: vi.fn(),
18+
getPushSubscriptionId: vi.fn(),
19+
getPushSubscriptionToken: vi.fn(),
20+
getOptedIn: vi.fn(),
21+
optOut: vi.fn(),
22+
optIn: vi.fn(),
23+
addUserStateObserver: vi.fn(),
24+
getOnesignalId: vi.fn(),
25+
getExternalId: vi.fn(),
26+
setLanguage: vi.fn(),
27+
addAlias: vi.fn(),
28+
addAliases: vi.fn(),
29+
removeAlias: vi.fn(),
30+
removeAliases: vi.fn(),
31+
addEmail: vi.fn(),
32+
removeEmail: vi.fn(),
33+
addSms: vi.fn(),
34+
removeSms: vi.fn(),
35+
addTag: vi.fn(),
36+
addTags: vi.fn(),
37+
removeTags: vi.fn(),
38+
getTags: vi.fn(),
39+
hasNotificationPermission: vi.fn(),
40+
requestNotificationPermission: vi.fn(),
41+
canRequestNotificationPermission: vi.fn(),
42+
registerForProvisionalAuthorization: vi.fn(),
43+
permissionNative: vi.fn(),
44+
addNotificationClickListener: vi.fn(),
45+
addNotificationForegroundLifecycleListener: vi.fn(),
46+
addPermissionObserver: vi.fn(),
47+
clearAllNotifications: vi.fn(),
48+
removeNotification: vi.fn(),
49+
removeGroupedNotifications: vi.fn(),
50+
addInAppMessageClickListener: vi.fn(),
51+
addInAppMessagesLifecycleListener: vi.fn(),
52+
addTriggers: vi.fn(),
53+
removeTrigger: vi.fn(),
54+
removeTriggers: vi.fn(),
55+
clearTriggers: vi.fn(),
56+
paused: vi.fn(),
57+
getPaused: vi.fn(),
58+
requestLocationPermission: vi.fn(),
59+
setLocationShared: vi.fn(),
60+
isLocationShared: vi.fn(),
61+
addOutcome: vi.fn(),
62+
addUniqueOutcome: vi.fn(),
63+
addOutcomeWithValue: vi.fn(),
64+
};
65+
66+
const mockPlatform = {
67+
OS: 'ios',
68+
};
69+
70+
export const NativeModules = {
71+
OneSignal: mockRNOneSignal,
72+
};
73+
74+
export const Platform = mockPlatform;
75+
76+
export { mockPlatform, mockRNOneSignal };
77+
78+
export class NativeEventEmitter {
79+
constructor(_nativeModule: typeof mockRNOneSignal) {}
80+
81+
addListener(_eventName: string, _callback: (payload: unknown) => void) {
82+
return {
83+
remove: vi.fn(),
84+
};
85+
}
86+
87+
removeListener(_eventName: string, _callback: (payload: unknown) => void) {
88+
// Mock implementation
89+
}
90+
91+
removeAllListeners(_eventName: string) {
92+
// Mock implementation
93+
}
94+
}

bun.lock

Lines changed: 79 additions & 5 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
"prepare": "bun run build",
99
"build": "tsc --noEmit && vite build",
1010
"lint": "oxlint src examples && prettier --check src examples",
11-
"lint:fix": "oxlint src examples --fix && prettier --write src examples"
11+
"lint:fix": "oxlint src examples --fix && prettier --write src examples",
12+
"test": "vitest",
13+
"test:coverage": "vitest run --coverage"
1214
},
1315
"dependencies": {
1416
"invariant": "^2.2.4"
1517
},
1618
"devDependencies": {
1719
"@types/invariant": "^2.2.37",
1820
"@types/react-native": "^0.73.0",
21+
"@vitest/coverage-v8": "4.0.8",
1922
"oxlint": "^1.26.0",
2023
"prettier": "^3.6.2",
2124
"typescript": "^5.9.3",
2225
"vite": "^7.2.0",
23-
"vite-plugin-dts": "^4.5.4"
26+
"vite-plugin-dts": "^4.5.4",
27+
"vitest": "^4.0.8"
2428
},
2529
"keywords": [
2630
"react-component",
File renamed without changes.

src/constants/subscription.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export enum OSNotificationPermission {
2+
NotDetermined = 0,
3+
Denied,
4+
Authorized,
5+
Provisional, // only available in iOS 12
6+
Ephemeral, // only available in iOS 14
7+
}

src/events/EventManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import {
33
NativeEventEmitter,
44
type NativeModule,
55
} from 'react-native';
6-
import OSNotification from '../OSNotification';
7-
import NotificationWillDisplayEvent from './NotificationWillDisplayEvent';
86
import {
97
IN_APP_MESSAGE_CLICKED,
108
IN_APP_MESSAGE_DID_DISMISS,
@@ -16,7 +14,9 @@ import {
1614
PERMISSION_CHANGED,
1715
SUBSCRIPTION_CHANGED,
1816
USER_STATE_CHANGED,
19-
} from './events';
17+
} from '../constants/events';
18+
import OSNotification from '../OSNotification';
19+
import NotificationWillDisplayEvent from './NotificationWillDisplayEvent';
2020

2121
const eventList = [
2222
PERMISSION_CHANGED,

0 commit comments

Comments
 (0)