-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
queryClient.types.test.tsx
174 lines (148 loc) 路 5.28 KB
/
queryClient.types.test.tsx
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { describe, it } from 'vitest'
import { QueryClient } from '../queryClient'
import { doNotExecute } from './utils'
import type { Equal, Expect } from './utils'
import type { DataTag, InfiniteData } from '../types'
describe('getQueryData', () => {
it('should be typed if key is tagged', () => {
doNotExecute(() => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()
const data = queryClient.getQueryData(queryKey)
const result: Expect<Equal<typeof data, number | undefined>> = true
return result
})
})
it('should infer unknown if key is not tagged', () => {
doNotExecute(() => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.getQueryData(queryKey)
const result: Expect<Equal<typeof data, unknown>> = true
return result
})
})
it('should infer passed generic if passed', () => {
doNotExecute(() => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.getQueryData<number>(queryKey)
const result: Expect<Equal<typeof data, number | undefined>> = true
return result
})
})
it('should only allow Arrays to be passed', () => {
doNotExecute(() => {
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', () => {
doNotExecute(() => {
const queryKey = ['key'] as DataTag<Array<string>, number>
const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, (prev) => {
const result: Expect<Equal<typeof prev, number | undefined>> = true
return result ? prev : 1
})
const result: Expect<Equal<typeof data, number | undefined>> = true
return result
})
})
it('value should be typed if key is tagged', () => {
doNotExecute(() => {
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)
const result: Expect<Equal<typeof data, number | undefined>> = true
return result
})
})
it('should infer unknown for updater if key is not tagged', () => {
doNotExecute(() => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, (prev) => {
const result: Expect<Equal<typeof prev, unknown>> = true
return result ? prev : 1
})
const result: Expect<Equal<typeof data, unknown>> = true
return result
})
})
it('should infer unknown for value if key is not tagged', () => {
doNotExecute(() => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, 'foo')
const result: Expect<Equal<typeof data, unknown>> = true
return result
})
})
it('should infer passed generic if passed', () => {
doNotExecute(() => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.setQueryData<string>(queryKey, (prev) => {
const result: Expect<Equal<typeof prev, string | undefined>> = true
return result ? prev : '1'
})
const result: Expect<Equal<typeof data, string | undefined>> = true
return result
})
})
it('should infer passed generic for value', () => {
doNotExecute(() => {
const queryKey = ['key'] as const
const queryClient = new QueryClient()
const data = queryClient.setQueryData<string>(queryKey, 'foo')
const result: Expect<Equal<typeof data, string | undefined>> = true
return result
})
})
})
describe('fetchInfiniteQuery', () => {
it('should allow passing pages', () => {
doNotExecute(async () => {
const data = await new QueryClient().fetchInfiniteQuery({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
pages: 5,
})
const result: Expect<Equal<typeof data, InfiniteData<string, number>>> =
true
return result
})
})
it('should not allow passing getNextPageParam without pages', () => {
doNotExecute(async () => {
return new QueryClient().fetchInfiniteQuery({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
initialPageParam: 1,
getNextPageParam: () => 1,
})
})
})
it('should not allow passing pages without getNextPageParam', () => {
doNotExecute(async () => {
// @ts-expect-error Property 'getNextPageParam' is missing
return new QueryClient().fetchInfiniteQuery({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
initialPageParam: 1,
pages: 5,
})
})
})
})