1
- import { describe , expect , it , vi } from 'vitest'
2
- import { EventClient } from '../src'
1
+ import { describe , expect , it , vi , } from 'vitest'
3
2
import { ClientEventBus } from '@tanstack/devtools-event-bus/client'
3
+ import { EventClient } from '../src'
4
4
5
5
// start the client bus for testing
6
6
new ClientEventBus ( ) . start ( )
7
7
// client bus uses window to dispatch events
8
8
const clientBusEmitTarget = window
9
-
10
9
describe ( 'EventClient' , ( ) => {
11
10
describe ( 'debug config' , ( ) => {
12
- it ( 'should emit logs when debug set to true and have the correct plugin name' , ( ) => {
11
+ it ( 'should emit logs when debug set to true and have the correct plugin name' , async ( ) => {
13
12
const consoleSpy = vi . spyOn ( console , 'log' )
14
13
new EventClient ( {
15
14
debug : true ,
@@ -35,15 +34,25 @@ describe('EventClient', () => {
35
34
describe ( 'getGlobalTarget' , ( ) => {
36
35
it ( 'if the global target is set it should re-use it for emitting/listening/removing of events' , ( ) => {
37
36
const target = new EventTarget ( )
38
- globalThis . __TANSTACK_EVENT_TARGET__ = target
37
+ const handleSuccessConnection = vi . fn ( )
38
+ target . addEventListener ( "tanstack-connect" , ( ) => {
39
+ target . dispatchEvent (
40
+ new CustomEvent ( "tanstack-connect-success" )
41
+ )
42
+ } )
43
+ globalThis . __TANSTACK_EVENT_TARGET__ = null
44
+
45
+ vi . spyOn ( globalThis , '__TANSTACK_EVENT_TARGET__' , 'get' ) . mockImplementation ( ( ) => {
46
+ return target
47
+ } )
39
48
const client = new EventClient ( {
40
49
debug : false ,
41
50
pluginId : 'test' ,
42
51
} )
43
52
const targetEmitSpy = vi . spyOn ( target , 'dispatchEvent' )
44
53
const targetListenSpy = vi . spyOn ( target , 'addEventListener' )
45
54
const targetRemoveSpy = vi . spyOn ( target , 'removeEventListener' )
46
- const cleanup = client . on ( 'test:event' , ( ) => { } )
55
+ const cleanup = client . on ( 'test:event' , ( ) => { } )
47
56
cleanup ( )
48
57
client . emit ( 'test:event' , { foo : 'bar' } )
49
58
expect ( targetEmitSpy ) . toHaveBeenCalledWith ( expect . any ( Event ) )
@@ -55,9 +64,9 @@ describe('EventClient', () => {
55
64
expect . any ( String ) ,
56
65
expect . any ( Function ) ,
57
66
)
58
- globalThis . __TANSTACK_EVENT_TARGET__ = null
67
+ vi . resetAllMocks ( )
68
+ target . removeEventListener ( "tanstack-connect" , handleSuccessConnection )
59
69
} )
60
-
61
70
it ( 'should use the window object if the globalTarget is not set for emitting/listening/removing of events' , ( ) => {
62
71
const target = window
63
72
const client = new EventClient ( {
@@ -67,7 +76,7 @@ describe('EventClient', () => {
67
76
const targetEmitSpy = vi . spyOn ( target , 'dispatchEvent' )
68
77
const targetListenSpy = vi . spyOn ( target , 'addEventListener' )
69
78
const targetRemoveSpy = vi . spyOn ( target , 'removeEventListener' )
70
- const cleanup = client . on ( 'test:event' , ( ) => { } )
79
+ const cleanup = client . on ( 'test:event' , ( ) => { } )
71
80
cleanup ( )
72
81
client . emit ( 'test:event' , { foo : 'bar' } )
73
82
expect ( targetEmitSpy ) . toHaveBeenCalledWith ( expect . any ( Event ) )
@@ -80,6 +89,7 @@ describe('EventClient', () => {
80
89
expect . any ( Function ) ,
81
90
)
82
91
} )
92
+
83
93
} )
84
94
85
95
describe ( 'on' , ( ) => {
@@ -90,7 +100,7 @@ describe('EventClient', () => {
90
100
} )
91
101
92
102
const eventBusSpy = vi . spyOn ( clientBusEmitTarget , 'addEventListener' )
93
- client . on ( 'event' , ( ) => { } )
103
+ client . on ( 'event' , ( ) => { } )
94
104
expect ( eventBusSpy ) . toHaveBeenCalledWith (
95
105
'test:event' ,
96
106
expect . any ( Function ) ,
0 commit comments