@@ -3,7 +3,8 @@ import { ClientEventBus } from '@tanstack/devtools-event-bus/client'
33import { EventClient } from '../src'
44
55// start the client bus for testing
6- new ClientEventBus ( ) . start ( )
6+ const bus = new ClientEventBus ( )
7+ bus . start ( )
78// client bus uses window to dispatch events
89const clientBusEmitTarget = window
910describe ( 'EventClient' , ( ) => {
@@ -54,7 +55,7 @@ describe('EventClient', () => {
5455 const targetEmitSpy = vi . spyOn ( target , 'dispatchEvent' )
5556 const targetListenSpy = vi . spyOn ( target , 'addEventListener' )
5657 const targetRemoveSpy = vi . spyOn ( target , 'removeEventListener' )
57- const cleanup = client . on ( 'test:event' , ( ) => { } )
58+ const cleanup = client . on ( 'test:event' , ( ) => { } )
5859 cleanup ( )
5960 client . emit ( 'test:event' , { foo : 'bar' } )
6061 expect ( targetEmitSpy ) . toHaveBeenCalledWith ( expect . any ( Event ) )
@@ -78,7 +79,7 @@ describe('EventClient', () => {
7879 const targetEmitSpy = vi . spyOn ( target , 'dispatchEvent' )
7980 const targetListenSpy = vi . spyOn ( target , 'addEventListener' )
8081 const targetRemoveSpy = vi . spyOn ( target , 'removeEventListener' )
81- const cleanup = client . on ( 'test:event' , ( ) => { } )
82+ const cleanup = client . on ( 'test:event' , ( ) => { } )
8283 cleanup ( )
8384 client . emit ( 'test:event' , { foo : 'bar' } )
8485 expect ( targetEmitSpy ) . toHaveBeenCalledWith ( expect . any ( Event ) )
@@ -101,7 +102,7 @@ describe('EventClient', () => {
101102 } )
102103
103104 const eventBusSpy = vi . spyOn ( clientBusEmitTarget , 'addEventListener' )
104- client . on ( 'event' , ( ) => { } )
105+ client . on ( 'event' , ( ) => { } )
105106 expect ( eventBusSpy ) . toHaveBeenCalledWith (
106107 'test:event' ,
107108 expect . any ( Function ) ,
@@ -194,6 +195,27 @@ describe('EventClient', () => {
194195 } )
195196 } )
196197
198+ describe ( "queued events" , ( ) => {
199+ it ( "emits queued events when connected to the event bus" , async ( ) => {
200+ bus . stop ( )
201+ const client = new EventClient ( {
202+ debug : false ,
203+ pluginId : 'test' ,
204+ } )
205+ const eventHandler = vi . fn ( )
206+ client . on ( 'event' , eventHandler )
207+ client . emit ( 'event' , { foo : 'bar' } )
208+
209+ bus . start ( )
210+ // wait to connect to the bus
211+ await new Promise ( resolve => setTimeout ( resolve , 500 ) )
212+ expect ( eventHandler ) . toHaveBeenCalledWith ( {
213+ type : 'test:event' ,
214+ payload : { foo : 'bar' } ,
215+ pluginId : 'test' ,
216+ } )
217+ } )
218+ } )
197219 describe ( 'onAllPluginEvents' , ( ) => {
198220 it ( 'should listen to all events that come from the plugin' , ( ) => {
199221 const client = new EventClient ( {
0 commit comments