Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions packages/event-bus-client/tests/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<EventClient<TestEvents>>().toEqualTypeOf<
ProductionEventClient<TestEvents>
>()
})

it('should infer the payload type of emit from the event name', () => {
const client = new EventClient<TestEvents>({ 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<TestEvents>({ 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<TestEvents>({ 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<TestEvents>({ pluginId: 'test' })

client.onAll((event) => {
expectTypeOf(event).toEqualTypeOf<TanStackDevtoolsEvent<string, any>>()
})
})
})
1 change: 1 addition & 0 deletions packages/event-bus-client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const config = defineConfig({
environment: 'jsdom',
setupFiles: ['./tests/test-setup.ts'],
globals: true,
typecheck: { enabled: true },
},
})

Expand Down
Loading