Skip to content

Commit 7f73788

Browse files
committed
improve test coverage
1 parent 83c5b92 commit 7f73788

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/index.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ const removeEventManagerListenerSpy = vi.spyOn(
3838
'removeEventListener',
3939
);
4040

41+
const filterEventListener = (eventName: string) => {
42+
return addEventManagerListenerSpy.mock.calls.filter(
43+
(call) => call[0] === eventName,
44+
)[0][1];
45+
};
46+
4147
describe('OneSignal', () => {
4248
beforeEach(() => {
4349
mockPlatform.OS = 'ios';
@@ -64,6 +70,28 @@ describe('OneSignal', () => {
6470
test('should initialize OneSignal with appId', () => {
6571
OneSignal.initialize(APP_ID);
6672
expect(mockRNOneSignal.initialize).toHaveBeenCalledWith(APP_ID);
73+
74+
// test permission change listener
75+
const changeFn = filterEventListener(PERMISSION_CHANGED);
76+
changeFn(true);
77+
const permission = OneSignal.Notifications.hasPermission();
78+
expect(permission).toBe(true);
79+
80+
// test push subscription change listener
81+
const subscriptionChangeFn = filterEventListener(SUBSCRIPTION_CHANGED);
82+
subscriptionChangeFn({
83+
current: {
84+
id: 'subscription-id',
85+
token: 'push-token',
86+
optedIn: true,
87+
},
88+
});
89+
const pushSubscription =
90+
OneSignal.User.pushSubscription.getPushSubscriptionId();
91+
expect(pushSubscription).toBe('subscription-id');
92+
93+
// reset push subscription
94+
subscriptionChangeFn({ current: {} });
6795
});
6896

6997
test('should not initialize if native module is not loaded', () => {

src/index.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,10 @@ export namespace OneSignal {
165165
export function enter(
166166
activityId: string,
167167
token: string,
168-
handler?: Function,
168+
handler: Function = () => {},
169169
) {
170170
if (!isNativeModuleLoaded(RNOneSignal)) return;
171171

172-
if (!handler) {
173-
handler = () => {};
174-
}
175-
176172
// Only Available on iOS
177173
if (Platform.OS === 'ios') {
178174
RNOneSignal.enterLiveActivity(activityId, token, handler);
@@ -186,13 +182,9 @@ export namespace OneSignal {
186182
*
187183
* @param activityId: The activity identifier the live activity on this device will no longer receive updates for.
188184
**/
189-
export function exit(activityId: string, handler?: Function) {
185+
export function exit(activityId: string, handler: Function = () => {}) {
190186
if (!isNativeModuleLoaded(RNOneSignal)) return;
191187

192-
if (!handler) {
193-
handler = () => {};
194-
}
195-
196188
if (Platform.OS === 'ios') {
197189
RNOneSignal.exitLiveActivity(activityId, handler);
198190
}
@@ -697,6 +689,7 @@ export namespace OneSignal {
697689
if (!isNativeModuleLoaded(RNOneSignal)) return;
698690
isValidCallback(listener);
699691

692+
/* v8 ignore else -- @preserve */
700693
if (event === 'click') {
701694
RNOneSignal.addNotificationClickListener();
702695
eventManager.addEventListener<NotificationClickEvent>(
@@ -725,14 +718,13 @@ export namespace OneSignal {
725718
event: K,
726719
listener: (event: NotificationEventTypeMap[K]) => void,
727720
): void {
721+
/* v8 ignore else -- @preserve */
728722
if (event === 'click') {
729723
eventManager.removeEventListener(NOTIFICATION_CLICKED, listener);
730724
} else if (event === 'foregroundWillDisplay') {
731725
eventManager.removeEventListener(NOTIFICATION_WILL_DISPLAY, listener);
732726
} else if (event === 'permissionChange') {
733727
eventManager.removeEventListener(PERMISSION_CHANGED, listener);
734-
} else {
735-
return;
736728
}
737729
}
738730

@@ -799,6 +791,7 @@ export namespace OneSignal {
799791
listener as (event: InAppMessageClickEvent) => void,
800792
);
801793
} else {
794+
/* v8 ignore else -- @preserve */
802795
if (event === 'willDisplay') {
803796
isValidCallback(listener);
804797
eventManager.addEventListener<InAppMessageWillDisplayEvent>(
@@ -840,6 +833,7 @@ export namespace OneSignal {
840833
if (event === 'click') {
841834
eventManager.removeEventListener(IN_APP_MESSAGE_CLICKED, listener);
842835
} else {
836+
/* v8 ignore else -- @preserve */
843837
if (event === 'willDisplay') {
844838
eventManager.removeEventListener(
845839
IN_APP_MESSAGE_WILL_DISPLAY,
@@ -860,8 +854,6 @@ export namespace OneSignal {
860854
IN_APP_MESSAGE_DID_DISMISS,
861855
listener,
862856
);
863-
} else {
864-
return;
865857
}
866858
}
867859
}

0 commit comments

Comments
 (0)