@@ -424,7 +424,7 @@ describe("Switch", () => {
424
424
return OFF ;
425
425
}
426
426
427
- test ( "sending events ", ( ) => {
427
+ it ( "changes state and change count ", ( ) => {
428
428
const machine = start ( Switch ) ;
429
429
expect ( machine ) . toBeDefined ( ) ;
430
430
expect ( machine . current ) . toEqual ( "OFF" ) ;
@@ -437,6 +437,30 @@ describe("Switch", () => {
437
437
expect ( machine . current ) . toEqual ( "OFF" ) ;
438
438
expect ( machine . changeCount ) . toEqual ( 2 ) ;
439
439
} ) ;
440
+
441
+ it ( "emits events to signal" , ( ) => {
442
+ const machine = start ( Switch ) ;
443
+ expect ( machine ) . toBeDefined ( ) ;
444
+ expect ( machine . signal ) . toBeInstanceOf ( AbortSignal ) ;
445
+
446
+ const eventListener = jest . fn ( ) ;
447
+ machine . signal . addEventListener ( "StateChanged" , eventListener ) ;
448
+
449
+ machine . next ( "FLICK" ) ;
450
+ expect ( machine . current ) . toEqual ( "ON" ) ;
451
+ expect ( eventListener ) . toHaveBeenCalledTimes ( 1 ) ;
452
+ expect ( eventListener ) . toHaveBeenLastCalledWith ( expect . objectContaining ( { type : "StateChanged" } ) ) ;
453
+
454
+ machine . next ( "FLICK" ) ;
455
+ expect ( machine . current ) . toEqual ( "OFF" ) ;
456
+ expect ( eventListener ) . toHaveBeenCalledTimes ( 2 ) ;
457
+
458
+ machine . signal . removeEventListener ( "StateChanged" , eventListener ) ;
459
+
460
+ machine . next ( "FLICK" ) ;
461
+ expect ( machine . current ) . toEqual ( "ON" ) ;
462
+ expect ( eventListener ) . toHaveBeenCalledTimes ( 2 ) ;
463
+ } ) ;
440
464
} ) ;
441
465
442
466
describe ( "Switch with symbol messages" , ( ) => {
0 commit comments