Skip to content

Commit

Permalink
test(solid-query): use vitest typecheck correctly with *.test-d.ts (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli committed Mar 3, 2024
1 parent add26ce commit b311a0a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 186 deletions.
98 changes: 98 additions & 0 deletions packages/solid-query/src/__tests__/createQuery.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { describe, expectTypeOf, it } from 'vitest'
import { createQuery, queryOptions } from '../index'

describe('initialData', () => {
describe('Config object overload', () => {
it('TData should always be defined when initialData is provided as an object', () => {
const { data } = createQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
initialData: { wow: true },
}))

expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
})

it('TData should be defined when passed through queryOptions', () => {
const options = queryOptions(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
initialData: { wow: true },
}))
const { data } = createQuery(options)

expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
})

it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
const { data } = createQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
initialData: () => ({ wow: true }),
}))

expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
})

it('TData should have undefined in the union when initialData is NOT provided', () => {
const { data } = createQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
}))

expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
})

it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
const { data } = createQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
initialData: () => undefined as { wow: boolean } | undefined,
}))

expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
})
})

describe('Query key overload', () => {
it('TData should always be defined when initialData is provided', () => {
const { data } = createQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
initialData: { wow: true },
}))

expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
})

it('TData should have undefined in the union when initialData is NOT provided', () => {
const { data } = createQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
}))

expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
})
})

describe('Query key and func', () => {
it('TData should always be defined when initialData is provided', () => {
const { data } = createQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
initialData: { wow: true },
}))

expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
})

it('TData should have undefined in the union when initialData is NOT provided', () => {
const { data } = createQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
}))

expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
})
})
})
186 changes: 0 additions & 186 deletions packages/solid-query/src/__tests__/createQuery.types.test.tsx

This file was deleted.

0 comments on commit b311a0a

Please sign in to comment.