@@ -3,7 +3,8 @@ import { ClientEventBus } from '@tanstack/devtools-event-bus/client'
3
3
import { EventClient } from '../src'
4
4
5
5
// start the client bus for testing
6
- new ClientEventBus ( ) . start ( )
6
+ const bus = new ClientEventBus ( )
7
+ bus . start ( )
7
8
// client bus uses window to dispatch events
8
9
const clientBusEmitTarget = window
9
10
describe ( 'EventClient' , ( ) => {
@@ -54,7 +55,7 @@ describe('EventClient', () => {
54
55
const targetEmitSpy = vi . spyOn ( target , 'dispatchEvent' )
55
56
const targetListenSpy = vi . spyOn ( target , 'addEventListener' )
56
57
const targetRemoveSpy = vi . spyOn ( target , 'removeEventListener' )
57
- const cleanup = client . on ( 'test:event' , ( ) => { } )
58
+ const cleanup = client . on ( 'test:event' , ( ) => { } )
58
59
cleanup ( )
59
60
client . emit ( 'test:event' , { foo : 'bar' } )
60
61
expect ( targetEmitSpy ) . toHaveBeenCalledWith ( expect . any ( Event ) )
@@ -78,7 +79,7 @@ describe('EventClient', () => {
78
79
const targetEmitSpy = vi . spyOn ( target , 'dispatchEvent' )
79
80
const targetListenSpy = vi . spyOn ( target , 'addEventListener' )
80
81
const targetRemoveSpy = vi . spyOn ( target , 'removeEventListener' )
81
- const cleanup = client . on ( 'test:event' , ( ) => { } )
82
+ const cleanup = client . on ( 'test:event' , ( ) => { } )
82
83
cleanup ( )
83
84
client . emit ( 'test:event' , { foo : 'bar' } )
84
85
expect ( targetEmitSpy ) . toHaveBeenCalledWith ( expect . any ( Event ) )
@@ -101,7 +102,7 @@ describe('EventClient', () => {
101
102
} )
102
103
103
104
const eventBusSpy = vi . spyOn ( clientBusEmitTarget , 'addEventListener' )
104
- client . on ( 'event' , ( ) => { } )
105
+ client . on ( 'event' , ( ) => { } )
105
106
expect ( eventBusSpy ) . toHaveBeenCalledWith (
106
107
'test:event' ,
107
108
expect . any ( Function ) ,
@@ -194,6 +195,27 @@ describe('EventClient', () => {
194
195
} )
195
196
} )
196
197
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
+ } )
197
219
describe ( 'onAllPluginEvents' , ( ) => {
198
220
it ( 'should listen to all events that come from the plugin' , ( ) => {
199
221
const client = new EventClient ( {
0 commit comments