Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(query-core): update vitest type test correctly #7039

Merged
merged 7 commits into from
Mar 9, 2024
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
62 changes: 62 additions & 0 deletions packages/query-core/src/tests/infiniteQueryObserver.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { afterEach, beforeEach, describe, expectTypeOf, it, vi } from 'vitest'
import { InfiniteQueryObserver } from '..'
import { createQueryClient, queryKey } from './utils'
import type { InfiniteData, QueryClient } from '..'

describe('InfiniteQueryObserver', () => {
let queryClient: QueryClient

beforeEach(() => {
queryClient = createQueryClient()
queryClient.mount()
})

afterEach(() => {
queryClient.clear()
})

it('should be inferred as a correct result type', async () => {
const next: number | undefined = 2
const queryFn = vi.fn(({ pageParam }) => String(pageParam))
const observer = new InfiniteQueryObserver(queryClient, {
queryKey: queryKey(),
queryFn,
initialPageParam: 1,
getNextPageParam: () => next,
})

const result = observer.getCurrentResult()

if (result.isPending) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.isLoading).toEqualTypeOf<boolean>()
expectTypeOf(result.status).toEqualTypeOf<'pending'>()
}

if (result.isLoading) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.isPending).toEqualTypeOf<true>()
expectTypeOf(result.status).toEqualTypeOf<'pending'>()
}

if (result.isLoadingError) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
expectTypeOf(result.error).toEqualTypeOf<Error>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
}

if (result.isRefetchError) {
expectTypeOf(result.data).toEqualTypeOf<InfiniteData<string, unknown>>()
expectTypeOf(result.error).toEqualTypeOf<Error>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
}

if (result.isSuccess) {
expectTypeOf(result.data).toEqualTypeOf<InfiniteData<string, unknown>>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.status).toEqualTypeOf<'success'>()
}
})
})
53 changes: 2 additions & 51 deletions packages/query-core/src/tests/infiniteQueryObserver.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import {
afterEach,
beforeEach,
describe,
expect,
expectTypeOf,
test,
vi,
} from 'vitest'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { InfiniteQueryObserver } from '..'
import { createQueryClient, queryKey, sleep } from './utils'
import type { InfiniteData, QueryClient } from '..'
import type { QueryClient } from '..'

describe('InfiniteQueryObserver', () => {
let queryClient: QueryClient
Expand Down Expand Up @@ -164,45 +156,4 @@ describe('InfiniteQueryObserver', () => {
expect(queryFn).toBeCalledTimes(3)
expect(observer.getCurrentResult().hasNextPage).toBe(false)
})

test('should be inferred as a correct result type', async () => {
const key = queryKey()
const next: number | undefined = 2
const queryFn = vi.fn(({ pageParam }) => String(pageParam))
const observer = new InfiniteQueryObserver(queryClient, {
queryKey: key,
queryFn,
initialPageParam: 1,
getNextPageParam: () => next,
})

const result = observer.getCurrentResult()

result.isPending &&
expectTypeOf<undefined>(result.data) &&
expectTypeOf<null>(result.error) &&
TkDodo marked this conversation as resolved.
Show resolved Hide resolved
expectTypeOf<boolean>(result.isLoading) &&
expectTypeOf<'pending'>(result.status)

result.isLoading &&
expectTypeOf<undefined>(result.data) &&
expectTypeOf<null>(result.error) &&
expectTypeOf<true>(result.isPending) &&
expectTypeOf<'pending'>(result.status)

result.isLoadingError &&
expectTypeOf<undefined>(result.data) &&
expectTypeOf<Error>(result.error) &&
expectTypeOf<'error'>(result.status)

result.isRefetchError &&
expectTypeOf<InfiniteData<string>>(result.data) &&
expectTypeOf<Error>(result.error) &&
expectTypeOf<'error'>(result.status)

result.isSuccess &&
expectTypeOf<InfiniteData<string>>(result.data) &&
expectTypeOf<null>(result.error) &&
expectTypeOf<'success'>(result.status)
})
})
135 changes: 135 additions & 0 deletions packages/query-core/src/tests/queryClient.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { describe, expectTypeOf, it } from 'vitest'
import { QueryClient } from '../queryClient'
import type { DataTag, InfiniteData } from '../types'

describe('getQueryData', () => {
it('should be typed if key is tagged', () => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()
const data = queryClient.getQueryData(queryKey)

expectTypeOf(data).toEqualTypeOf<number | undefined>()
})

it('should infer unknown if key is not tagged', () => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.getQueryData(queryKey)

expectTypeOf(data).toEqualTypeOf<unknown>()
})

it('should infer passed generic if passed', () => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.getQueryData<number>(queryKey)

expectTypeOf(data).toEqualTypeOf<number | undefined>()
})

it('should only allow Arrays to be passed', () => {
const queryKey = 'key' as const
const queryClient = new QueryClient()
// @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey'
return queryClient.getQueryData(queryKey)
})
})

describe('setQueryData', () => {
it('updater should be typed if key is tagged', () => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, (prev) => {
expectTypeOf(prev).toEqualTypeOf<number | undefined>()
return prev
})

expectTypeOf(data).toEqualTypeOf<number | undefined>()
})

it('value should be typed if key is tagged', () => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()

// @ts-expect-error value should be a number
queryClient.setQueryData(queryKey, '1')

// @ts-expect-error value should be a number
queryClient.setQueryData(queryKey, () => '1')

const data = queryClient.setQueryData(queryKey, 1)

expectTypeOf(data).toEqualTypeOf<number | undefined>()
})

it('should infer unknown for updater if key is not tagged', () => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, (prev) => {
expectTypeOf(prev).toEqualTypeOf<unknown>()
return prev
})

expectTypeOf(data).toEqualTypeOf<unknown>()
})

it('should infer unknown for value if key is not tagged', () => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, 'foo')

expectTypeOf(data).toEqualTypeOf<unknown>()
})

it('should infer passed generic if passed', () => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.setQueryData<string>(queryKey, (prev) => {
expectTypeOf(prev).toEqualTypeOf<string | undefined>()
return prev
})

expectTypeOf(data).toEqualTypeOf<string | undefined>()
})

it('should infer passed generic for value', () => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.setQueryData<string>(queryKey, 'foo')

expectTypeOf(data).toEqualTypeOf<string | undefined>()
})
})

describe('fetchInfiniteQuery', () => {
it('should allow passing pages', async () => {
const data = await new QueryClient().fetchInfiniteQuery({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
pages: 5,
})

expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})

it('should not allow passing getNextPageParam without pages', () => {
new QueryClient().fetchInfiniteQuery({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
initialPageParam: 1,
getNextPageParam: () => 1,
})
})

it('should not allow passing pages without getNextPageParam', () => {
// @ts-expect-error Property 'getNextPageParam' is missing
return new QueryClient().fetchInfiniteQuery({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
initialPageParam: 1,
pages: 5,
})
})
})
Loading
Loading