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(react-query): update vitest type test correctly #6990

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
f345936
test(react-query): update vitest type test code correctly (expectTypeOf)
manudeli Feb 28, 2024
5c1968a
chore: update
manudeli Feb 28, 2024
9f53ad4
chore(react-query): remove optional chaining
manudeli Feb 28, 2024
07c57b9
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Feb 29, 2024
e4077bb
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 2, 2024
5a7fdb5
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 3, 2024
0093460
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 3, 2024
1caa6d1
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 3, 2024
8fb94b8
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 4, 2024
9d6e078
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 4, 2024
a844599
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 4, 2024
699eede
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 5, 2024
1939c48
chore: update
manudeli Mar 5, 2024
fe5c3e9
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 6, 2024
a1717de
chore: update
manudeli Mar 6, 2024
0b2fac7
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 7, 2024
95e7528
Merge branch 'main' into test/react-query/vitest-expectTypeOf-Correctly
manudeli Mar 8, 2024
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
57 changes: 31 additions & 26 deletions packages/query-core/src/tests/infiniteQueryObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,31 +178,36 @@ describe('InfiniteQueryObserver', () => {

const result = observer.getCurrentResult()

result.isPending &&
expectTypeOf<undefined>(result.data) &&
expectTypeOf<null>(result.error) &&
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)
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>>()
expectTypeOf(result.error).toEqualTypeOf<Error>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
}

if (result.isSuccess) {
expectTypeOf(result.data).toEqualTypeOf<InfiniteData<string>>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.status).toEqualTypeOf<'success'>()
}
})
})
64 changes: 36 additions & 28 deletions packages/query-core/src/tests/queryObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ describe('queryObserver', () => {
})
let observerResult
const unsubscribe = observer.subscribe((result) => {
expectTypeOf<QueryObserverResult<{ myCount: number }>>(result)
expectTypeOf(result).toEqualTypeOf<
QueryObserverResult<{ myCount: number }>
>()
observerResult = result
})
await sleep(1)
Expand All @@ -136,7 +138,9 @@ describe('queryObserver', () => {
select: (data) => ({ myCount: data.count }),
})
const observerResult = await observer.refetch()
expectTypeOf<{ myCount: number } | undefined>(observerResult.data)
expectTypeOf(observerResult.data).toEqualTypeOf<
{ myCount: number } | undefined
>()
expect(observerResult.data).toMatchObject({ myCount: 1 })
})

Expand Down Expand Up @@ -905,31 +909,35 @@ describe('queryObserver', () => {

const result = observer.getCurrentResult()

result.isPending &&
expectTypeOf<undefined>(result.data) &&
expectTypeOf<null>(result.error) &&
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<{ value: string }>(result.data) &&
expectTypeOf<Error>(result.error) &&
expectTypeOf<'error'>(result.status)

result.isSuccess &&
expectTypeOf<{ value: string }>(result.data) &&
expectTypeOf<null>(result.error) &&
expectTypeOf<'success'>(result.status)
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<{ value: string }>()
expectTypeOf(result.error).toEqualTypeOf<Error>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
}

if (result.isSuccess) {
expectTypeOf(result.data).toEqualTypeOf<{ value: string }>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.status).toEqualTypeOf<'success'>()
}
})
})
14 changes: 4 additions & 10 deletions packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expectTypeOf, it } from 'vitest'
import { QueryClient } from '@tanstack/query-core'
import { type InfiniteData, dataTagSymbol } from '@tanstack/query-core'
import { infiniteQueryOptions } from '../infiniteQueryOptions'
import { useInfiniteQuery } from '../useInfiniteQuery'
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
import type { InfiniteData, dataTagSymbol } from '@tanstack/query-core'

describe('queryOptions', () => {
it('should not allow excess properties', () => {
Expand Down Expand Up @@ -75,9 +75,7 @@ describe('queryOptions', () => {
initialPageParam: 1,
})

expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf<
InfiniteData<string>
>()
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary generics

})
it('should tag the queryKey even if no promise is returned', () => {
const { queryKey } = infiniteQueryOptions({
Expand All @@ -87,9 +85,7 @@ describe('queryOptions', () => {
initialPageParam: 1,
})

expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf<
InfiniteData<string>
>()
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
})
it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
const { queryKey } = infiniteQueryOptions({
Expand All @@ -100,9 +96,7 @@ describe('queryOptions', () => {
initialPageParam: 1,
})

expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf<
InfiniteData<string>
>()
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
})
it('should return the proper type when passed to getQueryData', () => {
const { queryKey } = infiniteQueryOptions({
Expand Down
Loading
Loading