@@ -12,20 +12,22 @@ import {
1212 SUBSCRIPTION_CHANGED ,
1313 USER_STATE_CHANGED ,
1414} from '../constants/events' ;
15+ import OSNotification from '../OSNotification' ;
16+ import type { NotificationClickEvent } from '../types/notificationEvents' ;
1517import type { PushSubscriptionChangedState } from '../types/subscription' ;
1618import type { UserChangedState } from '../types/user' ;
17- import EventManager , { type EventListenerMap } from './EventManager' ;
19+ import EventManager , { type RawEventListenerMap } from './EventManager' ;
1820import NotificationWillDisplayEvent from './NotificationWillDisplayEvent' ;
1921
2022describe ( 'EventManager' , ( ) => {
2123 let eventManager : EventManager ;
2224 let mockNativeModule : any ;
2325 let eventCallbacks : Map < string , ( payload : any ) => void > ;
2426
25- const getCallback = < K extends keyof EventListenerMap > (
27+ const getCallback = < K extends keyof RawEventListenerMap > (
2628 eventName : K ,
27- ) : ( ( payload : Parameters < EventListenerMap [ K ] > [ 0 ] ) => void ) | undefined => {
28- return eventCallbacks . get ( eventName ) as any ;
29+ ) : RawEventListenerMap [ K ] | undefined => {
30+ return eventCallbacks . get ( eventName ) as RawEventListenerMap [ K ] | undefined ;
2931 } ;
3032
3133 beforeEach ( ( ) => {
@@ -210,14 +212,7 @@ describe('EventManager', () => {
210212 eventManager . addEventListener ( NOTIFICATION_WILL_DISPLAY , handler ) ;
211213
212214 const callback = getCallback ( NOTIFICATION_WILL_DISPLAY ) ;
213-
214- const notificationPayload = {
215- notificationId : 'test-id' ,
216- body : 'test-body' ,
217- rawPayload : { } ,
218- } ;
219-
220- callback ! ( notificationPayload ) ;
215+ callback ! ( rawWillDisplayPayload ) ;
221216
222217 expect ( handler ) . toHaveBeenCalled ( ) ;
223218 const receivedEvent = handler . mock . calls [ 0 ] [ 0 ] ;
@@ -228,33 +223,22 @@ describe('EventManager', () => {
228223 const handler = vi . fn ( ) ;
229224 eventManager . addEventListener ( PERMISSION_CHANGED , handler ) ;
230225
226+ const payload = getRawPermissionChangedPayload ( true ) ;
231227 const callback = getCallback ( PERMISSION_CHANGED ) ;
232- callback ! ( { permission : true } ) ;
228+ callback ! ( payload ) ;
233229
234230 expect ( handler ) . toHaveBeenCalledWith ( true ) ;
235231 } ) ;
236232
237233 test ( 'should handle generic events with object payload' , ( ) => {
238234 const handler = vi . fn ( ) ;
239- const payload = {
240- previous : {
241- id : 'previous-id' ,
242- token : 'previous-token' ,
243- optedIn : false ,
244- } ,
245- current : {
246- id : 'current-id' ,
247- token : 'current-token' ,
248- optedIn : true ,
249- } ,
250- } satisfies PushSubscriptionChangedState ;
251235
252236 eventManager . addEventListener ( SUBSCRIPTION_CHANGED , handler ) ;
253237
254238 const callback = getCallback ( SUBSCRIPTION_CHANGED ) ;
255- callback ! ( payload ) ;
239+ callback ! ( pushChangedPayload ) ;
256240
257- expect ( handler ) . toHaveBeenCalledWith ( payload ) ;
241+ expect ( handler ) . toHaveBeenCalledWith ( pushChangedPayload ) ;
258242 } ) ;
259243
260244 test ( 'should call all handlers for an event' , ( ) => {
@@ -350,7 +334,14 @@ describe('EventManager', () => {
350334
351335 test ( 'should handle NOTIFICATION_CLICKED events' , ( ) => {
352336 const handler = vi . fn ( ) ;
353- const payload = { notificationId : 'notif-123' , action : 'clicked' } ;
337+ const payload = {
338+ result : { actionId : 'action-1' } ,
339+ notification : new OSNotification ( {
340+ notificationId : 'test-id' ,
341+ body : 'test-body' ,
342+ rawPayload : { } ,
343+ } ) ,
344+ } satisfies NotificationClickEvent ;
354345
355346 eventManager . addEventListener ( NOTIFICATION_CLICKED , handler ) ;
356347
@@ -372,7 +363,8 @@ describe('EventManager', () => {
372363
373364 // Trigger event
374365 const callback = getCallback ( PERMISSION_CHANGED ) ;
375- callback ! ( { permission : true } ) ;
366+ const payload = getRawPermissionChangedPayload ( true ) ;
367+ callback ! ( payload ) ;
376368
377369 expect ( handler1 ) . toHaveBeenCalledWith ( true ) ;
378370 expect ( handler2 ) . toHaveBeenCalledWith ( true ) ;
@@ -385,7 +377,7 @@ describe('EventManager', () => {
385377 handler2 . mockClear ( ) ;
386378
387379 // Trigger event again
388- callback ! ( { permission : false } ) ;
380+ callback ! ( getRawPermissionChangedPayload ( false ) ) ;
389381
390382 expect ( handler1 ) . not . toHaveBeenCalled ( ) ;
391383 expect ( handler2 ) . toHaveBeenCalledWith ( false ) ;
@@ -409,17 +401,16 @@ describe('EventManager', () => {
409401 const notificationCallback = getCallback ( NOTIFICATION_WILL_DISPLAY ) ;
410402
411403 // Trigger different event types
412- permissionCallback ! ( { permission : true } ) ;
413- subscriptionCallback ! ( { id : 'sub-123' } ) ;
414- notificationCallback ! ( {
415- notificationId : 'notif-123' ,
416- body : 'test' ,
417- rawPayload : { } ,
418- } ) ;
404+ const permissionPayload = getRawPermissionChangedPayload ( true ) ;
405+ permissionCallback ! ( permissionPayload ) ;
406+ subscriptionCallback ! ( pushChangedPayload ) ;
407+ notificationCallback ! ( rawWillDisplayPayload ) ;
419408
420409 expect ( permissionHandler ) . toHaveBeenCalledWith ( true ) ;
421- expect ( subscriptionHandler ) . toHaveBeenCalledWith ( { id : 'sub-123' } ) ;
422- expect ( notificationWillDisplayHandler ) . toHaveBeenCalled ( ) ;
410+ expect ( subscriptionHandler ) . toHaveBeenCalledWith ( pushChangedPayload ) ;
411+ expect ( notificationWillDisplayHandler ) . toHaveBeenCalledWith (
412+ new NotificationWillDisplayEvent ( rawWillDisplayPayload ) ,
413+ ) ;
423414 } ) ;
424415
425416 test ( 'should maintain separate handler arrays for different events' , ( ) => {
@@ -455,20 +446,44 @@ describe('EventManager', () => {
455446
456447 const callback = getCallback ( SUBSCRIPTION_CHANGED ) ;
457448
458- callback ! ( { id : '1' } ) ;
459- expect ( handler1 ) . toHaveBeenCalledWith ( { id : '1' } ) ;
460- expect ( handler2 ) . toHaveBeenCalledWith ( { id : '1' } ) ;
461- expect ( handler3 ) . toHaveBeenCalledWith ( { id : '1' } ) ;
449+ callback ! ( pushChangedPayload ) ;
450+ expect ( handler1 ) . toHaveBeenCalledWith ( pushChangedPayload ) ;
451+ expect ( handler2 ) . toHaveBeenCalledWith ( pushChangedPayload ) ;
452+ expect ( handler3 ) . toHaveBeenCalledWith ( pushChangedPayload ) ;
462453
463454 eventManager . removeEventListener ( SUBSCRIPTION_CHANGED , handler2 ) ;
464455 handler1 . mockClear ( ) ;
465456 handler2 . mockClear ( ) ;
466457 handler3 . mockClear ( ) ;
467458
468- callback ! ( { id : '2' } ) ;
469- expect ( handler1 ) . toHaveBeenCalledWith ( { id : '2' } ) ;
459+ callback ! ( pushChangedPayload ) ;
460+ expect ( handler1 ) . toHaveBeenCalledWith ( pushChangedPayload ) ;
470461 expect ( handler2 ) . not . toHaveBeenCalled ( ) ;
471- expect ( handler3 ) . toHaveBeenCalledWith ( { id : '2' } ) ;
462+ expect ( handler3 ) . toHaveBeenCalledWith ( pushChangedPayload ) ;
472463 } ) ;
473464 } ) ;
474465} ) ;
466+
467+ // helper payloads
468+ const getRawPermissionChangedPayload = ( permission : boolean ) => ( {
469+ permission : permission ,
470+ } ) ;
471+
472+ const rawWillDisplayPayload = new OSNotification ( {
473+ notificationId : 'test-id' ,
474+ body : 'test-body' ,
475+ rawPayload : { } ,
476+ } ) ;
477+
478+ const pushChangedPayload = {
479+ previous : {
480+ id : 'previous-id' ,
481+ token : 'previous-token' ,
482+ optedIn : false ,
483+ } ,
484+ current : {
485+ id : 'current-id' ,
486+ token : 'current-token' ,
487+ optedIn : true ,
488+ } ,
489+ } satisfies PushSubscriptionChangedState ;
0 commit comments