Skip to content

Commit 1eb33be

Browse files
committed
add event manager tests
1 parent 7f73788 commit 1eb33be

File tree

5 files changed

+540
-15
lines changed

5 files changed

+540
-15
lines changed

__mocks__/react-native.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import { vi } from 'vitest';
22

3+
export const createEmitterSubscriptionMock = (
4+
eventName: string,
5+
callback: (payload: unknown) => void,
6+
) => ({
7+
remove: vi.fn(),
8+
emitter: {
9+
addListener: vi.fn(),
10+
removeAllListeners: vi.fn(),
11+
listenerCount: vi.fn(() => 1),
12+
emit: vi.fn(),
13+
},
14+
listener: () => callback,
15+
context: undefined,
16+
eventType: eventName,
17+
key: 0,
18+
subscriber: {
19+
addSubscription: vi.fn(),
20+
removeSubscription: vi.fn(),
21+
removeAllSubscriptions: vi.fn(),
22+
getSubscriptionsForType: vi.fn(),
23+
},
24+
});
25+
326
const mockRNOneSignal = {
427
initialize: vi.fn(),
528
login: vi.fn(),
@@ -79,10 +102,8 @@ export { mockPlatform, mockRNOneSignal };
79102
export class NativeEventEmitter {
80103
constructor(_nativeModule: typeof mockRNOneSignal) {}
81104

82-
addListener(_eventName: string, _callback: (payload: unknown) => void) {
83-
return {
84-
remove: vi.fn(),
85-
};
105+
addListener(eventName: string, callback: (payload: unknown) => void) {
106+
return createEmitterSubscriptionMock(eventName, callback);
86107
}
87108

88109
removeListener(_eventName: string, _callback: (payload: unknown) => void) {

src/OSNotification.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ interface iOSNotificationData extends BaseNotificationData {
4242
interruptionLevel?: string;
4343
}
4444

45+
export type OSNotificationData = AndroidNotificationData | iOSNotificationData;
46+
4547
export default class OSNotification {
4648
body: string;
4749
sound?: string;
@@ -78,7 +80,7 @@ export default class OSNotification {
7880
relevanceScore?: number;
7981
interruptionLevel?: string;
8082

81-
constructor(receivedEvent: AndroidNotificationData | iOSNotificationData) {
83+
constructor(receivedEvent: OSNotificationData) {
8284
this.body = receivedEvent.body;
8385
this.sound = receivedEvent.sound;
8486
this.title = receivedEvent.title;

0 commit comments

Comments
 (0)