-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathNotificationWillDisplayEvent.test.ts
More file actions
107 lines (91 loc) · 4.12 KB
/
NotificationWillDisplayEvent.test.ts
File metadata and controls
107 lines (91 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { NativeModules } from 'react-native';
import OSNotification, { type BaseNotificationData } from '../OSNotification';
import NotificationWillDisplayEvent from './NotificationWillDisplayEvent';
const mockRNOneSignal = NativeModules.OneSignal;
describe('NotificationWillDisplayEvent', () => {
const notificationId = 'test-notification-id';
const baseNotificationData: BaseNotificationData = {
body: 'Test notification body',
sound: 'default',
title: 'Test Title',
launchURL: 'https://example.com',
rawPayload: { key: 'value' },
actionButtons: [{ id: 'btn1', text: 'Button 1' }],
additionalData: { custom: 'data' },
notificationId,
};
describe('constructor', () => {
test('should initialize with OSNotification instance', () => {
const notification = new OSNotification(baseNotificationData);
const event = new NotificationWillDisplayEvent(notification);
expect(event.notification).toBeInstanceOf(OSNotification);
expect(event.notification.notificationId).toBe(notificationId);
expect(event.notification.body).toBe('Test notification body');
expect(event.notification.title).toBe('Test Title');
});
test('should create a new OSNotification instance from the provided notification', () => {
const notification = new OSNotification(baseNotificationData);
const event = new NotificationWillDisplayEvent(notification);
expect(event.notification).not.toBe(notification);
expect(event.notification.notificationId).toBe(notificationId);
expect(event.notification.body).toBe(notification.body);
});
test('should initialize with notification containing all optional fields', () => {
const fullData = {
...baseNotificationData,
sound: 'custom-sound',
launchURL: 'https://example.com/launch',
actionButtons: [
{ id: 'btn1', text: 'Button 1' },
{ id: 'btn2', text: 'Button 2' },
],
additionalData: { key1: 'value1', key2: 'value2' },
};
const notification = new OSNotification(fullData);
const event = new NotificationWillDisplayEvent(notification);
expect(event.notification.sound).toBe('custom-sound');
expect(event.notification.launchURL).toBe('https://example.com/launch');
expect(event.notification.actionButtons).toHaveLength(2);
expect(event.notification.additionalData).toEqual({
key1: 'value1',
key2: 'value2',
});
});
});
describe('preventDefault', () => {
test('should call native preventDefault with notificationId', () => {
const notification = new OSNotification(baseNotificationData);
const event = new NotificationWillDisplayEvent(notification);
const result = event.preventDefault();
expect(mockRNOneSignal.preventDefault).toHaveBeenCalledWith(
notificationId,
);
expect(result).toBeUndefined();
});
test('should allow multiple calls to preventDefault', () => {
const notification = new OSNotification(baseNotificationData);
const event = new NotificationWillDisplayEvent(notification);
event.preventDefault();
event.preventDefault();
event.preventDefault();
expect(mockRNOneSignal.preventDefault).toHaveBeenCalledTimes(3);
expect(mockRNOneSignal.preventDefault).toHaveBeenCalledWith(
'test-notification-id',
);
});
});
describe('getNotification', () => {
test('should return the notification instance', () => {
const notification = new OSNotification(baseNotificationData);
const event = new NotificationWillDisplayEvent(notification);
const returnedNotification = event.getNotification();
expect(returnedNotification).toBe(event.notification);
expect(returnedNotification).toBeInstanceOf(OSNotification);
expect(returnedNotification.notificationId).toBe('test-notification-id');
expect(returnedNotification.body).toBe('Test notification body');
expect(returnedNotification.title).toBe('Test Title');
expect(returnedNotification.sound).toBe('default');
expect(returnedNotification.rawPayload).toEqual({ key: 'value' });
});
});
});