forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryClient.test-d.ts
145 lines (120 loc) · 4.79 KB
/
queryClient.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { describe, expectTypeOf, it } from 'vitest'
import { QueryClient } from '../queryClient'
import type { DataTag, InfiniteData } from '@tanstack/query-core'
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'
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>()
})
it('should preserve updater parameter type inference when used in functions with explicit return types', () => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()
// Simulate usage inside a function with explicit return type
// The outer function returns 'unknown' but this shouldn't affect the updater's type inference
;(() =>
queryClient.setQueryData(queryKey, (data) => {
expectTypeOf(data).toEqualTypeOf<number | undefined>()
return data
})) satisfies () => unknown
})
})
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
new QueryClient().fetchInfiniteQuery({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
initialPageParam: 1,
pages: 5,
})
})
})