Skip to content

Commit

Permalink
test(query-core): add type check tests for queryObserver (#8304)
Browse files Browse the repository at this point in the history
Co-authored-by: Dominik Dorfmeister <[email protected]>
  • Loading branch information
saul-atomrigs and TkDodo authored Dec 2, 2024
1 parent b7bad3d commit 12b6782
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions packages/query-core/src/__tests__/queryObserver.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expectTypeOf, it } from 'vitest'
import { QueryObserver } from '..'
import { createQueryClient, queryKey } from './utils'
import type { QueryClient } from '..'
import type { DefaultError, QueryClient } from '..'

describe('queryObserver', () => {
let queryClient: QueryClient
Expand All @@ -26,31 +26,58 @@ describe('queryObserver', () => {
if (result.isPending) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.isError).toEqualTypeOf<false>()
expectTypeOf(result.isPending).toEqualTypeOf<true>()
expectTypeOf(result.isLoading).toEqualTypeOf<boolean>()
expectTypeOf(result.isLoadingError).toEqualTypeOf<false>()
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.status).toEqualTypeOf<'pending'>()
}
if (result.isLoading) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.isError).toEqualTypeOf<false>()
expectTypeOf(result.isPending).toEqualTypeOf<true>()
expectTypeOf(result.isLoading).toEqualTypeOf<true>()
expectTypeOf(result.isLoadingError).toEqualTypeOf<false>()
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.isSuccess).toEqualTypeOf<false>()
expectTypeOf(result.status).toEqualTypeOf<'pending'>()
}

if (result.isLoadingError) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
expectTypeOf(result.error).toEqualTypeOf<Error>()
expectTypeOf(result.error).toEqualTypeOf<DefaultError>()
expectTypeOf(result.isError).toEqualTypeOf<true>()
expectTypeOf(result.isPending).toEqualTypeOf<false>()
expectTypeOf(result.isLoading).toEqualTypeOf<false>()
expectTypeOf(result.isLoadingError).toEqualTypeOf<true>()
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.isSuccess).toEqualTypeOf<false>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
}

if (result.isRefetchError) {
expectTypeOf(result.data).toEqualTypeOf<{ value: string }>()
expectTypeOf(result.error).toEqualTypeOf<Error>()
expectTypeOf(result.error).toEqualTypeOf<DefaultError>()
expectTypeOf(result.isError).toEqualTypeOf<true>()
expectTypeOf(result.isPending).toEqualTypeOf<false>()
expectTypeOf(result.isLoading).toEqualTypeOf<false>()
expectTypeOf(result.isLoadingError).toEqualTypeOf<false>()
expectTypeOf(result.isRefetchError).toEqualTypeOf<true>()
expectTypeOf(result.isSuccess).toEqualTypeOf<false>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
}

if (result.isSuccess) {
expectTypeOf(result.data).toEqualTypeOf<{ value: string }>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.isError).toEqualTypeOf<false>()
expectTypeOf(result.isPending).toEqualTypeOf<false>()
expectTypeOf(result.isLoading).toEqualTypeOf<false>()
expectTypeOf(result.isLoadingError).toEqualTypeOf<false>()
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.isSuccess).toEqualTypeOf<true>()
expectTypeOf(result.status).toEqualTypeOf<'success'>()
}
})
Expand Down

0 comments on commit 12b6782

Please sign in to comment.