Skip to content

Commit 18c2cae

Browse files
authored
test(react-query): use vitest typecheck correctly with *.test-d.ts (#6978)
1 parent 6b96736 commit 18c2cae

15 files changed

+864
-1117
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@types/react-dom": "^18.2.19",
5151
"@typescript-eslint/eslint-plugin": "^6.20.0",
5252
"@typescript-eslint/parser": "^6.20.0",
53-
"@vitest/coverage-istanbul": "^1.2.2",
53+
"@vitest/coverage-istanbul": "^1.3.1",
5454
"cpy-cli": "^5.0.0",
5555
"esbuild-plugin-file-path-extensions": "^2.0.0",
5656
"eslint": "^8.56.0",
@@ -73,7 +73,7 @@
7373
"tsup": "^8.0.1",
7474
"typescript": "5.2.2",
7575
"vite": "^5.1.1",
76-
"vitest": "^1.2.2"
76+
"vitest": "^1.3.1"
7777
},
7878
"pnpm": {
7979
"overrides": {
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { describe, expectTypeOf, it } from 'vitest'
2+
import { QueryClient } from '@tanstack/query-core'
3+
import { infiniteQueryOptions } from '../infiniteQueryOptions'
4+
import { useInfiniteQuery } from '../useInfiniteQuery'
5+
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
6+
import type { InfiniteData, dataTagSymbol } from '@tanstack/query-core'
7+
8+
describe('queryOptions', () => {
9+
it('should not allow excess properties', () => {
10+
infiniteQueryOptions({
11+
queryKey: ['key'],
12+
queryFn: () => Promise.resolve('data'),
13+
getNextPageParam: () => 1,
14+
initialPageParam: 1,
15+
// @ts-expect-error this is a good error, because stallTime does not exist!
16+
stallTime: 1000,
17+
})
18+
})
19+
it('should infer types for callbacks', () => {
20+
infiniteQueryOptions({
21+
queryKey: ['key'],
22+
queryFn: () => Promise.resolve('data'),
23+
staleTime: 1000,
24+
getNextPageParam: () => 1,
25+
initialPageParam: 1,
26+
select: (data) => {
27+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
28+
},
29+
})
30+
})
31+
it('should work when passed to useInfiniteQuery', () => {
32+
const options = infiniteQueryOptions({
33+
queryKey: ['key'],
34+
queryFn: () => Promise.resolve('string'),
35+
getNextPageParam: () => 1,
36+
initialPageParam: 1,
37+
})
38+
39+
const { data } = useInfiniteQuery(options)
40+
41+
// known issue: type of pageParams is unknown when returned from useInfiniteQuery
42+
expectTypeOf(data).toEqualTypeOf<
43+
InfiniteData<string, unknown> | undefined
44+
>()
45+
})
46+
it('should work when passed to useSuspenseInfiniteQuery', () => {
47+
const options = infiniteQueryOptions({
48+
queryKey: ['key'],
49+
queryFn: () => Promise.resolve('string'),
50+
getNextPageParam: () => 1,
51+
initialPageParam: 1,
52+
})
53+
54+
const { data } = useSuspenseInfiniteQuery(options)
55+
56+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
57+
})
58+
it('should work when passed to fetchInfiniteQuery', async () => {
59+
const options = infiniteQueryOptions({
60+
queryKey: ['key'],
61+
queryFn: () => Promise.resolve('string'),
62+
getNextPageParam: () => 1,
63+
initialPageParam: 1,
64+
})
65+
66+
const data = await new QueryClient().fetchInfiniteQuery(options)
67+
68+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
69+
})
70+
it('should tag the queryKey with the result type of the QueryFn', () => {
71+
const { queryKey } = infiniteQueryOptions({
72+
queryKey: ['key'],
73+
queryFn: () => Promise.resolve('string'),
74+
getNextPageParam: () => 1,
75+
initialPageParam: 1,
76+
})
77+
78+
expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf<
79+
InfiniteData<string>
80+
>()
81+
})
82+
it('should tag the queryKey even if no promise is returned', () => {
83+
const { queryKey } = infiniteQueryOptions({
84+
queryKey: ['key'],
85+
queryFn: () => 'string',
86+
getNextPageParam: () => 1,
87+
initialPageParam: 1,
88+
})
89+
90+
expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf<
91+
InfiniteData<string>
92+
>()
93+
})
94+
it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
95+
const { queryKey } = infiniteQueryOptions({
96+
queryKey: ['key'],
97+
queryFn: () => Promise.resolve('string'),
98+
select: (data) => data.pages,
99+
getNextPageParam: () => 1,
100+
initialPageParam: 1,
101+
})
102+
103+
expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf<
104+
InfiniteData<string>
105+
>()
106+
})
107+
it('should return the proper type when passed to getQueryData', () => {
108+
const { queryKey } = infiniteQueryOptions({
109+
queryKey: ['key'],
110+
queryFn: () => Promise.resolve('string'),
111+
getNextPageParam: () => 1,
112+
initialPageParam: 1,
113+
})
114+
115+
const queryClient = new QueryClient()
116+
const data = queryClient.getQueryData(queryKey)
117+
118+
expectTypeOf(data).toEqualTypeOf<
119+
InfiniteData<string, unknown> | undefined
120+
>()
121+
})
122+
it('should properly type when passed to setQueryData', () => {
123+
const { queryKey } = infiniteQueryOptions({
124+
queryKey: ['key'],
125+
queryFn: () => Promise.resolve('string'),
126+
getNextPageParam: () => 1,
127+
initialPageParam: 1,
128+
})
129+
130+
const queryClient = new QueryClient()
131+
const data = queryClient.setQueryData(queryKey, (prev) => {
132+
expectTypeOf(prev).toEqualTypeOf<
133+
InfiniteData<string, unknown> | undefined
134+
>()
135+
return prev
136+
})
137+
138+
expectTypeOf(data).toEqualTypeOf<
139+
InfiniteData<string, unknown> | undefined
140+
>()
141+
})
142+
})

packages/react-query/src/__tests__/infiniteQueryOptions.types.test.tsx

Lines changed: 0 additions & 177 deletions
This file was deleted.

0 commit comments

Comments
 (0)