Skip to content

Commit b72cc40

Browse files
committed
chore: more tests for client bus
1 parent 537bc1f commit b72cc40

File tree

1 file changed

+85
-4
lines changed

1 file changed

+85
-4
lines changed
Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,88 @@
1-
import { describe, expect, it } from 'vitest'
1+
import { afterEach, describe, expect, it, vi } from 'vitest'
2+
import { ClientEventBus } from '../src/client'
23

3-
describe('devtools', () => {
4-
it('should pass', () => {
5-
expect(true).toBe(true)
4+
describe('ClientEventBus', () => {
5+
describe('debug', () => {
6+
afterEach(() => {
7+
vi.restoreAllMocks()
8+
})
9+
it('should log events to the console when debug set to true', () => {
10+
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
11+
const clientBus = new ClientEventBus({ debug: true })
12+
clientBus.start()
13+
14+
expect(logSpy).toHaveBeenCalledWith(
15+
'🌴 [tanstack-devtools:client-bus]',
16+
'Initializing client event bus',
17+
)
18+
logSpy.mockRestore()
19+
clientBus.stop()
20+
})
21+
22+
it('should not log events to the console when debug set to false', () => {
23+
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
24+
const clientBus = new ClientEventBus({ debug: false })
25+
clientBus.start()
26+
27+
expect(logSpy).not.toHaveBeenCalled()
28+
logSpy.mockRestore()
29+
clientBus.stop()
30+
})
31+
})
32+
it('should emit events to a subscribed listener', () => {
33+
const clientBus = new ClientEventBus()
34+
clientBus.start()
35+
const handler = vi.fn()
36+
window.addEventListener('test:event', handler)
37+
38+
window.dispatchEvent(
39+
new CustomEvent('tanstack-dispatch-event', {
40+
detail: {
41+
type: 'test:event',
42+
payload: { foo: 'bar' },
43+
},
44+
}),
45+
)
46+
expect(handler).toHaveBeenCalled()
47+
clientBus.stop()
48+
})
49+
50+
it('should emit events to listeners that are subscribed', () => {
51+
const clientBus = new ClientEventBus()
52+
clientBus.start()
53+
const handler = vi.fn()
54+
window.addEventListener('test:event', handler)
55+
const secondHandler = vi.fn()
56+
window.addEventListener('test:event', secondHandler)
57+
58+
window.dispatchEvent(
59+
new CustomEvent('tanstack-dispatch-event', {
60+
detail: {
61+
type: 'test:event',
62+
payload: { foo: 'bar' },
63+
},
64+
}),
65+
)
66+
expect(handler).toHaveBeenCalled()
67+
expect(secondHandler).toHaveBeenCalled()
68+
clientBus.stop()
69+
})
70+
71+
it('should emit events to global listeners when they are subscribed', () => {
72+
const clientBus = new ClientEventBus()
73+
clientBus.start()
74+
const handler = vi.fn()
75+
window.addEventListener('tanstack-devtools-global', handler)
76+
77+
window.dispatchEvent(
78+
new CustomEvent('tanstack-dispatch-event', {
79+
detail: {
80+
type: 'test:event',
81+
payload: { foo: 'bar' },
82+
},
83+
}),
84+
)
85+
expect(handler).toHaveBeenCalled()
86+
clientBus.stop()
687
})
788
})

0 commit comments

Comments
 (0)