diff --git a/packages/event-bus-client/tests/index.test-d.ts b/packages/event-bus-client/tests/index.test-d.ts new file mode 100644 index 00000000..cf32cd24 --- /dev/null +++ b/packages/event-bus-client/tests/index.test-d.ts @@ -0,0 +1,65 @@ +import { describe, expectTypeOf, it } from 'vitest' +import { EventClient } from '../src' +import { EventClient as ProductionEventClient } from '../src/production' +import type { TanStackDevtoolsEvent } from '../src/types' + +type TestEvents = { + 'state-update': { count: number } + reset: void +} + +describe('EventClient', () => { + it('should expose the same type from the root and production entries', () => { + expectTypeOf>().toEqualTypeOf< + ProductionEventClient + >() + }) + + it('should infer the payload type of emit from the event name', () => { + const client = new EventClient({ pluginId: 'test' }) + + client.emit('state-update', { count: 1 }) + client.emit('reset', undefined) + // @ts-expect-error the payload does not match the event + client.emit('state-update', { count: '1' }) + // @ts-expect-error the event is not in the event map + client.emit('unknown', { count: 1 }) + }) + + it('should infer the event type of the on callback from the event name', () => { + const client = new EventClient({ pluginId: 'test' }) + + const cleanup = client.on('state-update', (event) => { + expectTypeOf(event).toEqualTypeOf< + TanStackDevtoolsEvent<'state-update', { count: number }> + >() + }) + + expectTypeOf(cleanup).toEqualTypeOf<() => void>() + // @ts-expect-error the event is not in the event map + client.on('unknown', () => {}) + }) + + it('should pass the union of plugin events to the onAllPluginEvents callback', () => { + const client = new EventClient({ pluginId: 'test' }) + + client.onAllPluginEvents((event) => { + expectTypeOf(event).toEqualTypeOf< + | TanStackDevtoolsEvent<'state-update', { count: number }> + | TanStackDevtoolsEvent<'reset', void> + >() + + if (event.type === 'state-update') { + expectTypeOf(event.payload).toEqualTypeOf<{ count: number }>() + } + }) + }) + + it('should pass untyped events to the onAll callback', () => { + const client = new EventClient({ pluginId: 'test' }) + + client.onAll((event) => { + expectTypeOf(event).toEqualTypeOf>() + }) + }) +}) diff --git a/packages/event-bus-client/vite.config.ts b/packages/event-bus-client/vite.config.ts index 999254e4..e1a2e0cc 100644 --- a/packages/event-bus-client/vite.config.ts +++ b/packages/event-bus-client/vite.config.ts @@ -11,6 +11,7 @@ const config = defineConfig({ environment: 'jsdom', setupFiles: ['./tests/test-setup.ts'], globals: true, + typecheck: { enabled: true }, }, })