Skip to content

Commit 892662d

Browse files
authored
test(event-bus-client): add type tests for 'EventClient' and enable vitest 'typecheck' (#612)
1 parent 39e7210 commit 892662d

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { describe, expectTypeOf, it } from 'vitest'
2+
import { EventClient } from '../src'
3+
import { EventClient as ProductionEventClient } from '../src/production'
4+
import type { TanStackDevtoolsEvent } from '../src/types'
5+
6+
type TestEvents = {
7+
'state-update': { count: number }
8+
reset: void
9+
}
10+
11+
describe('EventClient', () => {
12+
it('should expose the same type from the root and production entries', () => {
13+
expectTypeOf<EventClient<TestEvents>>().toEqualTypeOf<
14+
ProductionEventClient<TestEvents>
15+
>()
16+
})
17+
18+
it('should infer the payload type of emit from the event name', () => {
19+
const client = new EventClient<TestEvents>({ pluginId: 'test' })
20+
21+
client.emit('state-update', { count: 1 })
22+
client.emit('reset', undefined)
23+
// @ts-expect-error the payload does not match the event
24+
client.emit('state-update', { count: '1' })
25+
// @ts-expect-error the event is not in the event map
26+
client.emit('unknown', { count: 1 })
27+
})
28+
29+
it('should infer the event type of the on callback from the event name', () => {
30+
const client = new EventClient<TestEvents>({ pluginId: 'test' })
31+
32+
const cleanup = client.on('state-update', (event) => {
33+
expectTypeOf(event).toEqualTypeOf<
34+
TanStackDevtoolsEvent<'state-update', { count: number }>
35+
>()
36+
})
37+
38+
expectTypeOf(cleanup).toEqualTypeOf<() => void>()
39+
// @ts-expect-error the event is not in the event map
40+
client.on('unknown', () => {})
41+
})
42+
43+
it('should pass the union of plugin events to the onAllPluginEvents callback', () => {
44+
const client = new EventClient<TestEvents>({ pluginId: 'test' })
45+
46+
client.onAllPluginEvents((event) => {
47+
expectTypeOf(event).toEqualTypeOf<
48+
| TanStackDevtoolsEvent<'state-update', { count: number }>
49+
| TanStackDevtoolsEvent<'reset', void>
50+
>()
51+
52+
if (event.type === 'state-update') {
53+
expectTypeOf(event.payload).toEqualTypeOf<{ count: number }>()
54+
}
55+
})
56+
})
57+
58+
it('should pass untyped events to the onAll callback', () => {
59+
const client = new EventClient<TestEvents>({ pluginId: 'test' })
60+
61+
client.onAll((event) => {
62+
expectTypeOf(event).toEqualTypeOf<TanStackDevtoolsEvent<string, any>>()
63+
})
64+
})
65+
})

‎packages/event-bus-client/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const config = defineConfig({
1111
environment: 'jsdom',
1212
setupFiles: ['./tests/test-setup.ts'],
1313
globals: true,
14+
typecheck: { enabled: true },
1415
},
1516
})
1617

0 commit comments

Comments
 (0)