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