|
| 1 | +import { NativeModules, Platform } from 'react-native'; |
| 2 | +import OSNotification, { type BaseNotificationData } from './OSNotification'; |
| 3 | + |
| 4 | +const mockRNOneSignal = NativeModules.OneSignal; |
| 5 | +const mockPlatform = Platform; |
| 6 | + |
| 7 | +describe('OSNotification', () => { |
| 8 | + const baseNotificationData: BaseNotificationData = { |
| 9 | + body: 'Test notification body', |
| 10 | + sound: 'default', |
| 11 | + title: 'Test Title', |
| 12 | + launchURL: 'https://example.com', |
| 13 | + rawPayload: { key: 'value' }, |
| 14 | + actionButtons: [{ id: 'btn1', text: 'Button 1' }], |
| 15 | + additionalData: { custom: 'data' }, |
| 16 | + notificationId: 'test-notification-id', |
| 17 | + }; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + mockPlatform.OS = 'ios'; |
| 21 | + mockRNOneSignal.displayNotification.mockClear(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('constructor', () => { |
| 25 | + test('should initialize common properties', () => { |
| 26 | + const notification = new OSNotification(baseNotificationData); |
| 27 | + |
| 28 | + expect(notification.body).toBe('Test notification body'); |
| 29 | + expect(notification.sound).toBe('default'); |
| 30 | + expect(notification.title).toBe('Test Title'); |
| 31 | + expect(notification.launchURL).toBe('https://example.com'); |
| 32 | + expect(notification.rawPayload).toEqual({ key: 'value' }); |
| 33 | + expect(notification.actionButtons).toEqual([ |
| 34 | + { id: 'btn1', text: 'Button 1' }, |
| 35 | + ]); |
| 36 | + expect(notification.additionalData).toEqual({ custom: 'data' }); |
| 37 | + expect(notification.notificationId).toBe('test-notification-id'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should initialize with optional common properties as undefined', () => { |
| 41 | + const notificationData = { |
| 42 | + body: 'Test body', |
| 43 | + rawPayload: '', |
| 44 | + notificationId: 'id-123', |
| 45 | + }; |
| 46 | + const notification = new OSNotification(notificationData); |
| 47 | + |
| 48 | + expect(notification.body).toBe('Test body'); |
| 49 | + expect(notification.sound).toBeUndefined(); |
| 50 | + expect(notification.title).toBeUndefined(); |
| 51 | + expect(notification.launchURL).toBeUndefined(); |
| 52 | + expect(notification.actionButtons).toBeUndefined(); |
| 53 | + expect(notification.additionalData).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('Android-specific properties', () => { |
| 57 | + beforeEach(() => { |
| 58 | + mockPlatform.OS = 'android'; |
| 59 | + }); |
| 60 | + |
| 61 | + test('should initialize Android-specific properties on Android platform', () => { |
| 62 | + const androidData = { |
| 63 | + ...baseNotificationData, |
| 64 | + groupKey: 'group-1', |
| 65 | + groupMessage: 'group message', |
| 66 | + ledColor: 'FFFF0000', |
| 67 | + priority: 2, |
| 68 | + smallIcon: 'icon_small', |
| 69 | + largeIcon: 'icon_large', |
| 70 | + bigPicture: 'image_url', |
| 71 | + collapseId: 'collapse-1', |
| 72 | + fromProjectNumber: '123456789', |
| 73 | + smallIconAccentColor: 'FFFF0000', |
| 74 | + lockScreenVisibility: '1', |
| 75 | + androidNotificationId: 456, |
| 76 | + }; |
| 77 | + const notification = new OSNotification(androidData); |
| 78 | + |
| 79 | + expect(notification.groupKey).toBe('group-1'); |
| 80 | + expect(notification.groupMessage).toBe('group message'); |
| 81 | + expect(notification.ledColor).toBe('FFFF0000'); |
| 82 | + expect(notification.priority).toBe(2); |
| 83 | + expect(notification.smallIcon).toBe('icon_small'); |
| 84 | + expect(notification.largeIcon).toBe('icon_large'); |
| 85 | + expect(notification.bigPicture).toBe('image_url'); |
| 86 | + expect(notification.collapseId).toBe('collapse-1'); |
| 87 | + expect(notification.fromProjectNumber).toBe('123456789'); |
| 88 | + expect(notification.smallIconAccentColor).toBe('FFFF0000'); |
| 89 | + expect(notification.lockScreenVisibility).toBe('1'); |
| 90 | + expect(notification.androidNotificationId).toBe(456); |
| 91 | + }); |
| 92 | + |
| 93 | + test('should not set iOS-specific properties on Android', () => { |
| 94 | + const notification = new OSNotification(baseNotificationData); |
| 95 | + |
| 96 | + expect(notification.badge).toBeUndefined(); |
| 97 | + expect(notification.badgeIncrement).toBeUndefined(); |
| 98 | + expect(notification.category).toBeUndefined(); |
| 99 | + expect(notification.threadId).toBeUndefined(); |
| 100 | + expect(notification.subtitle).toBeUndefined(); |
| 101 | + expect(notification.templateId).toBeUndefined(); |
| 102 | + expect(notification.templateName).toBeUndefined(); |
| 103 | + expect(notification.attachments).toBeUndefined(); |
| 104 | + expect(notification.mutableContent).toBeUndefined(); |
| 105 | + expect(notification.contentAvailable).toBeUndefined(); |
| 106 | + expect(notification.relevanceScore).toBeUndefined(); |
| 107 | + expect(notification.interruptionLevel).toBeUndefined(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('iOS-specific properties', () => { |
| 112 | + beforeEach(() => { |
| 113 | + mockPlatform.OS = 'ios'; |
| 114 | + }); |
| 115 | + |
| 116 | + test('should initialize iOS-specific properties on iOS platform', () => { |
| 117 | + const iosData = { |
| 118 | + ...baseNotificationData, |
| 119 | + badge: '1', |
| 120 | + badgeIncrement: '5', |
| 121 | + category: 'NOTIFICATION_CATEGORY', |
| 122 | + threadId: 'thread-123', |
| 123 | + subtitle: 'Test Subtitle', |
| 124 | + templateId: 'template-1', |
| 125 | + templateName: 'template-name', |
| 126 | + attachments: { key: 'attachment-value' }, |
| 127 | + mutableContent: true, |
| 128 | + contentAvailable: '1', |
| 129 | + relevanceScore: 0.9, |
| 130 | + interruptionLevel: 'timeSensitive', |
| 131 | + }; |
| 132 | + const notification = new OSNotification(iosData); |
| 133 | + |
| 134 | + expect(notification.badge).toBe('1'); |
| 135 | + expect(notification.badgeIncrement).toBe('5'); |
| 136 | + expect(notification.category).toBe('NOTIFICATION_CATEGORY'); |
| 137 | + expect(notification.threadId).toBe('thread-123'); |
| 138 | + expect(notification.subtitle).toBe('Test Subtitle'); |
| 139 | + expect(notification.templateId).toBe('template-1'); |
| 140 | + expect(notification.templateName).toBe('template-name'); |
| 141 | + expect(notification.attachments).toEqual({ key: 'attachment-value' }); |
| 142 | + expect(notification.mutableContent).toBe(true); |
| 143 | + expect(notification.contentAvailable).toBe('1'); |
| 144 | + expect(notification.relevanceScore).toBe(0.9); |
| 145 | + expect(notification.interruptionLevel).toBe('timeSensitive'); |
| 146 | + }); |
| 147 | + |
| 148 | + test('should not set Android-specific properties on iOS', () => { |
| 149 | + const notification = new OSNotification(baseNotificationData); |
| 150 | + |
| 151 | + expect(notification.groupKey).toBeUndefined(); |
| 152 | + expect(notification.groupMessage).toBeUndefined(); |
| 153 | + expect(notification.ledColor).toBeUndefined(); |
| 154 | + expect(notification.priority).toBeUndefined(); |
| 155 | + expect(notification.smallIcon).toBeUndefined(); |
| 156 | + expect(notification.largeIcon).toBeUndefined(); |
| 157 | + expect(notification.bigPicture).toBeUndefined(); |
| 158 | + expect(notification.collapseId).toBeUndefined(); |
| 159 | + expect(notification.fromProjectNumber).toBeUndefined(); |
| 160 | + expect(notification.smallIconAccentColor).toBeUndefined(); |
| 161 | + expect(notification.lockScreenVisibility).toBeUndefined(); |
| 162 | + expect(notification.androidNotificationId).toBeUndefined(); |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('display', () => { |
| 168 | + test('should call native displayNotification with notificationId', () => { |
| 169 | + const notification = new OSNotification(baseNotificationData); |
| 170 | + notification.display(); |
| 171 | + |
| 172 | + expect(mockRNOneSignal.displayNotification).toHaveBeenCalledWith( |
| 173 | + 'test-notification-id', |
| 174 | + ); |
| 175 | + }); |
| 176 | + |
| 177 | + test('should display notification with different notificationId', () => { |
| 178 | + const notificationID = 'custom-id-789'; |
| 179 | + const notification = new OSNotification({ |
| 180 | + ...baseNotificationData, |
| 181 | + notificationId: notificationID, |
| 182 | + }); |
| 183 | + notification.display(); |
| 184 | + |
| 185 | + expect(mockRNOneSignal.displayNotification).toHaveBeenCalledWith( |
| 186 | + notificationID, |
| 187 | + ); |
| 188 | + }); |
| 189 | + |
| 190 | + test('should return undefined', () => { |
| 191 | + const notification = new OSNotification(baseNotificationData); |
| 192 | + const result = notification.display(); |
| 193 | + |
| 194 | + expect(result).toBeUndefined(); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + describe('rawPayload types', () => { |
| 199 | + test('should accept object as rawPayload', () => { |
| 200 | + const notificationData = { |
| 201 | + ...baseNotificationData, |
| 202 | + rawPayload: { key: 'value', nested: { key: 'value' } }, |
| 203 | + }; |
| 204 | + const notification = new OSNotification(notificationData); |
| 205 | + |
| 206 | + expect(notification.rawPayload).toEqual({ |
| 207 | + key: 'value', |
| 208 | + nested: { key: 'value' }, |
| 209 | + }); |
| 210 | + }); |
| 211 | + |
| 212 | + test('should accept string as rawPayload', () => { |
| 213 | + const notificationData = { |
| 214 | + ...baseNotificationData, |
| 215 | + rawPayload: '{"key":"value"}', |
| 216 | + }; |
| 217 | + const notification = new OSNotification(notificationData); |
| 218 | + |
| 219 | + expect(notification.rawPayload).toBe('{"key":"value"}'); |
| 220 | + }); |
| 221 | + |
| 222 | + test('should accept empty object as rawPayload', () => { |
| 223 | + const notificationData = { |
| 224 | + ...baseNotificationData, |
| 225 | + rawPayload: {}, |
| 226 | + }; |
| 227 | + const notification = new OSNotification(notificationData); |
| 228 | + |
| 229 | + expect(notification.rawPayload).toEqual({}); |
| 230 | + }); |
| 231 | + |
| 232 | + test('should accept empty string as rawPayload', () => { |
| 233 | + const notificationData = { |
| 234 | + ...baseNotificationData, |
| 235 | + rawPayload: '', |
| 236 | + }; |
| 237 | + const notification = new OSNotification(notificationData); |
| 238 | + |
| 239 | + expect(notification.rawPayload).toBe(''); |
| 240 | + }); |
| 241 | + }); |
| 242 | +}); |
0 commit comments