@tanstack/solid-query 4.24.10 → 5.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/index.js +300 -0
- package/build/cjs/index.js.map +1 -0
- package/build/esm/index.js +285 -0
- package/build/esm/index.js.map +1 -0
- package/build/source/QueryClientProvider.jsx +21 -0
- package/build/source/__tests__/QueryClientProvider.test.jsx +120 -0
- package/build/{solid → source}/__tests__/createInfiniteQuery.test.jsx +228 -372
- package/build/{solid → source}/__tests__/createMutation.test.jsx +174 -165
- package/build/{solid → source}/__tests__/createQueries.test.jsx +86 -367
- package/build/{solid → source}/__tests__/createQuery.test.jsx +991 -943
- package/build/{solid → source}/__tests__/createQuery.types.test.jsx +35 -24
- package/build/{solid → source}/__tests__/suspense.test.jsx +177 -148
- package/build/{solid → source}/__tests__/transition.test.jsx +7 -4
- package/build/{solid → source}/__tests__/useIsFetching.test.jsx +68 -85
- package/build/{solid → source}/__tests__/useIsMutating.test.jsx +78 -93
- package/build/{solid → source}/__tests__/utils.jsx +3 -9
- package/build/source/createBaseQuery.js +147 -0
- package/build/source/createInfiniteQuery.js +8 -0
- package/build/{solid → source}/createMutation.js +7 -9
- package/build/source/createQueries.js +32 -0
- package/build/source/createQuery.js +6 -0
- package/build/{solid → source}/index.js +5 -3
- package/build/source/setBatchUpdatesFn.js +3 -0
- package/build/source/useIsFetching.js +12 -0
- package/build/source/useIsMutating.js +12 -0
- package/build/source/utils.js +7 -0
- package/build/types/QueryClientProvider.d.ts +9 -0
- package/build/{lib → types}/__tests__/utils.d.ts +3 -8
- package/build/types/createBaseQuery.d.ts +4 -0
- package/build/types/createInfiniteQuery.d.ts +3 -0
- package/build/types/createMutation.d.ts +3 -0
- package/build/{lib → types}/createQueries.d.ts +10 -8
- package/build/types/createQuery.d.ts +11 -0
- package/build/{lib → types}/index.d.ts +4 -3
- package/build/types/setBatchUpdatesFn.d.ts +1 -0
- package/build/types/types.d.ts +33 -0
- package/build/types/useIsFetching.d.ts +8 -0
- package/build/types/useIsMutating.d.ts +8 -0
- package/build/types/utils.d.ts +1 -0
- package/build/umd/index.js +2 -0
- package/build/umd/index.js.map +1 -0
- package/package.json +25 -17
- package/src/QueryClientProvider.tsx +17 -86
- package/src/__tests__/QueryClientProvider.test.tsx +37 -140
- package/src/__tests__/createInfiniteQuery.test.tsx +277 -508
- package/src/__tests__/createMutation.test.tsx +177 -225
- package/src/__tests__/createQueries.test.tsx +180 -528
- package/src/__tests__/createQuery.test.tsx +970 -1200
- package/src/__tests__/createQuery.types.test.tsx +30 -25
- package/src/__tests__/suspense.test.tsx +141 -178
- package/src/__tests__/transition.test.tsx +7 -4
- package/src/__tests__/useIsFetching.test.tsx +77 -122
- package/src/__tests__/useIsMutating.test.tsx +83 -128
- package/src/__tests__/utils.tsx +4 -11
- package/src/createBaseQuery.ts +148 -60
- package/src/createInfiniteQuery.ts +16 -94
- package/src/createMutation.ts +9 -63
- package/src/createQueries.ts +44 -55
- package/src/createQuery.ts +42 -127
- package/src/index.ts +6 -3
- package/src/setBatchUpdatesFn.ts +4 -0
- package/src/types.ts +81 -75
- package/src/useIsFetching.ts +12 -44
- package/src/useIsMutating.ts +13 -29
- package/src/utils.ts +5 -79
- package/build/lib/QueryClientProvider.d.ts +0 -24
- package/build/lib/QueryClientProvider.esm.js +0 -74
- package/build/lib/QueryClientProvider.esm.js.map +0 -1
- package/build/lib/QueryClientProvider.js +0 -80
- package/build/lib/QueryClientProvider.js.map +0 -1
- package/build/lib/QueryClientProvider.mjs +0 -74
- package/build/lib/QueryClientProvider.mjs.map +0 -1
- package/build/lib/createBaseQuery.d.ts +0 -4
- package/build/lib/createBaseQuery.esm.js +0 -93
- package/build/lib/createBaseQuery.esm.js.map +0 -1
- package/build/lib/createBaseQuery.js +0 -97
- package/build/lib/createBaseQuery.js.map +0 -1
- package/build/lib/createBaseQuery.mjs +0 -93
- package/build/lib/createBaseQuery.mjs.map +0 -1
- package/build/lib/createInfiniteQuery.d.ts +0 -5
- package/build/lib/createInfiniteQuery.esm.js +0 -20
- package/build/lib/createInfiniteQuery.esm.js.map +0 -1
- package/build/lib/createInfiniteQuery.js +0 -24
- package/build/lib/createInfiniteQuery.js.map +0 -1
- package/build/lib/createInfiniteQuery.mjs +0 -20
- package/build/lib/createInfiniteQuery.mjs.map +0 -1
- package/build/lib/createMutation.d.ts +0 -6
- package/build/lib/createMutation.esm.js +0 -45
- package/build/lib/createMutation.esm.js.map +0 -1
- package/build/lib/createMutation.js +0 -49
- package/build/lib/createMutation.js.map +0 -1
- package/build/lib/createMutation.mjs +0 -45
- package/build/lib/createMutation.mjs.map +0 -1
- package/build/lib/createQueries.esm.js +0 -54
- package/build/lib/createQueries.esm.js.map +0 -1
- package/build/lib/createQueries.js +0 -58
- package/build/lib/createQueries.js.map +0 -1
- package/build/lib/createQueries.mjs +0 -54
- package/build/lib/createQueries.mjs.map +0 -1
- package/build/lib/createQuery.d.ts +0 -23
- package/build/lib/createQuery.esm.js +0 -25
- package/build/lib/createQuery.esm.js.map +0 -1
- package/build/lib/createQuery.js +0 -29
- package/build/lib/createQuery.js.map +0 -1
- package/build/lib/createQuery.mjs +0 -25
- package/build/lib/createQuery.mjs.map +0 -1
- package/build/lib/index.esm.js +0 -9
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.js +0 -31
- package/build/lib/index.js.map +0 -1
- package/build/lib/index.mjs +0 -9
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/types.d.ts +0 -47
- package/build/lib/useIsFetching.d.ts +0 -7
- package/build/lib/useIsFetching.esm.js +0 -29
- package/build/lib/useIsFetching.esm.js.map +0 -1
- package/build/lib/useIsFetching.js +0 -33
- package/build/lib/useIsFetching.js.map +0 -1
- package/build/lib/useIsFetching.mjs +0 -29
- package/build/lib/useIsFetching.mjs.map +0 -1
- package/build/lib/useIsMutating.d.ts +0 -8
- package/build/lib/useIsMutating.esm.js +0 -22
- package/build/lib/useIsMutating.esm.js.map +0 -1
- package/build/lib/useIsMutating.js +0 -26
- package/build/lib/useIsMutating.js.map +0 -1
- package/build/lib/useIsMutating.mjs +0 -22
- package/build/lib/useIsMutating.mjs.map +0 -1
- package/build/lib/utils.d.ts +0 -14
- package/build/lib/utils.esm.js +0 -63
- package/build/lib/utils.esm.js.map +0 -1
- package/build/lib/utils.js +0 -72
- package/build/lib/utils.js.map +0 -1
- package/build/lib/utils.mjs +0 -63
- package/build/lib/utils.mjs.map +0 -1
- package/build/solid/QueryClientProvider.jsx +0 -49
- package/build/solid/__tests__/QueryClientProvider.test.jsx +0 -185
- package/build/solid/createBaseQuery.js +0 -81
- package/build/solid/createInfiniteQuery.js +0 -16
- package/build/solid/createQueries.js +0 -39
- package/build/solid/createQuery.js +0 -16
- package/build/solid/useIsFetching.js +0 -23
- package/build/solid/useIsMutating.js +0 -16
- package/build/solid/utils.js +0 -45
- package/build/umd/index.development.js +0 -3577
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -2
- package/build/umd/index.production.js.map +0 -1
- /package/build/{solid → source}/types.js +0 -0
- /package/build/{lib → types}/__tests__/QueryClientProvider.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createInfiniteQuery.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createMutation.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQueries.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQuery.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQuery.types.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/suspense.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/transition.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/useIsFetching.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/useIsMutating.test.d.ts +0 -0
|
@@ -17,12 +17,16 @@ import type {
|
|
|
17
17
|
DefinedCreateQueryResult,
|
|
18
18
|
QueryFunction,
|
|
19
19
|
} from '..'
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
createQuery,
|
|
22
|
+
QueryCache,
|
|
23
|
+
QueryClientProvider,
|
|
24
|
+
keepPreviousData,
|
|
25
|
+
} from '..'
|
|
21
26
|
import {
|
|
22
27
|
Blink,
|
|
23
28
|
createQueryClient,
|
|
24
29
|
expectType,
|
|
25
|
-
mockLogger,
|
|
26
30
|
mockNavigatorOnLine,
|
|
27
31
|
mockVisibilityState,
|
|
28
32
|
queryKey,
|
|
@@ -41,70 +45,76 @@ describe('createQuery', () => {
|
|
|
41
45
|
// eslint-disable-next-line
|
|
42
46
|
function Page() {
|
|
43
47
|
// unspecified query function should default to unknown
|
|
44
|
-
const noQueryFn = createQuery(key)
|
|
48
|
+
const noQueryFn = createQuery(() => ({ queryKey: key }))
|
|
45
49
|
expectType<unknown>(noQueryFn.data)
|
|
46
50
|
expectType<unknown>(noQueryFn.error)
|
|
47
51
|
|
|
48
52
|
// it should infer the result type from the query function
|
|
49
|
-
const fromQueryFn = createQuery(
|
|
53
|
+
const fromQueryFn = createQuery(() => ({
|
|
54
|
+
queryKey: key,
|
|
55
|
+
queryFn: () => 'test',
|
|
56
|
+
}))
|
|
50
57
|
expectType<string | undefined>(fromQueryFn.data)
|
|
51
58
|
expectType<unknown>(fromQueryFn.error)
|
|
52
59
|
|
|
53
60
|
// it should be possible to specify the result type
|
|
54
|
-
const withResult = createQuery<string>(
|
|
61
|
+
const withResult = createQuery<string>(() => ({
|
|
62
|
+
queryKey: key,
|
|
63
|
+
queryFn: () => 'test',
|
|
64
|
+
}))
|
|
55
65
|
expectType<string | undefined>(withResult.data)
|
|
56
66
|
expectType<unknown | null>(withResult.error)
|
|
57
67
|
|
|
58
68
|
// it should be possible to specify the error type
|
|
59
|
-
const withError = createQuery<string, Error>(
|
|
69
|
+
const withError = createQuery<string, Error>(() => ({
|
|
70
|
+
queryKey: key,
|
|
71
|
+
queryFn: () => 'test',
|
|
72
|
+
}))
|
|
60
73
|
expectType<string | undefined>(withError.data)
|
|
61
74
|
expectType<Error | null>(withError.error)
|
|
62
75
|
|
|
63
76
|
// it should provide the result type in the configuration
|
|
64
|
-
createQuery(
|
|
65
|
-
|
|
66
|
-
async () => true,
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
},
|
|
71
|
-
)
|
|
77
|
+
createQuery(() => ({
|
|
78
|
+
queryKey: [key],
|
|
79
|
+
queryFn: async () => true,
|
|
80
|
+
onSuccess: (data) => expectType<boolean>(data),
|
|
81
|
+
onSettled: (data) => expectType<boolean | undefined>(data),
|
|
82
|
+
}))
|
|
72
83
|
|
|
73
84
|
// it should be possible to specify a union type as result type
|
|
74
|
-
const unionTypeSync = createQuery(
|
|
75
|
-
key,
|
|
76
|
-
() => (Math.random() > 0.5 ? 'a' : 'b'),
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
},
|
|
80
|
-
)
|
|
85
|
+
const unionTypeSync = createQuery(() => ({
|
|
86
|
+
queryKey: key,
|
|
87
|
+
queryFn: () => (Math.random() > 0.5 ? 'a' : 'b'),
|
|
88
|
+
onSuccess: (data) => expectType<'a' | 'b'>(data),
|
|
89
|
+
}))
|
|
81
90
|
expectType<'a' | 'b' | undefined>(unionTypeSync.data)
|
|
82
|
-
const unionTypeAsync = createQuery<'a' | 'b'>(
|
|
83
|
-
key,
|
|
84
|
-
() => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
},
|
|
88
|
-
)
|
|
91
|
+
const unionTypeAsync = createQuery<'a' | 'b'>(() => ({
|
|
92
|
+
queryKey: key,
|
|
93
|
+
queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
|
|
94
|
+
onSuccess: (data) => expectType<'a' | 'b'>(data),
|
|
95
|
+
}))
|
|
89
96
|
expectType<'a' | 'b' | undefined>(unionTypeAsync.data)
|
|
90
97
|
|
|
91
98
|
// should error when the query function result does not match with the specified type
|
|
92
99
|
// @ts-expect-error
|
|
93
|
-
createQuery<number>(key, () => 'test')
|
|
100
|
+
createQuery<number>(() => ({ queryKey: key, queryFn: () => 'test' }))
|
|
94
101
|
|
|
95
102
|
// it should infer the result type from a generic query function
|
|
96
103
|
function queryFn<T = string>(): Promise<T> {
|
|
97
104
|
return Promise.resolve({} as T)
|
|
98
105
|
}
|
|
99
106
|
|
|
100
|
-
const fromGenericQueryFn = createQuery(
|
|
107
|
+
const fromGenericQueryFn = createQuery(() => ({
|
|
108
|
+
queryKey: key,
|
|
109
|
+
queryFn: () => queryFn(),
|
|
110
|
+
}))
|
|
101
111
|
expectType<string | undefined>(fromGenericQueryFn.data)
|
|
102
112
|
expectType<unknown>(fromGenericQueryFn.error)
|
|
103
113
|
|
|
104
|
-
const fromGenericOptionsQueryFn = createQuery({
|
|
114
|
+
const fromGenericOptionsQueryFn = createQuery(() => ({
|
|
105
115
|
queryKey: key,
|
|
106
116
|
queryFn: () => queryFn(),
|
|
107
|
-
})
|
|
117
|
+
}))
|
|
108
118
|
expectType<string | undefined>(fromGenericOptionsQueryFn.data)
|
|
109
119
|
expectType<unknown>(fromGenericOptionsQueryFn.error)
|
|
110
120
|
|
|
@@ -117,38 +127,39 @@ describe('createQuery', () => {
|
|
|
117
127
|
return n + 42
|
|
118
128
|
}
|
|
119
129
|
|
|
120
|
-
createQuery({
|
|
121
|
-
queryKey:
|
|
130
|
+
createQuery(() => ({
|
|
131
|
+
queryKey: ['my-data', 100] as const,
|
|
122
132
|
queryFn: getMyDataArrayKey,
|
|
123
|
-
})
|
|
133
|
+
}))
|
|
124
134
|
|
|
125
|
-
const getMyDataStringKey: QueryFunction<MyData,
|
|
135
|
+
const getMyDataStringKey: QueryFunction<MyData, ['1']> = async (
|
|
126
136
|
context,
|
|
127
137
|
) => {
|
|
128
|
-
expectType<
|
|
138
|
+
expectType<['1']>(context.queryKey)
|
|
129
139
|
return Number(context.queryKey[0]) + 42
|
|
130
140
|
}
|
|
131
141
|
|
|
132
|
-
createQuery({
|
|
133
|
-
queryKey:
|
|
142
|
+
createQuery(() => ({
|
|
143
|
+
queryKey: ['1'] as ['1'],
|
|
134
144
|
queryFn: getMyDataStringKey,
|
|
135
|
-
})
|
|
145
|
+
}))
|
|
136
146
|
|
|
137
147
|
// it should handle query-functions that return Promise<any>
|
|
138
|
-
createQuery(
|
|
139
|
-
|
|
140
|
-
|
|
148
|
+
createQuery(() => ({
|
|
149
|
+
queryKey: key,
|
|
150
|
+
queryFn: () => fetch('return Promise<any>').then((resp) => resp.json()),
|
|
151
|
+
}))
|
|
141
152
|
|
|
142
153
|
// handles wrapped queries with custom fetcher passed as inline queryFn
|
|
143
154
|
const useWrappedQuery = <
|
|
144
|
-
TQueryKey extends
|
|
155
|
+
TQueryKey extends [string, Record<string, unknown>?],
|
|
145
156
|
TQueryFnData,
|
|
146
157
|
TError,
|
|
147
158
|
TData = TQueryFnData,
|
|
148
159
|
>(
|
|
149
160
|
qk: TQueryKey,
|
|
150
161
|
fetcher: (
|
|
151
|
-
obj:
|
|
162
|
+
obj: TQueryKey[1],
|
|
152
163
|
token: string,
|
|
153
164
|
// return type must be wrapped with TQueryFnReturn
|
|
154
165
|
) => Promise<TQueryFnData>,
|
|
@@ -156,16 +167,18 @@ describe('createQuery', () => {
|
|
|
156
167
|
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
157
168
|
'queryKey' | 'queryFn' | 'initialData'
|
|
158
169
|
>,
|
|
159
|
-
) =>
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
170
|
+
) =>
|
|
171
|
+
createQuery(() => ({
|
|
172
|
+
queryKey: qk,
|
|
173
|
+
queryFn: () => fetcher(qk[1], 'token'),
|
|
174
|
+
...options,
|
|
175
|
+
}))
|
|
176
|
+
const test = useWrappedQuery([''], async () => '1')
|
|
164
177
|
expectType<string | undefined>(test.data)
|
|
165
178
|
|
|
166
179
|
// handles wrapped queries with custom fetcher passed directly to createQuery
|
|
167
180
|
const useWrappedFuncStyleQuery = <
|
|
168
|
-
TQueryKey extends
|
|
181
|
+
TQueryKey extends [string, Record<string, unknown>?],
|
|
169
182
|
TQueryFnData,
|
|
170
183
|
TError,
|
|
171
184
|
TData = TQueryFnData,
|
|
@@ -176,11 +189,8 @@ describe('createQuery', () => {
|
|
|
176
189
|
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
177
190
|
'queryKey' | 'queryFn' | 'initialData'
|
|
178
191
|
>,
|
|
179
|
-
) => createQuery(qk, fetcher, options)
|
|
180
|
-
const testFuncStyle = useWrappedFuncStyleQuery(
|
|
181
|
-
() => [''],
|
|
182
|
-
async () => true,
|
|
183
|
-
)
|
|
192
|
+
) => createQuery(() => ({ queryKey: qk, queryFn: fetcher, ...options }))
|
|
193
|
+
const testFuncStyle = useWrappedFuncStyleQuery([''], async () => true)
|
|
184
194
|
expectType<boolean | undefined>(testFuncStyle.data)
|
|
185
195
|
}
|
|
186
196
|
})
|
|
@@ -190,10 +200,13 @@ describe('createQuery', () => {
|
|
|
190
200
|
const key = queryKey()
|
|
191
201
|
|
|
192
202
|
function Page() {
|
|
193
|
-
const state = createQuery(
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
203
|
+
const state = createQuery(() => ({
|
|
204
|
+
queryKey: key,
|
|
205
|
+
queryFn: async () => {
|
|
206
|
+
await sleep(10)
|
|
207
|
+
return 'test'
|
|
208
|
+
},
|
|
209
|
+
}))
|
|
197
210
|
|
|
198
211
|
return (
|
|
199
212
|
<div>
|
|
@@ -218,16 +231,19 @@ describe('createQuery', () => {
|
|
|
218
231
|
const states: CreateQueryResult<string>[] = []
|
|
219
232
|
|
|
220
233
|
function Page(): JSX.Element {
|
|
221
|
-
const state = createQuery<string, Error>(
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
234
|
+
const state = createQuery<string, Error>(() => ({
|
|
235
|
+
queryKey: key,
|
|
236
|
+
queryFn: async () => {
|
|
237
|
+
await sleep(10)
|
|
238
|
+
return 'test'
|
|
239
|
+
},
|
|
240
|
+
}))
|
|
225
241
|
|
|
226
242
|
createRenderEffect(() => {
|
|
227
243
|
states.push({ ...state })
|
|
228
244
|
})
|
|
229
245
|
|
|
230
|
-
if (state.
|
|
246
|
+
if (state.isPending) {
|
|
231
247
|
expectType<undefined>(state.data)
|
|
232
248
|
expectType<null>(state.error)
|
|
233
249
|
} else if (state.isLoadingError) {
|
|
@@ -240,8 +256,8 @@ describe('createQuery', () => {
|
|
|
240
256
|
|
|
241
257
|
return (
|
|
242
258
|
<Switch fallback={<span>{state.data}</span>}>
|
|
243
|
-
<Match when={state.
|
|
244
|
-
<span>
|
|
259
|
+
<Match when={state.isPending}>
|
|
260
|
+
<span>pending</span>
|
|
245
261
|
</Match>
|
|
246
262
|
<Match when={state.isLoadingError}>
|
|
247
263
|
<span>{state.error!.message}</span>
|
|
@@ -273,18 +289,17 @@ describe('createQuery', () => {
|
|
|
273
289
|
isFetchedAfterMount: false,
|
|
274
290
|
isFetching: true,
|
|
275
291
|
isPaused: false,
|
|
276
|
-
|
|
292
|
+
isPending: true,
|
|
277
293
|
isInitialLoading: true,
|
|
294
|
+
isLoading: true,
|
|
278
295
|
isLoadingError: false,
|
|
279
296
|
isPlaceholderData: false,
|
|
280
|
-
isPreviousData: false,
|
|
281
297
|
isRefetchError: false,
|
|
282
298
|
isRefetching: false,
|
|
283
299
|
isStale: true,
|
|
284
300
|
isSuccess: false,
|
|
285
301
|
refetch: expect.any(Function),
|
|
286
|
-
|
|
287
|
-
status: 'loading',
|
|
302
|
+
status: 'pending',
|
|
288
303
|
fetchStatus: 'fetching',
|
|
289
304
|
})
|
|
290
305
|
|
|
@@ -301,17 +316,16 @@ describe('createQuery', () => {
|
|
|
301
316
|
isFetchedAfterMount: true,
|
|
302
317
|
isFetching: false,
|
|
303
318
|
isPaused: false,
|
|
304
|
-
|
|
319
|
+
isPending: false,
|
|
305
320
|
isInitialLoading: false,
|
|
321
|
+
isLoading: false,
|
|
306
322
|
isLoadingError: false,
|
|
307
323
|
isPlaceholderData: false,
|
|
308
|
-
isPreviousData: false,
|
|
309
324
|
isRefetchError: false,
|
|
310
325
|
isRefetching: false,
|
|
311
326
|
isStale: true,
|
|
312
327
|
isSuccess: true,
|
|
313
328
|
refetch: expect.any(Function),
|
|
314
|
-
remove: expect.any(Function),
|
|
315
329
|
status: 'success',
|
|
316
330
|
fetchStatus: 'idle',
|
|
317
331
|
})
|
|
@@ -320,17 +334,15 @@ describe('createQuery', () => {
|
|
|
320
334
|
it('should return the correct states for an unsuccessful query', async () => {
|
|
321
335
|
const key = queryKey()
|
|
322
336
|
|
|
323
|
-
const states: CreateQueryResult<
|
|
337
|
+
const states: CreateQueryResult<unknown, Error>[] = []
|
|
324
338
|
|
|
325
339
|
function Page() {
|
|
326
|
-
const state = createQuery
|
|
327
|
-
key,
|
|
328
|
-
() => Promise.reject('rejected'),
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
},
|
|
333
|
-
)
|
|
340
|
+
const state = createQuery(() => ({
|
|
341
|
+
queryKey: key,
|
|
342
|
+
queryFn: () => Promise.reject(new Error('rejected')),
|
|
343
|
+
retry: 1,
|
|
344
|
+
retryDelay: 1,
|
|
345
|
+
}))
|
|
334
346
|
|
|
335
347
|
createRenderEffect(() => {
|
|
336
348
|
states.push({ ...state })
|
|
@@ -340,7 +352,7 @@ describe('createQuery', () => {
|
|
|
340
352
|
<div>
|
|
341
353
|
<h1>Status: {state.status}</h1>
|
|
342
354
|
<div>Failure Count: {state.failureCount}</div>
|
|
343
|
-
<div>Failure Reason: {state.failureReason}</div>
|
|
355
|
+
<div>Failure Reason: {state.failureReason?.message}</div>
|
|
344
356
|
</div>
|
|
345
357
|
)
|
|
346
358
|
}
|
|
@@ -366,18 +378,17 @@ describe('createQuery', () => {
|
|
|
366
378
|
isFetchedAfterMount: false,
|
|
367
379
|
isFetching: true,
|
|
368
380
|
isPaused: false,
|
|
369
|
-
|
|
381
|
+
isPending: true,
|
|
370
382
|
isInitialLoading: true,
|
|
383
|
+
isLoading: true,
|
|
371
384
|
isLoadingError: false,
|
|
372
385
|
isPlaceholderData: false,
|
|
373
|
-
isPreviousData: false,
|
|
374
386
|
isRefetchError: false,
|
|
375
387
|
isRefetching: false,
|
|
376
388
|
isStale: true,
|
|
377
389
|
isSuccess: false,
|
|
378
390
|
refetch: expect.any(Function),
|
|
379
|
-
|
|
380
|
-
status: 'loading',
|
|
391
|
+
status: 'pending',
|
|
381
392
|
fetchStatus: 'fetching',
|
|
382
393
|
})
|
|
383
394
|
|
|
@@ -387,52 +398,50 @@ describe('createQuery', () => {
|
|
|
387
398
|
error: null,
|
|
388
399
|
errorUpdatedAt: 0,
|
|
389
400
|
failureCount: 1,
|
|
390
|
-
failureReason: 'rejected',
|
|
401
|
+
failureReason: new Error('rejected'),
|
|
391
402
|
errorUpdateCount: 0,
|
|
392
403
|
isError: false,
|
|
393
404
|
isFetched: false,
|
|
394
405
|
isFetchedAfterMount: false,
|
|
395
406
|
isFetching: true,
|
|
396
407
|
isPaused: false,
|
|
397
|
-
|
|
408
|
+
isPending: true,
|
|
398
409
|
isInitialLoading: true,
|
|
410
|
+
isLoading: true,
|
|
399
411
|
isLoadingError: false,
|
|
400
412
|
isPlaceholderData: false,
|
|
401
|
-
isPreviousData: false,
|
|
402
413
|
isRefetchError: false,
|
|
403
414
|
isRefetching: false,
|
|
404
415
|
isStale: true,
|
|
405
416
|
isSuccess: false,
|
|
406
417
|
refetch: expect.any(Function),
|
|
407
|
-
|
|
408
|
-
status: 'loading',
|
|
418
|
+
status: 'pending',
|
|
409
419
|
fetchStatus: 'fetching',
|
|
410
420
|
})
|
|
411
421
|
|
|
412
422
|
expect(states[2]).toEqual({
|
|
413
423
|
data: undefined,
|
|
414
424
|
dataUpdatedAt: 0,
|
|
415
|
-
error: 'rejected',
|
|
425
|
+
error: new Error('rejected'),
|
|
416
426
|
errorUpdatedAt: expect.any(Number),
|
|
417
427
|
failureCount: 2,
|
|
418
|
-
failureReason: 'rejected',
|
|
428
|
+
failureReason: new Error('rejected'),
|
|
419
429
|
errorUpdateCount: 1,
|
|
420
430
|
isError: true,
|
|
421
431
|
isFetched: true,
|
|
422
432
|
isFetchedAfterMount: true,
|
|
423
433
|
isFetching: false,
|
|
424
434
|
isPaused: false,
|
|
425
|
-
|
|
435
|
+
isPending: false,
|
|
426
436
|
isInitialLoading: false,
|
|
437
|
+
isLoading: false,
|
|
427
438
|
isLoadingError: true,
|
|
428
439
|
isPlaceholderData: false,
|
|
429
|
-
isPreviousData: false,
|
|
430
440
|
isRefetchError: false,
|
|
431
441
|
isRefetching: false,
|
|
432
442
|
isStale: true,
|
|
433
443
|
isSuccess: false,
|
|
434
444
|
refetch: expect.any(Function),
|
|
435
|
-
remove: expect.any(Function),
|
|
436
445
|
status: 'error',
|
|
437
446
|
fetchStatus: 'idle',
|
|
438
447
|
})
|
|
@@ -442,11 +451,16 @@ describe('createQuery', () => {
|
|
|
442
451
|
const key = queryKey()
|
|
443
452
|
const states: CreateQueryResult<string>[] = []
|
|
444
453
|
|
|
445
|
-
|
|
446
|
-
|
|
454
|
+
await queryClient.prefetchQuery({
|
|
455
|
+
queryKey: key,
|
|
456
|
+
queryFn: () => 'prefetched',
|
|
457
|
+
})
|
|
447
458
|
|
|
448
459
|
function Page() {
|
|
449
|
-
const state = createQuery(
|
|
460
|
+
const state = createQuery(() => ({
|
|
461
|
+
queryKey: key,
|
|
462
|
+
queryFn: () => 'data',
|
|
463
|
+
}))
|
|
450
464
|
createRenderEffect(() => {
|
|
451
465
|
states.push({ ...state })
|
|
452
466
|
})
|
|
@@ -480,14 +494,14 @@ describe('createQuery', () => {
|
|
|
480
494
|
const onSuccess = jest.fn()
|
|
481
495
|
|
|
482
496
|
function Page() {
|
|
483
|
-
const state = createQuery(
|
|
484
|
-
key,
|
|
485
|
-
async () => {
|
|
497
|
+
const state = createQuery(() => ({
|
|
498
|
+
queryKey: key,
|
|
499
|
+
queryFn: async () => {
|
|
486
500
|
await sleep(10)
|
|
487
501
|
return 'data'
|
|
488
502
|
},
|
|
489
|
-
|
|
490
|
-
)
|
|
503
|
+
onSuccess,
|
|
504
|
+
}))
|
|
491
505
|
createRenderEffect(() => {
|
|
492
506
|
states.push({ ...state })
|
|
493
507
|
})
|
|
@@ -506,65 +520,18 @@ describe('createQuery', () => {
|
|
|
506
520
|
expect(onSuccess).toHaveBeenCalledWith('data')
|
|
507
521
|
})
|
|
508
522
|
|
|
509
|
-
it('should call onSuccess after a query has been refetched', async () => {
|
|
510
|
-
const key = queryKey()
|
|
511
|
-
const states: CreateQueryResult<string>[] = []
|
|
512
|
-
const onSuccess = jest.fn()
|
|
513
|
-
let count = 0
|
|
514
|
-
|
|
515
|
-
function Page() {
|
|
516
|
-
const state = createQuery(
|
|
517
|
-
key,
|
|
518
|
-
async () => {
|
|
519
|
-
count++
|
|
520
|
-
await sleep(10)
|
|
521
|
-
return 'data' + count
|
|
522
|
-
},
|
|
523
|
-
{ onSuccess },
|
|
524
|
-
)
|
|
525
|
-
|
|
526
|
-
createRenderEffect(
|
|
527
|
-
on(
|
|
528
|
-
() => [state.data, state.refetch],
|
|
529
|
-
() => {
|
|
530
|
-
states.push(state)
|
|
531
|
-
},
|
|
532
|
-
),
|
|
533
|
-
)
|
|
534
|
-
|
|
535
|
-
return (
|
|
536
|
-
<div>
|
|
537
|
-
<div>data: {state.data}</div>
|
|
538
|
-
<button onClick={() => state.refetch()}>refetch</button>
|
|
539
|
-
</div>
|
|
540
|
-
)
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
render(() => (
|
|
544
|
-
<QueryClientProvider client={queryClient}>
|
|
545
|
-
<Page />
|
|
546
|
-
</QueryClientProvider>
|
|
547
|
-
))
|
|
548
|
-
|
|
549
|
-
await screen.findByText('data: data1')
|
|
550
|
-
fireEvent.click(screen.getByRole('button', { name: /refetch/i }))
|
|
551
|
-
await screen.findByText('data: data2')
|
|
552
|
-
|
|
553
|
-
expect(states.length).toBe(3) //loading, success, success after refetch
|
|
554
|
-
expect(count).toBe(2)
|
|
555
|
-
expect(onSuccess).toHaveBeenCalledTimes(2)
|
|
556
|
-
})
|
|
557
|
-
|
|
558
523
|
it('should call onSuccess after a disabled query has been fetched', async () => {
|
|
559
524
|
const key = queryKey()
|
|
560
525
|
const states: CreateQueryResult<string>[] = []
|
|
561
526
|
const onSuccess = jest.fn()
|
|
562
527
|
|
|
563
528
|
function Page() {
|
|
564
|
-
const state = createQuery(
|
|
529
|
+
const state = createQuery(() => ({
|
|
530
|
+
queryKey: key,
|
|
531
|
+
queryFn: () => 'data',
|
|
565
532
|
enabled: false,
|
|
566
533
|
onSuccess,
|
|
567
|
-
})
|
|
534
|
+
}))
|
|
568
535
|
|
|
569
536
|
createRenderEffect(() => {
|
|
570
537
|
states.push({ ...state })
|
|
@@ -606,14 +573,14 @@ describe('createQuery', () => {
|
|
|
606
573
|
}
|
|
607
574
|
|
|
608
575
|
function Component() {
|
|
609
|
-
const state = createQuery(
|
|
610
|
-
key,
|
|
611
|
-
async () => {
|
|
576
|
+
const state = createQuery(() => ({
|
|
577
|
+
queryKey: key,
|
|
578
|
+
queryFn: async () => {
|
|
612
579
|
await sleep(10)
|
|
613
580
|
return 'data'
|
|
614
581
|
},
|
|
615
|
-
|
|
616
|
-
)
|
|
582
|
+
onSuccess,
|
|
583
|
+
}))
|
|
617
584
|
createRenderEffect(() => {
|
|
618
585
|
states.push({ ...state })
|
|
619
586
|
})
|
|
@@ -637,10 +604,13 @@ describe('createQuery', () => {
|
|
|
637
604
|
const onError = jest.fn()
|
|
638
605
|
|
|
639
606
|
function Page() {
|
|
640
|
-
const state = createQuery
|
|
607
|
+
const state = createQuery(() => ({
|
|
608
|
+
queryKey: key,
|
|
609
|
+
queryFn: () => Promise.reject(new Error('error')),
|
|
641
610
|
retry: false,
|
|
642
611
|
onError,
|
|
643
|
-
})
|
|
612
|
+
}))
|
|
613
|
+
|
|
644
614
|
createRenderEffect(() => {
|
|
645
615
|
states.push({ ...state })
|
|
646
616
|
})
|
|
@@ -657,7 +627,7 @@ describe('createQuery', () => {
|
|
|
657
627
|
await sleep(10)
|
|
658
628
|
expect(states.length).toBe(2)
|
|
659
629
|
expect(onError).toHaveBeenCalledTimes(1)
|
|
660
|
-
expect(onError).toHaveBeenCalledWith('error')
|
|
630
|
+
expect(onError).toHaveBeenCalledWith(new Error('error'))
|
|
661
631
|
})
|
|
662
632
|
|
|
663
633
|
it('should not call onError when receiving a CancelledError', async () => {
|
|
@@ -665,16 +635,14 @@ describe('createQuery', () => {
|
|
|
665
635
|
const onError = jest.fn()
|
|
666
636
|
|
|
667
637
|
function Page() {
|
|
668
|
-
const state = createQuery(
|
|
669
|
-
key,
|
|
670
|
-
async () => {
|
|
638
|
+
const state = createQuery(() => ({
|
|
639
|
+
queryKey: key,
|
|
640
|
+
queryFn: async () => {
|
|
671
641
|
await sleep(10)
|
|
672
642
|
return 23
|
|
673
643
|
},
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
},
|
|
677
|
-
)
|
|
644
|
+
onError,
|
|
645
|
+
}))
|
|
678
646
|
return (
|
|
679
647
|
<span>
|
|
680
648
|
status: {state.status}, fetchStatus: {state.fetchStatus}
|
|
@@ -689,9 +657,9 @@ describe('createQuery', () => {
|
|
|
689
657
|
))
|
|
690
658
|
|
|
691
659
|
await sleep(5)
|
|
692
|
-
await queryClient.cancelQueries(key
|
|
660
|
+
await queryClient.cancelQueries({ queryKey: key })
|
|
693
661
|
// query cancellation will reset the query to it's initial state
|
|
694
|
-
await waitFor(() => screen.getByText('status:
|
|
662
|
+
await waitFor(() => screen.getByText('status: pending, fetchStatus: idle'))
|
|
695
663
|
expect(onError).not.toHaveBeenCalled()
|
|
696
664
|
})
|
|
697
665
|
|
|
@@ -701,7 +669,12 @@ describe('createQuery', () => {
|
|
|
701
669
|
const onSettled = jest.fn()
|
|
702
670
|
|
|
703
671
|
function Page() {
|
|
704
|
-
const state = createQuery(
|
|
672
|
+
const state = createQuery(() => ({
|
|
673
|
+
queryKey: key,
|
|
674
|
+
queryFn: () => 'data',
|
|
675
|
+
onSettled,
|
|
676
|
+
}))
|
|
677
|
+
|
|
705
678
|
createRenderEffect(() => {
|
|
706
679
|
states.push({ ...state })
|
|
707
680
|
})
|
|
@@ -726,10 +699,12 @@ describe('createQuery', () => {
|
|
|
726
699
|
const onSettled = jest.fn()
|
|
727
700
|
|
|
728
701
|
function Page() {
|
|
729
|
-
const state = createQuery(
|
|
702
|
+
const state = createQuery(() => ({
|
|
703
|
+
queryKey: key,
|
|
704
|
+
queryFn: () => Promise.reject<unknown>('error'),
|
|
730
705
|
retry: false,
|
|
731
706
|
onSettled,
|
|
732
|
-
})
|
|
707
|
+
}))
|
|
733
708
|
createRenderEffect(() => {
|
|
734
709
|
states.push({ ...state })
|
|
735
710
|
})
|
|
@@ -753,15 +728,16 @@ describe('createQuery', () => {
|
|
|
753
728
|
let fetchCount = 0
|
|
754
729
|
|
|
755
730
|
function Page() {
|
|
756
|
-
const state = createQuery(
|
|
757
|
-
key,
|
|
758
|
-
async () => {
|
|
731
|
+
const state = createQuery(() => ({
|
|
732
|
+
queryKey: key,
|
|
733
|
+
queryFn: async () => {
|
|
759
734
|
fetchCount++
|
|
760
735
|
await sleep(10)
|
|
761
736
|
return 'data'
|
|
762
737
|
},
|
|
763
|
-
|
|
764
|
-
|
|
738
|
+
enabled: false,
|
|
739
|
+
initialData: 'initialData',
|
|
740
|
+
}))
|
|
765
741
|
|
|
766
742
|
createEffect(() => {
|
|
767
743
|
setActTimeout(() => {
|
|
@@ -791,15 +767,16 @@ describe('createQuery', () => {
|
|
|
791
767
|
let fetchCount = 0
|
|
792
768
|
|
|
793
769
|
function Page() {
|
|
794
|
-
const state = createQuery(
|
|
795
|
-
key,
|
|
796
|
-
async () => {
|
|
770
|
+
const state = createQuery(() => ({
|
|
771
|
+
queryKey: key,
|
|
772
|
+
queryFn: async () => {
|
|
797
773
|
fetchCount++
|
|
798
774
|
await sleep(10)
|
|
799
775
|
return 'data'
|
|
800
776
|
},
|
|
801
|
-
|
|
802
|
-
|
|
777
|
+
enabled: false,
|
|
778
|
+
initialData: 'initialData',
|
|
779
|
+
}))
|
|
803
780
|
|
|
804
781
|
createEffect(() => {
|
|
805
782
|
setActTimeout(() => {
|
|
@@ -829,15 +806,15 @@ describe('createQuery', () => {
|
|
|
829
806
|
let fetchCount = 0
|
|
830
807
|
|
|
831
808
|
function Page() {
|
|
832
|
-
const state = createQuery(
|
|
833
|
-
key,
|
|
834
|
-
async () => {
|
|
809
|
+
const state = createQuery(() => ({
|
|
810
|
+
queryKey: key,
|
|
811
|
+
queryFn: async () => {
|
|
835
812
|
fetchCount++
|
|
836
813
|
await sleep(10)
|
|
837
814
|
return 'data'
|
|
838
815
|
},
|
|
839
|
-
|
|
840
|
-
)
|
|
816
|
+
enabled: false,
|
|
817
|
+
}))
|
|
841
818
|
|
|
842
819
|
createEffect(() => {
|
|
843
820
|
setActTimeout(() => {
|
|
@@ -866,11 +843,10 @@ describe('createQuery', () => {
|
|
|
866
843
|
const key = queryKey()
|
|
867
844
|
const states: CreateQueryResult<string>[] = []
|
|
868
845
|
|
|
869
|
-
|
|
870
|
-
queryClient.setQueryDefaults(key(), { queryFn: () => 'data' })
|
|
846
|
+
queryClient.setQueryDefaults(key, { queryFn: () => 'data' })
|
|
871
847
|
|
|
872
848
|
function Page() {
|
|
873
|
-
const state = createQuery<string>(key)
|
|
849
|
+
const state = createQuery<string>(() => ({ queryKey: key }))
|
|
874
850
|
createRenderEffect(() => {
|
|
875
851
|
states.push({ ...state })
|
|
876
852
|
})
|
|
@@ -890,7 +866,7 @@ describe('createQuery', () => {
|
|
|
890
866
|
expect(states[1]).toMatchObject({ data: 'data' })
|
|
891
867
|
})
|
|
892
868
|
|
|
893
|
-
it('should pick up a query when re-mounting with
|
|
869
|
+
it('should pick up a query when re-mounting with gcTime 0', async () => {
|
|
894
870
|
const key = queryKey()
|
|
895
871
|
const states: CreateQueryResult<string>[] = []
|
|
896
872
|
|
|
@@ -913,17 +889,14 @@ describe('createQuery', () => {
|
|
|
913
889
|
}
|
|
914
890
|
|
|
915
891
|
function Component({ value }: { value: string }) {
|
|
916
|
-
const state = createQuery(
|
|
917
|
-
key,
|
|
918
|
-
async () => {
|
|
892
|
+
const state = createQuery(() => ({
|
|
893
|
+
queryKey: key,
|
|
894
|
+
queryFn: async () => {
|
|
919
895
|
await sleep(10)
|
|
920
896
|
return 'data: ' + value
|
|
921
897
|
},
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
notifyOnChangeProps: 'all',
|
|
925
|
-
},
|
|
926
|
-
)
|
|
898
|
+
gcTime: 0,
|
|
899
|
+
}))
|
|
927
900
|
createRenderEffect(() => {
|
|
928
901
|
states.push({ ...state })
|
|
929
902
|
})
|
|
@@ -949,64 +922,43 @@ describe('createQuery', () => {
|
|
|
949
922
|
expect(states.length).toBe(4)
|
|
950
923
|
// First load
|
|
951
924
|
expect(states[0]).toMatchObject({
|
|
952
|
-
|
|
925
|
+
isPending: true,
|
|
953
926
|
isSuccess: false,
|
|
954
927
|
isFetching: true,
|
|
955
928
|
})
|
|
956
929
|
// First success
|
|
957
930
|
expect(states[1]).toMatchObject({
|
|
958
|
-
|
|
931
|
+
isPending: false,
|
|
959
932
|
isSuccess: true,
|
|
960
933
|
isFetching: false,
|
|
961
934
|
})
|
|
962
935
|
// Switch, goes to fetching
|
|
963
936
|
expect(states[2]).toMatchObject({
|
|
964
|
-
|
|
937
|
+
isPending: false,
|
|
965
938
|
isSuccess: true,
|
|
966
939
|
isFetching: true,
|
|
967
940
|
})
|
|
968
941
|
// Second success
|
|
969
942
|
expect(states[3]).toMatchObject({
|
|
970
|
-
|
|
943
|
+
isPending: false,
|
|
971
944
|
isSuccess: true,
|
|
972
945
|
isFetching: false,
|
|
973
946
|
})
|
|
974
947
|
})
|
|
975
948
|
|
|
976
|
-
it
|
|
949
|
+
it('should fetch when refetchOnMount is false and nothing has been fetched yet', async () => {
|
|
977
950
|
const key = queryKey()
|
|
978
951
|
const states: CreateQueryResult<string>[] = []
|
|
979
952
|
|
|
980
953
|
function Page() {
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
async () => {
|
|
987
|
-
await sleep(5)
|
|
988
|
-
return 'data'
|
|
989
|
-
},
|
|
990
|
-
{
|
|
991
|
-
cacheTime: 0,
|
|
992
|
-
notifyOnChangeProps: 'all',
|
|
993
|
-
},
|
|
994
|
-
)
|
|
995
|
-
|
|
954
|
+
const state = createQuery(() => ({
|
|
955
|
+
queryKey: key,
|
|
956
|
+
queryFn: () => 'test',
|
|
957
|
+
refetchOnMount: false,
|
|
958
|
+
}))
|
|
996
959
|
createRenderEffect(() => {
|
|
997
960
|
states.push({ ...state })
|
|
998
961
|
})
|
|
999
|
-
|
|
1000
|
-
const { remove } = state
|
|
1001
|
-
|
|
1002
|
-
//@ts-expect-error skip this test
|
|
1003
|
-
NotReact.useEffect(() => {
|
|
1004
|
-
setActTimeout(() => {
|
|
1005
|
-
remove()
|
|
1006
|
-
rerender({})
|
|
1007
|
-
}, 20)
|
|
1008
|
-
}, [remove])
|
|
1009
|
-
|
|
1010
962
|
return null
|
|
1011
963
|
}
|
|
1012
964
|
|
|
@@ -1016,29 +968,25 @@ describe('createQuery', () => {
|
|
|
1016
968
|
</QueryClientProvider>
|
|
1017
969
|
))
|
|
1018
970
|
|
|
1019
|
-
await sleep(
|
|
971
|
+
await sleep(10)
|
|
1020
972
|
|
|
1021
|
-
expect(states.length).toBe(
|
|
1022
|
-
|
|
1023
|
-
expect(states[
|
|
1024
|
-
// First success
|
|
1025
|
-
expect(states[1]).toMatchObject({ isLoading: false, isSuccess: true })
|
|
1026
|
-
// Remove
|
|
1027
|
-
expect(states[2]).toMatchObject({ isLoading: true, isSuccess: false })
|
|
1028
|
-
// Hook state update
|
|
1029
|
-
expect(states[3]).toMatchObject({ isLoading: true, isSuccess: false })
|
|
1030
|
-
// Second success
|
|
1031
|
-
expect(states[4]).toMatchObject({ isLoading: false, isSuccess: true })
|
|
973
|
+
expect(states.length).toBe(2)
|
|
974
|
+
expect(states[0]).toMatchObject({ data: undefined })
|
|
975
|
+
expect(states[1]).toMatchObject({ data: 'test' })
|
|
1032
976
|
})
|
|
1033
977
|
|
|
1034
|
-
it('should fetch when refetchOnMount is false and
|
|
978
|
+
it('should not fetch when refetchOnMount is false and data has been fetched already', async () => {
|
|
1035
979
|
const key = queryKey()
|
|
1036
980
|
const states: CreateQueryResult<string>[] = []
|
|
1037
981
|
|
|
982
|
+
queryClient.setQueryData(key, 'prefetched')
|
|
983
|
+
|
|
1038
984
|
function Page() {
|
|
1039
|
-
const state = createQuery(
|
|
985
|
+
const state = createQuery(() => ({
|
|
986
|
+
queryKey: key,
|
|
987
|
+
queryFn: () => 'test',
|
|
1040
988
|
refetchOnMount: false,
|
|
1041
|
-
})
|
|
989
|
+
}))
|
|
1042
990
|
createRenderEffect(() => {
|
|
1043
991
|
states.push({ ...state })
|
|
1044
992
|
})
|
|
@@ -1053,21 +1001,20 @@ describe('createQuery', () => {
|
|
|
1053
1001
|
|
|
1054
1002
|
await sleep(10)
|
|
1055
1003
|
|
|
1056
|
-
expect(states.length).toBe(
|
|
1057
|
-
expect(states[0]).toMatchObject({ data:
|
|
1058
|
-
expect(states[1]).toMatchObject({ data: 'test' })
|
|
1004
|
+
expect(states.length).toBe(1)
|
|
1005
|
+
expect(states[0]).toMatchObject({ data: 'prefetched' })
|
|
1059
1006
|
})
|
|
1060
1007
|
|
|
1061
|
-
it('should
|
|
1008
|
+
it('should be able to select a part of the data with select', async () => {
|
|
1062
1009
|
const key = queryKey()
|
|
1063
1010
|
const states: CreateQueryResult<string>[] = []
|
|
1064
1011
|
|
|
1065
|
-
queryClient.setQueryData(key(), 'prefetched')
|
|
1066
|
-
|
|
1067
1012
|
function Page() {
|
|
1068
|
-
const state = createQuery(
|
|
1069
|
-
|
|
1070
|
-
|
|
1013
|
+
const state = createQuery(() => ({
|
|
1014
|
+
queryKey: key,
|
|
1015
|
+
queryFn: () => ({ name: 'test' }),
|
|
1016
|
+
select: (data) => data.name,
|
|
1017
|
+
}))
|
|
1071
1018
|
createRenderEffect(() => {
|
|
1072
1019
|
states.push({ ...state })
|
|
1073
1020
|
})
|
|
@@ -1082,18 +1029,21 @@ describe('createQuery', () => {
|
|
|
1082
1029
|
|
|
1083
1030
|
await sleep(10)
|
|
1084
1031
|
|
|
1085
|
-
expect(states.length).toBe(
|
|
1086
|
-
expect(states[0]).toMatchObject({ data:
|
|
1032
|
+
expect(states.length).toBe(2)
|
|
1033
|
+
expect(states[0]).toMatchObject({ data: undefined })
|
|
1034
|
+
expect(states[1]).toMatchObject({ data: 'test' })
|
|
1087
1035
|
})
|
|
1088
1036
|
|
|
1089
|
-
it('should be able to select a part of the data with select', async () => {
|
|
1037
|
+
it('should be able to select a part of the data with select in object syntax', async () => {
|
|
1090
1038
|
const key = queryKey()
|
|
1091
1039
|
const states: CreateQueryResult<string>[] = []
|
|
1092
1040
|
|
|
1093
1041
|
function Page() {
|
|
1094
|
-
const state = createQuery(
|
|
1042
|
+
const state = createQuery(() => ({
|
|
1043
|
+
queryKey: key,
|
|
1044
|
+
queryFn: () => ({ name: 'test' }),
|
|
1095
1045
|
select: (data) => data.name,
|
|
1096
|
-
})
|
|
1046
|
+
}))
|
|
1097
1047
|
createRenderEffect(() => {
|
|
1098
1048
|
states.push({ ...state })
|
|
1099
1049
|
})
|
|
@@ -1118,11 +1068,11 @@ describe('createQuery', () => {
|
|
|
1118
1068
|
const states: CreateQueryResult<string>[] = []
|
|
1119
1069
|
|
|
1120
1070
|
function Page() {
|
|
1121
|
-
const state = createQuery({
|
|
1071
|
+
const state = createQuery(() => ({
|
|
1122
1072
|
queryKey: key,
|
|
1123
1073
|
queryFn: () => ({ name: 'test' }),
|
|
1124
1074
|
select: (data) => data.name,
|
|
1125
|
-
})
|
|
1075
|
+
}))
|
|
1126
1076
|
createRenderEffect(() => {
|
|
1127
1077
|
states.push({ ...state })
|
|
1128
1078
|
})
|
|
@@ -1147,23 +1097,26 @@ describe('createQuery', () => {
|
|
|
1147
1097
|
const states: CreateQueryResult<string>[] = []
|
|
1148
1098
|
|
|
1149
1099
|
function Page() {
|
|
1150
|
-
const state = createQuery(
|
|
1100
|
+
const state = createQuery(() => ({
|
|
1101
|
+
queryKey: key,
|
|
1102
|
+
queryFn: async () => {
|
|
1103
|
+
await sleep(10)
|
|
1104
|
+
return { name: 'test' }
|
|
1105
|
+
},
|
|
1151
1106
|
select: (data) => data.name,
|
|
1152
1107
|
notifyOnChangeProps: ['data'],
|
|
1153
|
-
})
|
|
1108
|
+
}))
|
|
1154
1109
|
|
|
1155
1110
|
createRenderEffect(() => {
|
|
1156
1111
|
states.push({ ...state })
|
|
1157
1112
|
})
|
|
1158
1113
|
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
refetch()
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
return null
|
|
1114
|
+
return (
|
|
1115
|
+
<div>
|
|
1116
|
+
data: {state.data}
|
|
1117
|
+
<button onClick={() => state.refetch()}>refetch</button>
|
|
1118
|
+
</div>
|
|
1119
|
+
)
|
|
1167
1120
|
}
|
|
1168
1121
|
|
|
1169
1122
|
render(() => (
|
|
@@ -1172,7 +1125,7 @@ describe('createQuery', () => {
|
|
|
1172
1125
|
</QueryClientProvider>
|
|
1173
1126
|
))
|
|
1174
1127
|
|
|
1175
|
-
await
|
|
1128
|
+
await waitFor(() => screen.getByText('data: test'))
|
|
1176
1129
|
|
|
1177
1130
|
expect(states.length).toBe(2)
|
|
1178
1131
|
expect(states[0]).toMatchObject({ data: undefined })
|
|
@@ -1185,11 +1138,13 @@ describe('createQuery', () => {
|
|
|
1185
1138
|
const error = new Error('Select Error')
|
|
1186
1139
|
|
|
1187
1140
|
function Page() {
|
|
1188
|
-
const state = createQuery(
|
|
1141
|
+
const state = createQuery(() => ({
|
|
1142
|
+
queryKey: key,
|
|
1143
|
+
queryFn: () => ({ name: 'test' }),
|
|
1189
1144
|
select: () => {
|
|
1190
1145
|
throw error
|
|
1191
1146
|
},
|
|
1192
|
-
})
|
|
1147
|
+
}))
|
|
1193
1148
|
createRenderEffect(() => {
|
|
1194
1149
|
states.push({ ...state })
|
|
1195
1150
|
})
|
|
@@ -1204,65 +1159,24 @@ describe('createQuery', () => {
|
|
|
1204
1159
|
|
|
1205
1160
|
await sleep(10)
|
|
1206
1161
|
|
|
1207
|
-
expect(mockLogger.error).toHaveBeenCalledWith(error)
|
|
1208
1162
|
expect(states.length).toBe(2)
|
|
1209
1163
|
|
|
1210
|
-
expect(states[0]).toMatchObject({ status: '
|
|
1164
|
+
expect(states[0]).toMatchObject({ status: 'pending', data: undefined })
|
|
1211
1165
|
expect(states[1]).toMatchObject({ status: 'error', error })
|
|
1212
1166
|
})
|
|
1213
1167
|
|
|
1214
|
-
it.skip('should not re-run a stable select when it re-renders if selector throws an error', async () => {
|
|
1215
|
-
const key = queryKey()
|
|
1216
|
-
const error = new Error('Select Error')
|
|
1217
|
-
let runs = 0
|
|
1218
|
-
|
|
1219
|
-
function Page() {
|
|
1220
|
-
//@ts-expect-error -- we skip this test, and no such thing as rerender in solid
|
|
1221
|
-
const [, rerender] = NotReact.useReducer(() => ({}), {})
|
|
1222
|
-
const state = createQuery<string, Error>(
|
|
1223
|
-
key,
|
|
1224
|
-
() => (runs === 0 ? 'test' : 'test2'),
|
|
1225
|
-
{
|
|
1226
|
-
select: () => {
|
|
1227
|
-
runs++
|
|
1228
|
-
throw error
|
|
1229
|
-
},
|
|
1230
|
-
},
|
|
1231
|
-
)
|
|
1232
|
-
return (
|
|
1233
|
-
<div>
|
|
1234
|
-
<div>error: {state.error?.message}</div>
|
|
1235
|
-
<button onClick={rerender}>rerender</button>
|
|
1236
|
-
<button onClick={() => state.refetch()}>refetch</button>
|
|
1237
|
-
</div>
|
|
1238
|
-
)
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
render(() => (
|
|
1242
|
-
<QueryClientProvider client={queryClient}>
|
|
1243
|
-
<Page />
|
|
1244
|
-
</QueryClientProvider>
|
|
1245
|
-
))
|
|
1246
|
-
|
|
1247
|
-
await waitFor(() => screen.getByText('error: Select Error'))
|
|
1248
|
-
expect(runs).toEqual(1)
|
|
1249
|
-
fireEvent.click(screen.getByRole('button', { name: 'rerender' }))
|
|
1250
|
-
await sleep(10)
|
|
1251
|
-
expect(runs).toEqual(1)
|
|
1252
|
-
fireEvent.click(screen.getByRole('button', { name: 'refetch' }))
|
|
1253
|
-
await sleep(10)
|
|
1254
|
-
expect(runs).toEqual(2)
|
|
1255
|
-
})
|
|
1256
|
-
|
|
1257
1168
|
it('should track properties and only re-render when a tracked property changes', async () => {
|
|
1258
1169
|
const key = queryKey()
|
|
1259
1170
|
const states: CreateQueryResult<string>[] = []
|
|
1260
1171
|
|
|
1261
1172
|
function Page() {
|
|
1262
|
-
const state = createQuery(
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1173
|
+
const state = createQuery(() => ({
|
|
1174
|
+
queryKey: key,
|
|
1175
|
+
queryFn: async () => {
|
|
1176
|
+
await sleep(10)
|
|
1177
|
+
return 'test'
|
|
1178
|
+
},
|
|
1179
|
+
}))
|
|
1266
1180
|
|
|
1267
1181
|
createRenderEffect(() => {
|
|
1268
1182
|
states.push({ ...state })
|
|
@@ -1291,129 +1205,40 @@ describe('createQuery', () => {
|
|
|
1291
1205
|
</QueryClientProvider>
|
|
1292
1206
|
))
|
|
1293
1207
|
|
|
1294
|
-
await waitFor(() => screen.getByText('test'))
|
|
1295
|
-
|
|
1296
|
-
expect(states.length).toBe(2)
|
|
1297
|
-
expect(states[0]).toMatchObject({ data: undefined })
|
|
1298
|
-
expect(states[1]).toMatchObject({ data: 'test' })
|
|
1299
|
-
})
|
|
1300
|
-
|
|
1301
|
-
it('should always re-render if we are tracking props but not using any', async () => {
|
|
1302
|
-
const key = queryKey()
|
|
1303
|
-
let renderCount = 0
|
|
1304
|
-
const states: CreateQueryResult<string>[] = []
|
|
1305
|
-
|
|
1306
|
-
function Page() {
|
|
1307
|
-
const state = createQuery(key, () => 'test')
|
|
1308
|
-
|
|
1309
|
-
createRenderEffect(() => {
|
|
1310
|
-
states.push({ ...state })
|
|
1311
|
-
})
|
|
1312
|
-
|
|
1313
|
-
createEffect(
|
|
1314
|
-
on(
|
|
1315
|
-
() => ({ ...state }),
|
|
1316
|
-
() => {
|
|
1317
|
-
renderCount++
|
|
1318
|
-
},
|
|
1319
|
-
),
|
|
1320
|
-
)
|
|
1321
|
-
|
|
1322
|
-
return (
|
|
1323
|
-
<div>
|
|
1324
|
-
<h1>hello</h1>
|
|
1325
|
-
</div>
|
|
1326
|
-
)
|
|
1327
|
-
}
|
|
1328
|
-
|
|
1329
|
-
render(() => (
|
|
1330
|
-
<QueryClientProvider client={queryClient}>
|
|
1331
|
-
<Page />
|
|
1332
|
-
</QueryClientProvider>
|
|
1333
|
-
))
|
|
1334
|
-
|
|
1335
|
-
await sleep(10)
|
|
1336
|
-
expect(renderCount).toBe(2)
|
|
1337
|
-
expect(states.length).toBe(2)
|
|
1338
|
-
expect(states[0]).toMatchObject({ data: undefined })
|
|
1339
|
-
expect(states[1]).toMatchObject({ data: 'test' })
|
|
1340
|
-
})
|
|
1341
|
-
|
|
1342
|
-
it.skip('should be able to remove a query', async () => {
|
|
1343
|
-
const key = queryKey()
|
|
1344
|
-
const states: CreateQueryResult<number>[] = []
|
|
1345
|
-
let count = 0
|
|
1346
|
-
|
|
1347
|
-
function Page() {
|
|
1348
|
-
//@ts-expect-error -- we skip this test, and no such thing as rerender in solid
|
|
1349
|
-
const [, rerender] = NotReact.useState({})
|
|
1350
|
-
const state = createQuery(key, () => ++count, {
|
|
1351
|
-
notifyOnChangeProps: 'all',
|
|
1352
|
-
})
|
|
1353
|
-
|
|
1354
|
-
createRenderEffect(() => {
|
|
1355
|
-
states.push({ ...state })
|
|
1356
|
-
})
|
|
1357
|
-
|
|
1358
|
-
const { remove } = state
|
|
1359
|
-
|
|
1360
|
-
return (
|
|
1361
|
-
<div>
|
|
1362
|
-
<button onClick={() => remove()}>remove</button>
|
|
1363
|
-
<button onClick={() => rerender({})}>rerender</button>
|
|
1364
|
-
data: {state.data ?? 'null'}
|
|
1365
|
-
</div>
|
|
1366
|
-
)
|
|
1367
|
-
}
|
|
1368
|
-
|
|
1369
|
-
render(() => (
|
|
1370
|
-
<QueryClientProvider client={queryClient}>
|
|
1371
|
-
<Page />
|
|
1372
|
-
</QueryClientProvider>
|
|
1373
|
-
))
|
|
1374
|
-
|
|
1375
|
-
await waitFor(() => screen.getByText('data: 1'))
|
|
1376
|
-
fireEvent.click(screen.getByRole('button', { name: /remove/i }))
|
|
1377
|
-
|
|
1378
|
-
await sleep(20)
|
|
1379
|
-
fireEvent.click(screen.getByRole('button', { name: /rerender/i }))
|
|
1380
|
-
await waitFor(() => screen.getByText('data: 2'))
|
|
1208
|
+
await waitFor(() => screen.getByText('test'))
|
|
1381
1209
|
|
|
1382
|
-
expect(states.length).toBe(
|
|
1383
|
-
|
|
1384
|
-
expect(states[
|
|
1385
|
-
// Fetched
|
|
1386
|
-
expect(states[1]).toMatchObject({ status: 'success', data: 1 })
|
|
1387
|
-
// Remove + Hook state update, batched
|
|
1388
|
-
expect(states[2]).toMatchObject({ status: 'loading', data: undefined })
|
|
1389
|
-
// Fetched
|
|
1390
|
-
expect(states[3]).toMatchObject({ status: 'success', data: 2 })
|
|
1210
|
+
expect(states.length).toBe(2)
|
|
1211
|
+
expect(states[0]).toMatchObject({ data: undefined })
|
|
1212
|
+
expect(states[1]).toMatchObject({ data: 'test' })
|
|
1391
1213
|
})
|
|
1392
1214
|
|
|
1393
|
-
it('should
|
|
1215
|
+
it('should always re-render if we are tracking props but not using any', async () => {
|
|
1394
1216
|
const key = queryKey()
|
|
1395
|
-
|
|
1396
|
-
|
|
1217
|
+
let renderCount = 0
|
|
1218
|
+
const states: CreateQueryResult<string>[] = []
|
|
1397
1219
|
|
|
1398
1220
|
function Page() {
|
|
1399
|
-
const state = createQuery(
|
|
1400
|
-
key,
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
return ++count
|
|
1404
|
-
},
|
|
1405
|
-
{ notifyOnChangeProps: 'all' },
|
|
1406
|
-
)
|
|
1221
|
+
const state = createQuery(() => ({
|
|
1222
|
+
queryKey: key,
|
|
1223
|
+
queryFn: () => 'test',
|
|
1224
|
+
}))
|
|
1407
1225
|
|
|
1408
1226
|
createRenderEffect(() => {
|
|
1409
1227
|
states.push({ ...state })
|
|
1410
1228
|
})
|
|
1411
1229
|
|
|
1230
|
+
createEffect(
|
|
1231
|
+
on(
|
|
1232
|
+
() => ({ ...state }),
|
|
1233
|
+
() => {
|
|
1234
|
+
renderCount++
|
|
1235
|
+
},
|
|
1236
|
+
),
|
|
1237
|
+
)
|
|
1238
|
+
|
|
1412
1239
|
return (
|
|
1413
1240
|
<div>
|
|
1414
|
-
<
|
|
1415
|
-
<button onClick={() => state.refetch()}>refetch</button>
|
|
1416
|
-
data: {state.data ?? 'null'}
|
|
1241
|
+
<h1>hello</h1>
|
|
1417
1242
|
</div>
|
|
1418
1243
|
)
|
|
1419
1244
|
}
|
|
@@ -1424,27 +1249,15 @@ describe('createQuery', () => {
|
|
|
1424
1249
|
</QueryClientProvider>
|
|
1425
1250
|
))
|
|
1426
1251
|
|
|
1427
|
-
await
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
await waitFor(() => screen.getByText('data: 2'))
|
|
1433
|
-
|
|
1434
|
-
expect(states.length).toBe(4)
|
|
1435
|
-
// Initial
|
|
1436
|
-
expect(states[0]).toMatchObject({ data: undefined, dataUpdatedAt: 0 })
|
|
1437
|
-
// Fetched
|
|
1438
|
-
expect(states[1]).toMatchObject({ data: 1 })
|
|
1439
|
-
// Switch
|
|
1440
|
-
expect(states[2]).toMatchObject({ data: undefined, dataUpdatedAt: 0 })
|
|
1441
|
-
// Fetched
|
|
1442
|
-
expect(states[3]).toMatchObject({ data: 2 })
|
|
1252
|
+
await sleep(10)
|
|
1253
|
+
expect(renderCount).toBe(2)
|
|
1254
|
+
expect(states.length).toBe(2)
|
|
1255
|
+
expect(states[0]).toMatchObject({ data: undefined })
|
|
1256
|
+
expect(states[1]).toMatchObject({ data: 'test' })
|
|
1443
1257
|
})
|
|
1444
1258
|
|
|
1445
1259
|
it('should share equal data structures between query results', async () => {
|
|
1446
1260
|
const key = queryKey()
|
|
1447
|
-
|
|
1448
1261
|
const result1 = [
|
|
1449
1262
|
{ id: '1', done: false },
|
|
1450
1263
|
{ id: '2', done: false },
|
|
@@ -1460,15 +1273,15 @@ describe('createQuery', () => {
|
|
|
1460
1273
|
let count = 0
|
|
1461
1274
|
|
|
1462
1275
|
function Page() {
|
|
1463
|
-
const state = createQuery(
|
|
1464
|
-
key,
|
|
1465
|
-
async () => {
|
|
1276
|
+
const state = createQuery(() => ({
|
|
1277
|
+
queryKey: key,
|
|
1278
|
+
queryFn: async () => {
|
|
1466
1279
|
await sleep(10)
|
|
1467
1280
|
count++
|
|
1468
1281
|
return count === 1 ? result1 : result2
|
|
1469
1282
|
},
|
|
1470
|
-
|
|
1471
|
-
)
|
|
1283
|
+
notifyOnChangeProps: 'all',
|
|
1284
|
+
}))
|
|
1472
1285
|
|
|
1473
1286
|
createRenderEffect(() => {
|
|
1474
1287
|
states.push({ ...state })
|
|
@@ -1518,20 +1331,18 @@ describe('createQuery', () => {
|
|
|
1518
1331
|
const key = queryKey()
|
|
1519
1332
|
const results: CreateQueryResult<string>[] = []
|
|
1520
1333
|
|
|
1521
|
-
queryClient.setQueryData(key
|
|
1334
|
+
queryClient.setQueryData(key, 'set')
|
|
1522
1335
|
|
|
1523
1336
|
function Page() {
|
|
1524
|
-
const result = createQuery(
|
|
1525
|
-
key,
|
|
1526
|
-
async () => {
|
|
1337
|
+
const result = createQuery(() => ({
|
|
1338
|
+
queryKey: key,
|
|
1339
|
+
queryFn: async () => {
|
|
1527
1340
|
await sleep(10)
|
|
1528
1341
|
return 'fetched'
|
|
1529
1342
|
},
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
},
|
|
1534
|
-
)
|
|
1343
|
+
initialData: 'initial',
|
|
1344
|
+
staleTime: Infinity,
|
|
1345
|
+
}))
|
|
1535
1346
|
|
|
1536
1347
|
createRenderEffect(() => {
|
|
1537
1348
|
results.push({ ...result })
|
|
@@ -1540,7 +1351,7 @@ describe('createQuery', () => {
|
|
|
1540
1351
|
return (
|
|
1541
1352
|
<div>
|
|
1542
1353
|
<div>isFetching: {result.isFetching}</div>
|
|
1543
|
-
<button onClick={() => queryClient.refetchQueries(key
|
|
1354
|
+
<button onClick={() => queryClient.refetchQueries({ queryKey: key })}>
|
|
1544
1355
|
refetch
|
|
1545
1356
|
</button>
|
|
1546
1357
|
data: {result.data}
|
|
@@ -1571,15 +1382,15 @@ describe('createQuery', () => {
|
|
|
1571
1382
|
let count = 0
|
|
1572
1383
|
|
|
1573
1384
|
function Page() {
|
|
1574
|
-
const state = createQuery(
|
|
1575
|
-
key,
|
|
1576
|
-
async () => {
|
|
1385
|
+
const state = createQuery(() => ({
|
|
1386
|
+
queryKey: key,
|
|
1387
|
+
queryFn: async () => {
|
|
1577
1388
|
await sleep(10)
|
|
1578
1389
|
count++
|
|
1579
1390
|
return count
|
|
1580
1391
|
},
|
|
1581
|
-
|
|
1582
|
-
)
|
|
1392
|
+
staleTime: Infinity,
|
|
1393
|
+
}))
|
|
1583
1394
|
|
|
1584
1395
|
createEffect(() => {
|
|
1585
1396
|
states.push({ ...state })
|
|
@@ -1587,7 +1398,9 @@ describe('createQuery', () => {
|
|
|
1587
1398
|
|
|
1588
1399
|
return (
|
|
1589
1400
|
<div>
|
|
1590
|
-
<button
|
|
1401
|
+
<button
|
|
1402
|
+
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
1403
|
+
>
|
|
1591
1404
|
invalidate
|
|
1592
1405
|
</button>
|
|
1593
1406
|
data: {state.data}
|
|
@@ -1643,15 +1456,15 @@ describe('createQuery', () => {
|
|
|
1643
1456
|
let count = 0
|
|
1644
1457
|
|
|
1645
1458
|
function Page() {
|
|
1646
|
-
const state = createQuery(
|
|
1647
|
-
key,
|
|
1648
|
-
async () => {
|
|
1459
|
+
const state = createQuery(() => ({
|
|
1460
|
+
queryKey: key,
|
|
1461
|
+
queryFn: async () => {
|
|
1649
1462
|
await sleep(10)
|
|
1650
1463
|
count++
|
|
1651
1464
|
return count
|
|
1652
1465
|
},
|
|
1653
|
-
|
|
1654
|
-
)
|
|
1466
|
+
enabled: false,
|
|
1467
|
+
}))
|
|
1655
1468
|
|
|
1656
1469
|
createRenderEffect(() => {
|
|
1657
1470
|
states.push({ ...state })
|
|
@@ -1659,7 +1472,7 @@ describe('createQuery', () => {
|
|
|
1659
1472
|
|
|
1660
1473
|
createEffect(() => {
|
|
1661
1474
|
setActTimeout(() => {
|
|
1662
|
-
queryClient.refetchQueries({ queryKey: key
|
|
1475
|
+
queryClient.refetchQueries({ queryKey: key })
|
|
1663
1476
|
}, 20)
|
|
1664
1477
|
})
|
|
1665
1478
|
|
|
@@ -1689,15 +1502,15 @@ describe('createQuery', () => {
|
|
|
1689
1502
|
let count = 0
|
|
1690
1503
|
|
|
1691
1504
|
function Page() {
|
|
1692
|
-
const state = createQuery(
|
|
1693
|
-
key,
|
|
1694
|
-
async () => {
|
|
1505
|
+
const state = createQuery(() => ({
|
|
1506
|
+
queryKey: key,
|
|
1507
|
+
queryFn: async () => {
|
|
1695
1508
|
await sleep(10)
|
|
1696
1509
|
count++
|
|
1697
1510
|
return count
|
|
1698
1511
|
},
|
|
1699
|
-
|
|
1700
|
-
)
|
|
1512
|
+
enabled: false,
|
|
1513
|
+
}))
|
|
1701
1514
|
|
|
1702
1515
|
createRenderEffect(() => {
|
|
1703
1516
|
states.push({ ...state })
|
|
@@ -1705,7 +1518,7 @@ describe('createQuery', () => {
|
|
|
1705
1518
|
|
|
1706
1519
|
createEffect(() => {
|
|
1707
1520
|
setActTimeout(() => {
|
|
1708
|
-
queryClient.invalidateQueries(key
|
|
1521
|
+
queryClient.invalidateQueries({ queryKey: key })
|
|
1709
1522
|
}, 20)
|
|
1710
1523
|
})
|
|
1711
1524
|
|
|
@@ -1736,18 +1549,14 @@ describe('createQuery', () => {
|
|
|
1736
1549
|
function Page() {
|
|
1737
1550
|
const [count, setCount] = createSignal(0)
|
|
1738
1551
|
|
|
1739
|
-
const state = createQuery(
|
|
1740
|
-
|
|
1741
|
-
async () => {
|
|
1552
|
+
const state = createQuery(() => ({
|
|
1553
|
+
queryKey: [key, count()],
|
|
1554
|
+
queryFn: async () => {
|
|
1742
1555
|
await sleep(5)
|
|
1743
1556
|
return count()
|
|
1744
1557
|
},
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
return count() === 0
|
|
1748
|
-
},
|
|
1749
|
-
},
|
|
1750
|
-
)
|
|
1558
|
+
enabled: count() === 0,
|
|
1559
|
+
}))
|
|
1751
1560
|
|
|
1752
1561
|
createRenderEffect(() => {
|
|
1753
1562
|
states.push({ ...state })
|
|
@@ -1774,7 +1583,6 @@ describe('createQuery', () => {
|
|
|
1774
1583
|
|
|
1775
1584
|
// Fetch query
|
|
1776
1585
|
expect(states[0]).toMatchObject({
|
|
1777
|
-
data: undefined,
|
|
1778
1586
|
isFetching: true,
|
|
1779
1587
|
isSuccess: false,
|
|
1780
1588
|
})
|
|
@@ -1786,27 +1594,26 @@ describe('createQuery', () => {
|
|
|
1786
1594
|
})
|
|
1787
1595
|
// Switch to disabled query
|
|
1788
1596
|
expect(states[2]).toMatchObject({
|
|
1789
|
-
data: undefined,
|
|
1790
1597
|
isFetching: false,
|
|
1791
1598
|
isSuccess: false,
|
|
1792
1599
|
})
|
|
1793
1600
|
})
|
|
1794
1601
|
|
|
1795
|
-
it('should keep the previous data when
|
|
1602
|
+
it('should keep the previous data when placeholderData is set', async () => {
|
|
1796
1603
|
const key = queryKey()
|
|
1797
1604
|
const states: CreateQueryResult<number>[] = []
|
|
1798
1605
|
|
|
1799
1606
|
function Page() {
|
|
1800
1607
|
const [count, setCount] = createSignal(0)
|
|
1801
1608
|
|
|
1802
|
-
const state = createQuery(
|
|
1803
|
-
|
|
1804
|
-
async () => {
|
|
1609
|
+
const state = createQuery(() => ({
|
|
1610
|
+
queryKey: [key, count()],
|
|
1611
|
+
queryFn: async () => {
|
|
1805
1612
|
await sleep(10)
|
|
1806
1613
|
return count()
|
|
1807
1614
|
},
|
|
1808
|
-
|
|
1809
|
-
)
|
|
1615
|
+
placeholderData: keepPreviousData,
|
|
1616
|
+
}))
|
|
1810
1617
|
|
|
1811
1618
|
createRenderEffect(() => {
|
|
1812
1619
|
states.push({ ...state })
|
|
@@ -1834,160 +1641,47 @@ describe('createQuery', () => {
|
|
|
1834
1641
|
data: undefined,
|
|
1835
1642
|
isFetching: true,
|
|
1836
1643
|
isSuccess: false,
|
|
1837
|
-
|
|
1644
|
+
isPlaceholderData: false,
|
|
1838
1645
|
})
|
|
1839
1646
|
// Fetched
|
|
1840
1647
|
expect(states[1]).toMatchObject({
|
|
1841
1648
|
data: 0,
|
|
1842
1649
|
isFetching: false,
|
|
1843
1650
|
isSuccess: true,
|
|
1844
|
-
|
|
1651
|
+
isPlaceholderData: false,
|
|
1845
1652
|
})
|
|
1846
1653
|
// Set state
|
|
1847
1654
|
expect(states[2]).toMatchObject({
|
|
1848
1655
|
data: 0,
|
|
1849
1656
|
isFetching: true,
|
|
1850
1657
|
isSuccess: true,
|
|
1851
|
-
|
|
1658
|
+
isPlaceholderData: true,
|
|
1852
1659
|
})
|
|
1853
1660
|
// New data
|
|
1854
1661
|
expect(states[3]).toMatchObject({
|
|
1855
1662
|
data: 1,
|
|
1856
1663
|
isFetching: false,
|
|
1857
1664
|
isSuccess: true,
|
|
1858
|
-
|
|
1859
|
-
})
|
|
1860
|
-
})
|
|
1861
|
-
|
|
1862
|
-
// this test relies on rerenders which don't exist in solid
|
|
1863
|
-
it.skip('should transition to error state when keepPreviousData is set', async () => {
|
|
1864
|
-
const key = queryKey()
|
|
1865
|
-
const states: CreateQueryResult<number>[] = []
|
|
1866
|
-
|
|
1867
|
-
function Page(props: { count: number }) {
|
|
1868
|
-
const state = createQuery<number, Error>(
|
|
1869
|
-
() => [key(), props.count],
|
|
1870
|
-
async () => {
|
|
1871
|
-
await sleep(10)
|
|
1872
|
-
if (props.count === 2) {
|
|
1873
|
-
throw new Error('Error test')
|
|
1874
|
-
}
|
|
1875
|
-
return Promise.resolve(props.count)
|
|
1876
|
-
},
|
|
1877
|
-
{
|
|
1878
|
-
retry: false,
|
|
1879
|
-
keepPreviousData: true,
|
|
1880
|
-
},
|
|
1881
|
-
)
|
|
1882
|
-
|
|
1883
|
-
createRenderEffect(() => {
|
|
1884
|
-
states.push({ ...state })
|
|
1885
|
-
})
|
|
1886
|
-
|
|
1887
|
-
return (
|
|
1888
|
-
<div>
|
|
1889
|
-
<h1>data: {state.data}</h1>
|
|
1890
|
-
<h2>error: {state.error?.message}</h2>
|
|
1891
|
-
<p>previous data: {state.isPreviousData}</p>
|
|
1892
|
-
</div>
|
|
1893
|
-
)
|
|
1894
|
-
}
|
|
1895
|
-
|
|
1896
|
-
render(() => (
|
|
1897
|
-
<QueryClientProvider client={queryClient}>
|
|
1898
|
-
<Page count={0} />
|
|
1899
|
-
</QueryClientProvider>
|
|
1900
|
-
))
|
|
1901
|
-
await waitFor(() => screen.getByText('data: 0'))
|
|
1902
|
-
// @ts-expect-error we skip this test and rerenders don't exist in solid
|
|
1903
|
-
act(() => screen.rerender(<Page count={1} />))
|
|
1904
|
-
await waitFor(() => screen.getByText('data: 1'))
|
|
1905
|
-
// @ts-expect-error we skip this test and rerenders don't exist in solid
|
|
1906
|
-
act(() => screen.rerender(<Page count={2} />))
|
|
1907
|
-
await waitFor(() => screen.getByText('error: Error test'))
|
|
1908
|
-
|
|
1909
|
-
await waitFor(() => expect(states.length).toBe(8))
|
|
1910
|
-
// Initial
|
|
1911
|
-
expect(states[0]).toMatchObject({
|
|
1912
|
-
data: undefined,
|
|
1913
|
-
isFetching: true,
|
|
1914
|
-
status: 'loading',
|
|
1915
|
-
error: null,
|
|
1916
|
-
isPreviousData: false,
|
|
1917
|
-
})
|
|
1918
|
-
// Fetched
|
|
1919
|
-
expect(states[1]).toMatchObject({
|
|
1920
|
-
data: 0,
|
|
1921
|
-
isFetching: false,
|
|
1922
|
-
status: 'success',
|
|
1923
|
-
error: null,
|
|
1924
|
-
isPreviousData: false,
|
|
1925
|
-
})
|
|
1926
|
-
// rerender Page 1
|
|
1927
|
-
expect(states[2]).toMatchObject({
|
|
1928
|
-
data: 0,
|
|
1929
|
-
isFetching: true,
|
|
1930
|
-
status: 'success',
|
|
1931
|
-
error: null,
|
|
1932
|
-
isPreviousData: true,
|
|
1933
|
-
})
|
|
1934
|
-
// Hook state update
|
|
1935
|
-
expect(states[3]).toMatchObject({
|
|
1936
|
-
data: 0,
|
|
1937
|
-
isFetching: true,
|
|
1938
|
-
status: 'success',
|
|
1939
|
-
error: null,
|
|
1940
|
-
isPreviousData: true,
|
|
1941
|
-
})
|
|
1942
|
-
// New data
|
|
1943
|
-
expect(states[4]).toMatchObject({
|
|
1944
|
-
data: 1,
|
|
1945
|
-
isFetching: false,
|
|
1946
|
-
status: 'success',
|
|
1947
|
-
error: null,
|
|
1948
|
-
isPreviousData: false,
|
|
1949
|
-
})
|
|
1950
|
-
// rerender Page 2
|
|
1951
|
-
expect(states[5]).toMatchObject({
|
|
1952
|
-
data: 1,
|
|
1953
|
-
isFetching: true,
|
|
1954
|
-
status: 'success',
|
|
1955
|
-
error: null,
|
|
1956
|
-
isPreviousData: true,
|
|
1957
|
-
})
|
|
1958
|
-
// Hook state update again
|
|
1959
|
-
expect(states[6]).toMatchObject({
|
|
1960
|
-
data: 1,
|
|
1961
|
-
isFetching: true,
|
|
1962
|
-
status: 'success',
|
|
1963
|
-
error: null,
|
|
1964
|
-
isPreviousData: true,
|
|
1965
|
-
})
|
|
1966
|
-
// Error
|
|
1967
|
-
expect(states[7]).toMatchObject({
|
|
1968
|
-
data: undefined,
|
|
1969
|
-
isFetching: false,
|
|
1970
|
-
status: 'error',
|
|
1971
|
-
isPreviousData: false,
|
|
1665
|
+
isPlaceholderData: false,
|
|
1972
1666
|
})
|
|
1973
|
-
expect(states[7]?.error).toHaveProperty('message', 'Error test')
|
|
1974
1667
|
})
|
|
1975
1668
|
|
|
1976
|
-
it('should not show initial data from next query if
|
|
1669
|
+
it('should not show initial data from next query if placeholderData is set', async () => {
|
|
1977
1670
|
const key = queryKey()
|
|
1978
1671
|
const states: DefinedCreateQueryResult<number>[] = []
|
|
1979
1672
|
|
|
1980
1673
|
function Page() {
|
|
1981
1674
|
const [count, setCount] = createSignal(0)
|
|
1982
1675
|
|
|
1983
|
-
const state = createQuery(
|
|
1984
|
-
|
|
1985
|
-
async () => {
|
|
1676
|
+
const state = createQuery(() => ({
|
|
1677
|
+
queryKey: [key, count()],
|
|
1678
|
+
queryFn: async () => {
|
|
1986
1679
|
await sleep(10)
|
|
1987
1680
|
return count()
|
|
1988
1681
|
},
|
|
1989
|
-
|
|
1990
|
-
|
|
1682
|
+
initialData: 99,
|
|
1683
|
+
placeholderData: keepPreviousData,
|
|
1684
|
+
}))
|
|
1991
1685
|
|
|
1992
1686
|
createRenderEffect(() => {
|
|
1993
1687
|
states.push({ ...state })
|
|
@@ -2027,46 +1721,48 @@ describe('createQuery', () => {
|
|
|
2027
1721
|
data: 99,
|
|
2028
1722
|
isFetching: true,
|
|
2029
1723
|
isSuccess: true,
|
|
2030
|
-
|
|
1724
|
+
isPlaceholderData: false,
|
|
2031
1725
|
})
|
|
2032
1726
|
// Fetched
|
|
2033
1727
|
expect(states[1]).toMatchObject({
|
|
2034
1728
|
data: 0,
|
|
2035
1729
|
isFetching: false,
|
|
2036
1730
|
isSuccess: true,
|
|
2037
|
-
|
|
1731
|
+
isPlaceholderData: false,
|
|
2038
1732
|
})
|
|
2039
1733
|
// Set state
|
|
2040
1734
|
expect(states[2]).toMatchObject({
|
|
2041
1735
|
data: 99,
|
|
2042
1736
|
isFetching: true,
|
|
2043
1737
|
isSuccess: true,
|
|
2044
|
-
|
|
1738
|
+
isPlaceholderData: false,
|
|
2045
1739
|
})
|
|
2046
1740
|
// New data
|
|
2047
1741
|
expect(states[3]).toMatchObject({
|
|
2048
1742
|
data: 1,
|
|
2049
1743
|
isFetching: false,
|
|
2050
1744
|
isSuccess: true,
|
|
2051
|
-
|
|
1745
|
+
isPlaceholderData: false,
|
|
2052
1746
|
})
|
|
2053
1747
|
})
|
|
2054
1748
|
|
|
2055
|
-
it('should keep the previous data on disabled query when
|
|
1749
|
+
it('should keep the previous data on disabled query when placeholderData is set to identity function', async () => {
|
|
2056
1750
|
const key = queryKey()
|
|
2057
1751
|
const states: CreateQueryResult<number>[] = []
|
|
2058
1752
|
|
|
2059
1753
|
function Page() {
|
|
2060
1754
|
const [count, setCount] = createSignal(0)
|
|
2061
1755
|
|
|
2062
|
-
const state = createQuery(
|
|
2063
|
-
|
|
2064
|
-
async () => {
|
|
1756
|
+
const state = createQuery(() => ({
|
|
1757
|
+
queryKey: [key, count()],
|
|
1758
|
+
queryFn: async () => {
|
|
2065
1759
|
await sleep(10)
|
|
2066
1760
|
return count()
|
|
2067
1761
|
},
|
|
2068
|
-
|
|
2069
|
-
|
|
1762
|
+
enabled: false,
|
|
1763
|
+
placeholderData: keepPreviousData,
|
|
1764
|
+
notifyOnChangeProps: 'all',
|
|
1765
|
+
}))
|
|
2070
1766
|
|
|
2071
1767
|
createRenderEffect(() => {
|
|
2072
1768
|
states.push({ ...state })
|
|
@@ -2103,64 +1799,66 @@ describe('createQuery', () => {
|
|
|
2103
1799
|
data: undefined,
|
|
2104
1800
|
isFetching: false,
|
|
2105
1801
|
isSuccess: false,
|
|
2106
|
-
|
|
1802
|
+
isPlaceholderData: false,
|
|
2107
1803
|
})
|
|
2108
1804
|
// Fetching query
|
|
2109
1805
|
expect(states[1]).toMatchObject({
|
|
2110
1806
|
data: undefined,
|
|
2111
1807
|
isFetching: true,
|
|
2112
1808
|
isSuccess: false,
|
|
2113
|
-
|
|
1809
|
+
isPlaceholderData: false,
|
|
2114
1810
|
})
|
|
2115
1811
|
// Fetched query
|
|
2116
1812
|
expect(states[2]).toMatchObject({
|
|
2117
1813
|
data: 0,
|
|
2118
1814
|
isFetching: false,
|
|
2119
1815
|
isSuccess: true,
|
|
2120
|
-
|
|
1816
|
+
isPlaceholderData: false,
|
|
2121
1817
|
})
|
|
2122
1818
|
// Set state
|
|
2123
1819
|
expect(states[3]).toMatchObject({
|
|
2124
1820
|
data: 0,
|
|
2125
1821
|
isFetching: false,
|
|
2126
1822
|
isSuccess: true,
|
|
2127
|
-
|
|
1823
|
+
isPlaceholderData: true,
|
|
2128
1824
|
})
|
|
2129
1825
|
// Fetching new query
|
|
2130
1826
|
expect(states[4]).toMatchObject({
|
|
2131
1827
|
data: 0,
|
|
2132
1828
|
isFetching: true,
|
|
2133
1829
|
isSuccess: true,
|
|
2134
|
-
|
|
1830
|
+
isPlaceholderData: true,
|
|
2135
1831
|
})
|
|
2136
1832
|
// Fetched new query
|
|
2137
1833
|
expect(states[5]).toMatchObject({
|
|
2138
1834
|
data: 1,
|
|
2139
1835
|
isFetching: false,
|
|
2140
1836
|
isSuccess: true,
|
|
2141
|
-
|
|
1837
|
+
isPlaceholderData: false,
|
|
2142
1838
|
})
|
|
2143
1839
|
})
|
|
2144
1840
|
|
|
2145
|
-
it('should keep the previous data on disabled query when
|
|
1841
|
+
it('should keep the previous data on disabled query when placeholderData is set and switching query key multiple times', async () => {
|
|
2146
1842
|
const key = queryKey()
|
|
2147
1843
|
const states: CreateQueryResult<number>[] = []
|
|
2148
1844
|
|
|
2149
|
-
queryClient.setQueryData([key
|
|
1845
|
+
queryClient.setQueryData([key, 10], 10)
|
|
2150
1846
|
|
|
2151
1847
|
await sleep(10)
|
|
2152
1848
|
|
|
2153
1849
|
function Page() {
|
|
2154
1850
|
const [count, setCount] = createSignal(10)
|
|
2155
1851
|
|
|
2156
|
-
const state = createQuery(
|
|
2157
|
-
|
|
2158
|
-
async () => {
|
|
1852
|
+
const state = createQuery(() => ({
|
|
1853
|
+
queryKey: [key, count()],
|
|
1854
|
+
queryFn: async () => {
|
|
2159
1855
|
await sleep(10)
|
|
2160
1856
|
return count()
|
|
2161
1857
|
},
|
|
2162
|
-
|
|
2163
|
-
|
|
1858
|
+
enabled: false,
|
|
1859
|
+
placeholderData: keepPreviousData,
|
|
1860
|
+
notifyOnChangeProps: 'all',
|
|
1861
|
+
}))
|
|
2164
1862
|
|
|
2165
1863
|
createRenderEffect(() => {
|
|
2166
1864
|
states.push({ ...state })
|
|
@@ -2197,28 +1895,28 @@ describe('createQuery', () => {
|
|
|
2197
1895
|
data: 10,
|
|
2198
1896
|
isFetching: false,
|
|
2199
1897
|
isSuccess: true,
|
|
2200
|
-
|
|
1898
|
+
isPlaceholderData: false,
|
|
2201
1899
|
})
|
|
2202
1900
|
// Set state
|
|
2203
1901
|
expect(states[1]).toMatchObject({
|
|
2204
1902
|
data: 10,
|
|
2205
1903
|
isFetching: false,
|
|
2206
1904
|
isSuccess: true,
|
|
2207
|
-
|
|
1905
|
+
isPlaceholderData: true,
|
|
2208
1906
|
})
|
|
2209
1907
|
// Refetch
|
|
2210
1908
|
expect(states[2]).toMatchObject({
|
|
2211
1909
|
data: 10,
|
|
2212
1910
|
isFetching: true,
|
|
2213
1911
|
isSuccess: true,
|
|
2214
|
-
|
|
1912
|
+
isPlaceholderData: true,
|
|
2215
1913
|
})
|
|
2216
1914
|
// Refetch done
|
|
2217
1915
|
expect(states[3]).toMatchObject({
|
|
2218
1916
|
data: 12,
|
|
2219
1917
|
isFetching: false,
|
|
2220
1918
|
isSuccess: true,
|
|
2221
|
-
|
|
1919
|
+
isPlaceholderData: false,
|
|
2222
1920
|
})
|
|
2223
1921
|
})
|
|
2224
1922
|
|
|
@@ -2227,14 +1925,13 @@ describe('createQuery', () => {
|
|
|
2227
1925
|
const states: CreateQueryResult<number>[] = []
|
|
2228
1926
|
|
|
2229
1927
|
function FirstComponent() {
|
|
2230
|
-
const state = createQuery(
|
|
2231
|
-
key,
|
|
2232
|
-
async () => {
|
|
1928
|
+
const state = createQuery(() => ({
|
|
1929
|
+
queryKey: key,
|
|
1930
|
+
queryFn: async () => {
|
|
2233
1931
|
await sleep(10)
|
|
2234
1932
|
return 1
|
|
2235
1933
|
},
|
|
2236
|
-
|
|
2237
|
-
)
|
|
1934
|
+
}))
|
|
2238
1935
|
|
|
2239
1936
|
createRenderEffect(() => {
|
|
2240
1937
|
states.push({ ...state })
|
|
@@ -2249,7 +1946,10 @@ describe('createQuery', () => {
|
|
|
2249
1946
|
}
|
|
2250
1947
|
|
|
2251
1948
|
function SecondComponent() {
|
|
2252
|
-
createQuery(
|
|
1949
|
+
createQuery(() => ({
|
|
1950
|
+
queryKey: key,
|
|
1951
|
+
queryFn: () => 2,
|
|
1952
|
+
}))
|
|
2253
1953
|
return null
|
|
2254
1954
|
}
|
|
2255
1955
|
|
|
@@ -2293,24 +1993,25 @@ describe('createQuery', () => {
|
|
|
2293
1993
|
const states1: CreateQueryResult<string>[] = []
|
|
2294
1994
|
const states2: CreateQueryResult<string>[] = []
|
|
2295
1995
|
|
|
2296
|
-
await queryClient.prefetchQuery(
|
|
2297
|
-
|
|
2298
|
-
|
|
1996
|
+
await queryClient.prefetchQuery({
|
|
1997
|
+
queryKey: key,
|
|
1998
|
+
queryFn: async () => {
|
|
1999
|
+
await sleep(10)
|
|
2000
|
+
return 'prefetch'
|
|
2001
|
+
},
|
|
2299
2002
|
})
|
|
2300
2003
|
|
|
2301
2004
|
await sleep(20)
|
|
2302
2005
|
|
|
2303
2006
|
function FirstComponent() {
|
|
2304
|
-
const state = createQuery(
|
|
2305
|
-
key,
|
|
2306
|
-
async () => {
|
|
2007
|
+
const state = createQuery(() => ({
|
|
2008
|
+
queryKey: key,
|
|
2009
|
+
queryFn: async () => {
|
|
2307
2010
|
await sleep(10)
|
|
2308
2011
|
return 'one'
|
|
2309
2012
|
},
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
},
|
|
2313
|
-
)
|
|
2013
|
+
staleTime: 100,
|
|
2014
|
+
}))
|
|
2314
2015
|
createRenderEffect(() => {
|
|
2315
2016
|
states1.push({ ...state })
|
|
2316
2017
|
})
|
|
@@ -2318,16 +2019,14 @@ describe('createQuery', () => {
|
|
|
2318
2019
|
}
|
|
2319
2020
|
|
|
2320
2021
|
function SecondComponent() {
|
|
2321
|
-
const state = createQuery(
|
|
2322
|
-
key,
|
|
2323
|
-
async () => {
|
|
2022
|
+
const state = createQuery(() => ({
|
|
2023
|
+
queryKey: key,
|
|
2024
|
+
queryFn: async () => {
|
|
2324
2025
|
await sleep(10)
|
|
2325
2026
|
return 'two'
|
|
2326
2027
|
},
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
},
|
|
2330
|
-
)
|
|
2028
|
+
staleTime: 10,
|
|
2029
|
+
}))
|
|
2331
2030
|
createRenderEffect(() => {
|
|
2332
2031
|
states2.push({ ...state })
|
|
2333
2032
|
})
|
|
@@ -2401,9 +2100,11 @@ describe('createQuery', () => {
|
|
|
2401
2100
|
const states: CreateQueryResult<string>[] = []
|
|
2402
2101
|
|
|
2403
2102
|
function Page() {
|
|
2404
|
-
const state = createQuery(
|
|
2103
|
+
const state = createQuery(() => ({
|
|
2104
|
+
queryKey: key,
|
|
2105
|
+
queryFn: () => 'test',
|
|
2405
2106
|
staleTime: 50,
|
|
2406
|
-
})
|
|
2107
|
+
}))
|
|
2407
2108
|
createRenderEffect(() => {
|
|
2408
2109
|
states.push({ ...state })
|
|
2409
2110
|
})
|
|
@@ -2429,16 +2130,14 @@ describe('createQuery', () => {
|
|
|
2429
2130
|
const states: CreateQueryResult<string>[] = []
|
|
2430
2131
|
|
|
2431
2132
|
function Page() {
|
|
2432
|
-
const state = createQuery(
|
|
2433
|
-
key,
|
|
2434
|
-
async () => {
|
|
2133
|
+
const state = createQuery(() => ({
|
|
2134
|
+
queryKey: key,
|
|
2135
|
+
queryFn: async () => {
|
|
2435
2136
|
await sleep(5)
|
|
2436
2137
|
return 'test'
|
|
2437
2138
|
},
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
},
|
|
2441
|
-
)
|
|
2139
|
+
notifyOnChangeProps: ['data'],
|
|
2140
|
+
}))
|
|
2442
2141
|
|
|
2443
2142
|
createRenderEffect(() => {
|
|
2444
2143
|
states.push({ ...state })
|
|
@@ -2464,7 +2163,7 @@ describe('createQuery', () => {
|
|
|
2464
2163
|
expect(states.length).toBe(2)
|
|
2465
2164
|
expect(states[0]).toMatchObject({
|
|
2466
2165
|
data: undefined,
|
|
2467
|
-
status: '
|
|
2166
|
+
status: 'pending',
|
|
2468
2167
|
isFetching: true,
|
|
2469
2168
|
})
|
|
2470
2169
|
expect(states[1]).toMatchObject({
|
|
@@ -2480,15 +2179,19 @@ describe('createQuery', () => {
|
|
|
2480
2179
|
const key2 = queryKey()
|
|
2481
2180
|
|
|
2482
2181
|
function Page() {
|
|
2483
|
-
const first = createQuery(
|
|
2182
|
+
const first = createQuery(() => ({
|
|
2183
|
+
queryKey: key1,
|
|
2184
|
+
queryFn: () => 'data',
|
|
2484
2185
|
enabled: false,
|
|
2485
2186
|
initialData: 'init',
|
|
2486
|
-
})
|
|
2187
|
+
}))
|
|
2487
2188
|
|
|
2488
|
-
const second = createQuery(
|
|
2189
|
+
const second = createQuery(() => ({
|
|
2190
|
+
queryKey: key2,
|
|
2191
|
+
queryFn: () => 'data',
|
|
2489
2192
|
enabled: false,
|
|
2490
2193
|
initialData: 'init',
|
|
2491
|
-
})
|
|
2194
|
+
}))
|
|
2492
2195
|
|
|
2493
2196
|
return (
|
|
2494
2197
|
<div>
|
|
@@ -2526,8 +2229,8 @@ describe('createQuery', () => {
|
|
|
2526
2229
|
}
|
|
2527
2230
|
|
|
2528
2231
|
function Page() {
|
|
2529
|
-
createQuery(key, queryFn1)
|
|
2530
|
-
createQuery(key, queryFn2)
|
|
2232
|
+
createQuery(() => ({ queryKey: key, queryFn: queryFn1 }))
|
|
2233
|
+
createQuery(() => ({ queryKey: key, queryFn: queryFn2 }))
|
|
2531
2234
|
return null
|
|
2532
2235
|
}
|
|
2533
2236
|
|
|
@@ -2537,7 +2240,7 @@ describe('createQuery', () => {
|
|
|
2537
2240
|
</QueryClientProvider>
|
|
2538
2241
|
))
|
|
2539
2242
|
|
|
2540
|
-
expect(queryCache.find(key
|
|
2243
|
+
expect(queryCache.find({ queryKey: key })!.options.queryFn).toBe(queryFn1)
|
|
2541
2244
|
})
|
|
2542
2245
|
|
|
2543
2246
|
it('should batch re-renders', async () => {
|
|
@@ -2551,8 +2254,8 @@ describe('createQuery', () => {
|
|
|
2551
2254
|
}
|
|
2552
2255
|
|
|
2553
2256
|
function Page() {
|
|
2554
|
-
createQuery(key, queryFn)
|
|
2555
|
-
createQuery(key, queryFn)
|
|
2257
|
+
createQuery(() => ({ queryKey: key, queryFn }))
|
|
2258
|
+
createQuery(() => ({ queryKey: key, queryFn }))
|
|
2556
2259
|
renders++
|
|
2557
2260
|
return null
|
|
2558
2261
|
}
|
|
@@ -2583,16 +2286,20 @@ describe('createQuery', () => {
|
|
|
2583
2286
|
|
|
2584
2287
|
function Page() {
|
|
2585
2288
|
const [count, setCount] = createSignal(0)
|
|
2586
|
-
createQuery(
|
|
2289
|
+
createQuery(() => ({
|
|
2290
|
+
queryKey: key,
|
|
2291
|
+
queryFn,
|
|
2587
2292
|
onSuccess: () => {
|
|
2588
2293
|
setCount((x) => x + 1)
|
|
2589
2294
|
},
|
|
2590
|
-
})
|
|
2591
|
-
createQuery(
|
|
2295
|
+
}))
|
|
2296
|
+
createQuery(() => ({
|
|
2297
|
+
queryKey: key,
|
|
2298
|
+
queryFn,
|
|
2592
2299
|
onSuccess: () => {
|
|
2593
2300
|
setCount((x) => x + 1)
|
|
2594
2301
|
},
|
|
2595
|
-
})
|
|
2302
|
+
}))
|
|
2596
2303
|
|
|
2597
2304
|
createEffect(() => {
|
|
2598
2305
|
renders++
|
|
@@ -2621,10 +2328,13 @@ describe('createQuery', () => {
|
|
|
2621
2328
|
|
|
2622
2329
|
function Page() {
|
|
2623
2330
|
const [, setNewState] = createSignal('state')
|
|
2624
|
-
const state = createQuery(
|
|
2331
|
+
const state = createQuery(() => ({
|
|
2332
|
+
queryKey: key,
|
|
2333
|
+
queryFn: () => 'data',
|
|
2334
|
+
}))
|
|
2625
2335
|
createEffect(() => {
|
|
2626
2336
|
setActTimeout(() => {
|
|
2627
|
-
queryClient.setQueryData(key
|
|
2337
|
+
queryClient.setQueryData(key, 'new')
|
|
2628
2338
|
// Update with same state to make react discard the next render
|
|
2629
2339
|
setNewState('state')
|
|
2630
2340
|
}, 10)
|
|
@@ -2642,15 +2352,20 @@ describe('createQuery', () => {
|
|
|
2642
2352
|
})
|
|
2643
2353
|
|
|
2644
2354
|
// See https://github.com/tannerlinsley/react-query/issues/170
|
|
2645
|
-
it('should start with status
|
|
2355
|
+
it('should start with status pending, fetchStatus idle if enabled is false', async () => {
|
|
2646
2356
|
const key1 = queryKey()
|
|
2647
2357
|
const key2 = queryKey()
|
|
2648
2358
|
|
|
2649
2359
|
function Page() {
|
|
2650
|
-
const first = createQuery(
|
|
2360
|
+
const first = createQuery(() => ({
|
|
2361
|
+
queryKey: key1,
|
|
2362
|
+
queryFn: () => 'data',
|
|
2651
2363
|
enabled: false,
|
|
2652
|
-
})
|
|
2653
|
-
const second = createQuery(
|
|
2364
|
+
}))
|
|
2365
|
+
const second = createQuery(() => ({
|
|
2366
|
+
queryKey: key2,
|
|
2367
|
+
queryFn: () => 'data',
|
|
2368
|
+
}))
|
|
2654
2369
|
|
|
2655
2370
|
return (
|
|
2656
2371
|
<div>
|
|
@@ -2672,20 +2387,23 @@ describe('createQuery', () => {
|
|
|
2672
2387
|
|
|
2673
2388
|
// use "act" to wait for state update and prevent console warning
|
|
2674
2389
|
|
|
2675
|
-
screen.getByText('First Status:
|
|
2676
|
-
await waitFor(() => screen.getByText('Second Status:
|
|
2390
|
+
screen.getByText('First Status: pending, idle')
|
|
2391
|
+
await waitFor(() => screen.getByText('Second Status: pending, fetching'))
|
|
2677
2392
|
await waitFor(() => screen.getByText('Second Status: success, idle'))
|
|
2678
2393
|
})
|
|
2679
2394
|
|
|
2680
2395
|
// See https://github.com/tannerlinsley/react-query/issues/144
|
|
2681
|
-
it('should be in "
|
|
2396
|
+
it('should be in "pending" state by default', async () => {
|
|
2682
2397
|
const key = queryKey()
|
|
2683
2398
|
|
|
2684
2399
|
function Page() {
|
|
2685
|
-
const { status } = createQuery(
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2400
|
+
const { status } = createQuery(() => ({
|
|
2401
|
+
queryKey: key,
|
|
2402
|
+
queryFn: async () => {
|
|
2403
|
+
await sleep(10)
|
|
2404
|
+
return 'test'
|
|
2405
|
+
},
|
|
2406
|
+
}))
|
|
2689
2407
|
|
|
2690
2408
|
return <div>status: {status}</div>
|
|
2691
2409
|
}
|
|
@@ -2696,26 +2414,25 @@ describe('createQuery', () => {
|
|
|
2696
2414
|
</QueryClientProvider>
|
|
2697
2415
|
))
|
|
2698
2416
|
|
|
2699
|
-
screen.getByText('status:
|
|
2417
|
+
screen.getByText('status: pending')
|
|
2700
2418
|
})
|
|
2701
2419
|
|
|
2702
2420
|
// See https://github.com/tannerlinsley/react-query/issues/147
|
|
2703
2421
|
it('should not pass stringified variables to query function', async () => {
|
|
2704
2422
|
const key = queryKey()
|
|
2705
2423
|
const variables = { number: 5, boolean: false, object: {}, array: [] }
|
|
2706
|
-
type CustomQueryKey = readonly [
|
|
2707
|
-
const states: CreateQueryResult<CustomQueryKey>[] = []
|
|
2708
|
-
|
|
2709
|
-
// TODO(lukemurray): extract the query function to a variable queryFn
|
|
2424
|
+
type CustomQueryKey = readonly [typeof key, typeof variables]
|
|
2425
|
+
const states: CreateQueryResult<CustomQueryKey, unknown>[] = []
|
|
2710
2426
|
|
|
2711
2427
|
function Page() {
|
|
2712
|
-
const state = createQuery(
|
|
2713
|
-
|
|
2714
|
-
async (ctx) => {
|
|
2428
|
+
const state = createQuery(() => ({
|
|
2429
|
+
queryKey: [key, variables] as const,
|
|
2430
|
+
queryFn: async (ctx) => {
|
|
2715
2431
|
await sleep(10)
|
|
2716
2432
|
return ctx.queryKey
|
|
2717
2433
|
},
|
|
2718
|
-
)
|
|
2434
|
+
}))
|
|
2435
|
+
|
|
2719
2436
|
createRenderEffect(() => {
|
|
2720
2437
|
states.push({ ...state })
|
|
2721
2438
|
})
|
|
@@ -2730,7 +2447,7 @@ describe('createQuery', () => {
|
|
|
2730
2447
|
|
|
2731
2448
|
await sleep(20)
|
|
2732
2449
|
|
|
2733
|
-
expect(states[1]?.data).toEqual([key
|
|
2450
|
+
expect(states[1]?.data).toEqual([key, variables])
|
|
2734
2451
|
})
|
|
2735
2452
|
|
|
2736
2453
|
it('should not refetch query on focus when `enabled` is set to `false`', async () => {
|
|
@@ -2738,9 +2455,11 @@ describe('createQuery', () => {
|
|
|
2738
2455
|
const queryFn = jest.fn<string, unknown[]>().mockReturnValue('data')
|
|
2739
2456
|
|
|
2740
2457
|
function Page() {
|
|
2741
|
-
const { data = 'default' } = createQuery(
|
|
2458
|
+
const { data = 'default' } = createQuery(() => ({
|
|
2459
|
+
queryKey: key,
|
|
2460
|
+
queryFn,
|
|
2742
2461
|
enabled: false,
|
|
2743
|
-
})
|
|
2462
|
+
}))
|
|
2744
2463
|
|
|
2745
2464
|
return (
|
|
2746
2465
|
<div>
|
|
@@ -2757,7 +2476,7 @@ describe('createQuery', () => {
|
|
|
2757
2476
|
|
|
2758
2477
|
await waitFor(() => screen.getByText('default'))
|
|
2759
2478
|
|
|
2760
|
-
window.dispatchEvent(new
|
|
2479
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2761
2480
|
|
|
2762
2481
|
expect(queryFn).not.toHaveBeenCalled()
|
|
2763
2482
|
})
|
|
@@ -2768,10 +2487,12 @@ describe('createQuery', () => {
|
|
|
2768
2487
|
let count = 0
|
|
2769
2488
|
|
|
2770
2489
|
function Page() {
|
|
2771
|
-
const state = createQuery(
|
|
2490
|
+
const state = createQuery(() => ({
|
|
2491
|
+
queryKey: key,
|
|
2492
|
+
queryFn: () => count++,
|
|
2772
2493
|
staleTime: 0,
|
|
2773
2494
|
refetchOnWindowFocus: false,
|
|
2774
|
-
})
|
|
2495
|
+
}))
|
|
2775
2496
|
createRenderEffect(() => {
|
|
2776
2497
|
states.push({ ...state })
|
|
2777
2498
|
})
|
|
@@ -2786,7 +2507,7 @@ describe('createQuery', () => {
|
|
|
2786
2507
|
|
|
2787
2508
|
await sleep(10)
|
|
2788
2509
|
|
|
2789
|
-
window.dispatchEvent(new
|
|
2510
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2790
2511
|
|
|
2791
2512
|
await sleep(10)
|
|
2792
2513
|
|
|
@@ -2801,10 +2522,12 @@ describe('createQuery', () => {
|
|
|
2801
2522
|
let count = 0
|
|
2802
2523
|
|
|
2803
2524
|
function Page() {
|
|
2804
|
-
const state = createQuery(
|
|
2525
|
+
const state = createQuery(() => ({
|
|
2526
|
+
queryKey: key,
|
|
2527
|
+
queryFn: () => count++,
|
|
2805
2528
|
staleTime: 0,
|
|
2806
2529
|
refetchOnWindowFocus: () => false,
|
|
2807
|
-
})
|
|
2530
|
+
}))
|
|
2808
2531
|
createRenderEffect(() => {
|
|
2809
2532
|
states.push({ ...state })
|
|
2810
2533
|
})
|
|
@@ -2819,7 +2542,7 @@ describe('createQuery', () => {
|
|
|
2819
2542
|
|
|
2820
2543
|
await sleep(10)
|
|
2821
2544
|
|
|
2822
|
-
window.dispatchEvent(new
|
|
2545
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2823
2546
|
|
|
2824
2547
|
await sleep(10)
|
|
2825
2548
|
|
|
@@ -2834,10 +2557,12 @@ describe('createQuery', () => {
|
|
|
2834
2557
|
let count = 0
|
|
2835
2558
|
|
|
2836
2559
|
function Page() {
|
|
2837
|
-
const state = createQuery(
|
|
2560
|
+
const state = createQuery(() => ({
|
|
2561
|
+
queryKey: key,
|
|
2562
|
+
queryFn: () => count++,
|
|
2838
2563
|
staleTime: Infinity,
|
|
2839
2564
|
refetchOnWindowFocus: true,
|
|
2840
|
-
})
|
|
2565
|
+
}))
|
|
2841
2566
|
createRenderEffect(() => {
|
|
2842
2567
|
states.push({ ...state })
|
|
2843
2568
|
})
|
|
@@ -2852,7 +2577,7 @@ describe('createQuery', () => {
|
|
|
2852
2577
|
|
|
2853
2578
|
await sleep(10)
|
|
2854
2579
|
|
|
2855
|
-
window.dispatchEvent(new
|
|
2580
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2856
2581
|
|
|
2857
2582
|
await sleep(10)
|
|
2858
2583
|
|
|
@@ -2867,17 +2592,15 @@ describe('createQuery', () => {
|
|
|
2867
2592
|
let count = 0
|
|
2868
2593
|
|
|
2869
2594
|
function Page() {
|
|
2870
|
-
const state = createQuery(
|
|
2871
|
-
key,
|
|
2872
|
-
async () => {
|
|
2595
|
+
const state = createQuery(() => ({
|
|
2596
|
+
queryKey: key,
|
|
2597
|
+
queryFn: async () => {
|
|
2873
2598
|
await sleep(10)
|
|
2874
2599
|
return count++
|
|
2875
2600
|
},
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
},
|
|
2880
|
-
)
|
|
2601
|
+
staleTime: Infinity,
|
|
2602
|
+
refetchOnWindowFocus: 'always',
|
|
2603
|
+
}))
|
|
2881
2604
|
createRenderEffect(() => {
|
|
2882
2605
|
states.push({ ...state })
|
|
2883
2606
|
})
|
|
@@ -2892,7 +2615,7 @@ describe('createQuery', () => {
|
|
|
2892
2615
|
|
|
2893
2616
|
await sleep(20)
|
|
2894
2617
|
|
|
2895
|
-
window.dispatchEvent(new
|
|
2618
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2896
2619
|
|
|
2897
2620
|
await sleep(20)
|
|
2898
2621
|
|
|
@@ -2909,18 +2632,16 @@ describe('createQuery', () => {
|
|
|
2909
2632
|
let count = 0
|
|
2910
2633
|
|
|
2911
2634
|
function Page() {
|
|
2912
|
-
const state = createQuery(
|
|
2913
|
-
key,
|
|
2914
|
-
async () => {
|
|
2635
|
+
const state = createQuery(() => ({
|
|
2636
|
+
queryKey: key,
|
|
2637
|
+
queryFn: async () => {
|
|
2915
2638
|
await sleep(10)
|
|
2916
2639
|
return count++
|
|
2917
2640
|
},
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
},
|
|
2923
|
-
)
|
|
2641
|
+
staleTime: 0,
|
|
2642
|
+
retry: 0,
|
|
2643
|
+
refetchOnWindowFocus: (query) => (query.state.data || 0) < 1,
|
|
2644
|
+
}))
|
|
2924
2645
|
createRenderEffect(() => {
|
|
2925
2646
|
states.push({ ...state })
|
|
2926
2647
|
})
|
|
@@ -2939,7 +2660,7 @@ describe('createQuery', () => {
|
|
|
2939
2660
|
expect(states[0]).toMatchObject({ data: undefined, isFetching: true })
|
|
2940
2661
|
expect(states[1]).toMatchObject({ data: 0, isFetching: false })
|
|
2941
2662
|
|
|
2942
|
-
window.dispatchEvent(new
|
|
2663
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2943
2664
|
|
|
2944
2665
|
await screen.findByText('data: 1')
|
|
2945
2666
|
|
|
@@ -2959,13 +2680,18 @@ describe('createQuery', () => {
|
|
|
2959
2680
|
const key = queryKey()
|
|
2960
2681
|
const states: CreateQueryResult<string>[] = []
|
|
2961
2682
|
|
|
2962
|
-
await queryClient.prefetchQuery(
|
|
2683
|
+
await queryClient.prefetchQuery({
|
|
2684
|
+
queryKey: key,
|
|
2685
|
+
queryFn: () => 'prefetched',
|
|
2686
|
+
})
|
|
2963
2687
|
|
|
2964
2688
|
function Page() {
|
|
2965
|
-
const state = createQuery(
|
|
2689
|
+
const state = createQuery(() => ({
|
|
2690
|
+
queryKey: key,
|
|
2691
|
+
queryFn: () => 'data',
|
|
2966
2692
|
refetchOnMount: 'always',
|
|
2967
2693
|
staleTime: Infinity,
|
|
2968
|
-
})
|
|
2694
|
+
}))
|
|
2969
2695
|
createRenderEffect(() => {
|
|
2970
2696
|
states.push({ ...state })
|
|
2971
2697
|
})
|
|
@@ -2997,15 +2723,20 @@ describe('createQuery', () => {
|
|
|
2997
2723
|
const key = queryKey()
|
|
2998
2724
|
const states: CreateQueryResult<string>[] = []
|
|
2999
2725
|
|
|
3000
|
-
await queryClient.prefetchQuery(
|
|
2726
|
+
await queryClient.prefetchQuery({
|
|
2727
|
+
queryKey: key,
|
|
2728
|
+
queryFn: () => 'prefetched',
|
|
2729
|
+
})
|
|
3001
2730
|
|
|
3002
2731
|
await sleep(10)
|
|
3003
2732
|
|
|
3004
2733
|
function Page() {
|
|
3005
|
-
const state = createQuery(
|
|
2734
|
+
const state = createQuery(() => ({
|
|
2735
|
+
queryKey: key,
|
|
2736
|
+
queryFn: () => 'data',
|
|
3006
2737
|
refetchOnMount: true,
|
|
3007
2738
|
staleTime: 0,
|
|
3008
|
-
})
|
|
2739
|
+
}))
|
|
3009
2740
|
createRenderEffect(() => {
|
|
3010
2741
|
states.push({ ...state })
|
|
3011
2742
|
})
|
|
@@ -3037,18 +2768,18 @@ describe('createQuery', () => {
|
|
|
3037
2768
|
const key = queryKey()
|
|
3038
2769
|
|
|
3039
2770
|
function Page() {
|
|
3040
|
-
const state = createQuery
|
|
3041
|
-
key,
|
|
3042
|
-
() => {
|
|
3043
|
-
return Promise.reject('Error test jaylen')
|
|
2771
|
+
const state = createQuery(() => ({
|
|
2772
|
+
queryKey: key,
|
|
2773
|
+
queryFn: () => {
|
|
2774
|
+
return Promise.reject(new Error('Error test jaylen'))
|
|
3044
2775
|
},
|
|
3045
|
-
|
|
3046
|
-
)
|
|
2776
|
+
retry: false,
|
|
2777
|
+
}))
|
|
3047
2778
|
|
|
3048
2779
|
return (
|
|
3049
2780
|
<div>
|
|
3050
2781
|
<h1>{state.status}</h1>
|
|
3051
|
-
<h2>{state.error}</h2>
|
|
2782
|
+
<h2>{state.error?.message}</h2>
|
|
3052
2783
|
</div>
|
|
3053
2784
|
)
|
|
3054
2785
|
}
|
|
@@ -3063,20 +2794,21 @@ describe('createQuery', () => {
|
|
|
3063
2794
|
await waitFor(() => screen.getByText('Error test jaylen'))
|
|
3064
2795
|
})
|
|
3065
2796
|
|
|
3066
|
-
it('should throw error if queryFn throws and
|
|
2797
|
+
it('should throw error if queryFn throws and throwErrors is in use', async () => {
|
|
3067
2798
|
const key = queryKey()
|
|
3068
2799
|
|
|
3069
2800
|
function Page() {
|
|
3070
|
-
const state = createQuery
|
|
3071
|
-
key,
|
|
3072
|
-
() => Promise.reject('Error test jaylen'),
|
|
3073
|
-
|
|
3074
|
-
|
|
2801
|
+
const state = createQuery(() => ({
|
|
2802
|
+
queryKey: key,
|
|
2803
|
+
queryFn: () => Promise.reject(new Error('Error test jaylen')),
|
|
2804
|
+
retry: false,
|
|
2805
|
+
throwErrors: true,
|
|
2806
|
+
}))
|
|
3075
2807
|
|
|
3076
2808
|
return (
|
|
3077
2809
|
<div>
|
|
3078
2810
|
<h1>{state.status}</h1>
|
|
3079
|
-
<h2>{state.error}</h2>
|
|
2811
|
+
<h2>{state.error?.message}</h2>
|
|
3080
2812
|
</div>
|
|
3081
2813
|
)
|
|
3082
2814
|
}
|
|
@@ -3092,15 +2824,17 @@ describe('createQuery', () => {
|
|
|
3092
2824
|
await waitFor(() => screen.getByText('error boundary'))
|
|
3093
2825
|
})
|
|
3094
2826
|
|
|
3095
|
-
it('should update with data if we observe no properties and
|
|
2827
|
+
it('should update with data if we observe no properties and throwErrors', async () => {
|
|
3096
2828
|
const key = queryKey()
|
|
3097
2829
|
|
|
3098
2830
|
let result: CreateQueryResult<string> | undefined
|
|
3099
2831
|
|
|
3100
2832
|
function Page() {
|
|
3101
|
-
const query = createQuery(
|
|
3102
|
-
|
|
3103
|
-
|
|
2833
|
+
const query = createQuery(() => ({
|
|
2834
|
+
queryKey: key,
|
|
2835
|
+
queryFn: () => Promise.resolve('data'),
|
|
2836
|
+
throwErrors: true,
|
|
2837
|
+
}))
|
|
3104
2838
|
|
|
3105
2839
|
createEffect(() => {
|
|
3106
2840
|
result = query
|
|
@@ -3124,19 +2858,17 @@ describe('createQuery', () => {
|
|
|
3124
2858
|
const key = queryKey()
|
|
3125
2859
|
|
|
3126
2860
|
function Page() {
|
|
3127
|
-
const state = createQuery
|
|
3128
|
-
key,
|
|
3129
|
-
() => Promise.reject('Local Error'),
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
},
|
|
3134
|
-
)
|
|
2861
|
+
const state = createQuery(() => ({
|
|
2862
|
+
queryKey: key,
|
|
2863
|
+
queryFn: () => Promise.reject(new Error('Local Error')),
|
|
2864
|
+
retry: false,
|
|
2865
|
+
throwErrors: (err) => err.message !== 'Local Error',
|
|
2866
|
+
}))
|
|
3135
2867
|
|
|
3136
2868
|
return (
|
|
3137
2869
|
<div>
|
|
3138
2870
|
<h1>{state.status}</h1>
|
|
3139
|
-
<h2>{state.error}</h2>
|
|
2871
|
+
<h2>{state.error?.message}</h2>
|
|
3140
2872
|
</div>
|
|
3141
2873
|
)
|
|
3142
2874
|
}
|
|
@@ -3157,14 +2889,12 @@ describe('createQuery', () => {
|
|
|
3157
2889
|
const key = queryKey()
|
|
3158
2890
|
|
|
3159
2891
|
function Page() {
|
|
3160
|
-
const state = createQuery<unknown, Error>(
|
|
3161
|
-
key,
|
|
3162
|
-
() => Promise.reject(new Error('Remote Error')),
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
},
|
|
3167
|
-
)
|
|
2892
|
+
const state = createQuery<unknown, Error>(() => ({
|
|
2893
|
+
queryKey: key,
|
|
2894
|
+
queryFn: () => Promise.reject(new Error('Remote Error')),
|
|
2895
|
+
retry: false,
|
|
2896
|
+
throwErrors: (err) => err.message !== 'Local Error',
|
|
2897
|
+
}))
|
|
3168
2898
|
|
|
3169
2899
|
return (
|
|
3170
2900
|
<div>
|
|
@@ -3199,24 +2929,23 @@ describe('createQuery', () => {
|
|
|
3199
2929
|
let count = 0
|
|
3200
2930
|
|
|
3201
2931
|
function Page() {
|
|
3202
|
-
const result = createQuery
|
|
3203
|
-
key,
|
|
3204
|
-
async () => {
|
|
2932
|
+
const result = createQuery(() => ({
|
|
2933
|
+
queryKey: key,
|
|
2934
|
+
queryFn: async () => {
|
|
3205
2935
|
count++
|
|
3206
2936
|
await sleep(10)
|
|
3207
|
-
return Promise.reject('some error')
|
|
3208
|
-
},
|
|
3209
|
-
{
|
|
3210
|
-
retry: 2,
|
|
3211
|
-
retryDelay: 100,
|
|
2937
|
+
return Promise.reject(new Error('some error'))
|
|
3212
2938
|
},
|
|
3213
|
-
|
|
2939
|
+
retry: 2,
|
|
2940
|
+
|
|
2941
|
+
retryDelay: 100,
|
|
2942
|
+
}))
|
|
3214
2943
|
|
|
3215
2944
|
return (
|
|
3216
2945
|
<div>
|
|
3217
|
-
<div>error: {result.error ?? 'null'}</div>
|
|
2946
|
+
<div>error: {result.error?.message ?? 'null'}</div>
|
|
3218
2947
|
<div>failureCount: {result.failureCount}</div>
|
|
3219
|
-
<div>failureReason: {result.failureReason}</div>
|
|
2948
|
+
<div>failureReason: {result.failureReason?.message}</div>
|
|
3220
2949
|
</div>
|
|
3221
2950
|
)
|
|
3222
2951
|
}
|
|
@@ -3255,24 +2984,22 @@ describe('createQuery', () => {
|
|
|
3255
2984
|
let count = 0
|
|
3256
2985
|
|
|
3257
2986
|
function Page() {
|
|
3258
|
-
const result = createQuery
|
|
3259
|
-
key,
|
|
3260
|
-
async () => {
|
|
2987
|
+
const result = createQuery(() => ({
|
|
2988
|
+
queryKey: key,
|
|
2989
|
+
queryFn: async () => {
|
|
3261
2990
|
count++
|
|
3262
2991
|
await sleep(10)
|
|
3263
|
-
return Promise.reject('some error')
|
|
2992
|
+
return Promise.reject(new Error('some error'))
|
|
3264
2993
|
},
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
},
|
|
3269
|
-
)
|
|
2994
|
+
retry: 2,
|
|
2995
|
+
retryDelay: 100,
|
|
2996
|
+
}))
|
|
3270
2997
|
|
|
3271
2998
|
return (
|
|
3272
2999
|
<div>
|
|
3273
|
-
<div>error: {result.error ?? 'null'}</div>
|
|
3000
|
+
<div>error: {result.error?.message ?? 'null'}</div>
|
|
3274
3001
|
<div>failureCount: {result.failureCount}</div>
|
|
3275
|
-
<div>failureReason: {result.failureReason}</div>
|
|
3002
|
+
<div>failureReason: {result.failureReason?.message}</div>
|
|
3276
3003
|
</div>
|
|
3277
3004
|
)
|
|
3278
3005
|
}
|
|
@@ -3285,9 +3012,7 @@ describe('createQuery', () => {
|
|
|
3285
3012
|
return (
|
|
3286
3013
|
<div>
|
|
3287
3014
|
<button onClick={toggle}>{show() ? 'hide' : 'show'}</button>
|
|
3288
|
-
<button
|
|
3289
|
-
onClick={() => queryClient.cancelQueries({ queryKey: key() })}
|
|
3290
|
-
>
|
|
3015
|
+
<button onClick={() => queryClient.cancelQueries({ queryKey: key })}>
|
|
3291
3016
|
cancel
|
|
3292
3017
|
</button>
|
|
3293
3018
|
{show() && <Page />}
|
|
@@ -3317,13 +3042,18 @@ describe('createQuery', () => {
|
|
|
3317
3042
|
const key = queryKey()
|
|
3318
3043
|
const states: CreateQueryResult<string>[] = []
|
|
3319
3044
|
|
|
3320
|
-
await queryClient.prefetchQuery(
|
|
3045
|
+
await queryClient.prefetchQuery({
|
|
3046
|
+
queryKey: key,
|
|
3047
|
+
queryFn: () => 'prefetched',
|
|
3048
|
+
})
|
|
3321
3049
|
|
|
3322
3050
|
function Page() {
|
|
3323
|
-
const state = createQuery(
|
|
3051
|
+
const state = createQuery(() => ({
|
|
3052
|
+
queryKey: key,
|
|
3053
|
+
queryFn: () => 'data',
|
|
3324
3054
|
refetchOnMount: 'always',
|
|
3325
3055
|
staleTime: 50,
|
|
3326
|
-
})
|
|
3056
|
+
}))
|
|
3327
3057
|
createRenderEffect(() => {
|
|
3328
3058
|
states.push({ ...state })
|
|
3329
3059
|
})
|
|
@@ -3367,9 +3097,11 @@ describe('createQuery', () => {
|
|
|
3367
3097
|
const states: DefinedCreateQueryResult<string>[] = []
|
|
3368
3098
|
|
|
3369
3099
|
function Page() {
|
|
3370
|
-
const state = createQuery(
|
|
3100
|
+
const state = createQuery(() => ({
|
|
3101
|
+
queryKey: key,
|
|
3102
|
+
queryFn: () => 'data',
|
|
3371
3103
|
initialData: 'initial',
|
|
3372
|
-
})
|
|
3104
|
+
}))
|
|
3373
3105
|
createRenderEffect(() => {
|
|
3374
3106
|
states.push({ ...state })
|
|
3375
3107
|
})
|
|
@@ -3403,10 +3135,12 @@ describe('createQuery', () => {
|
|
|
3403
3135
|
const states: DefinedCreateQueryResult<string>[] = []
|
|
3404
3136
|
|
|
3405
3137
|
function Page() {
|
|
3406
|
-
const state = createQuery(
|
|
3138
|
+
const state = createQuery(() => ({
|
|
3139
|
+
queryKey: key,
|
|
3140
|
+
queryFn: () => 'data',
|
|
3407
3141
|
staleTime: 50,
|
|
3408
3142
|
initialData: 'initial',
|
|
3409
|
-
})
|
|
3143
|
+
}))
|
|
3410
3144
|
createRenderEffect(() => {
|
|
3411
3145
|
states.push({ ...state })
|
|
3412
3146
|
})
|
|
@@ -3441,11 +3175,13 @@ describe('createQuery', () => {
|
|
|
3441
3175
|
const oneSecondAgo = Date.now() - 1000
|
|
3442
3176
|
|
|
3443
3177
|
function Page() {
|
|
3444
|
-
const state = createQuery(
|
|
3178
|
+
const state = createQuery(() => ({
|
|
3179
|
+
queryKey: key,
|
|
3180
|
+
queryFn: () => 'data',
|
|
3445
3181
|
staleTime: 50,
|
|
3446
3182
|
initialData: 'initial',
|
|
3447
3183
|
initialDataUpdatedAt: oneSecondAgo,
|
|
3448
|
-
})
|
|
3184
|
+
}))
|
|
3449
3185
|
createRenderEffect(() => {
|
|
3450
3186
|
states.push({ ...state })
|
|
3451
3187
|
})
|
|
@@ -3483,11 +3219,13 @@ describe('createQuery', () => {
|
|
|
3483
3219
|
const states: DefinedCreateQueryResult<string>[] = []
|
|
3484
3220
|
|
|
3485
3221
|
function Page() {
|
|
3486
|
-
const state = createQuery(
|
|
3222
|
+
const state = createQuery(() => ({
|
|
3223
|
+
queryKey: key,
|
|
3224
|
+
queryFn: () => 'data',
|
|
3487
3225
|
staleTime: 10 * 1000, // 10 seconds
|
|
3488
3226
|
initialData: 'initial',
|
|
3489
3227
|
initialDataUpdatedAt: 0,
|
|
3490
|
-
})
|
|
3228
|
+
}))
|
|
3491
3229
|
createRenderEffect(() => {
|
|
3492
3230
|
states.push({ ...state })
|
|
3493
3231
|
})
|
|
@@ -3521,14 +3259,12 @@ describe('createQuery', () => {
|
|
|
3521
3259
|
|
|
3522
3260
|
function Page() {
|
|
3523
3261
|
const [count, setCount] = createSignal(0)
|
|
3524
|
-
const state = createQuery(
|
|
3525
|
-
|
|
3526
|
-
() => ({ count: 10 }),
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
},
|
|
3531
|
-
)
|
|
3262
|
+
const state = createQuery(() => ({
|
|
3263
|
+
queryKey: [key, count()],
|
|
3264
|
+
queryFn: () => ({ count: 10 }),
|
|
3265
|
+
staleTime: Infinity,
|
|
3266
|
+
initialData: () => ({ count: count() }),
|
|
3267
|
+
}))
|
|
3532
3268
|
createRenderEffect(() => {
|
|
3533
3269
|
states.push({ ...state })
|
|
3534
3270
|
})
|
|
@@ -3562,20 +3298,22 @@ describe('createQuery', () => {
|
|
|
3562
3298
|
|
|
3563
3299
|
const queryFn = jest.fn<unknown, unknown[]>()
|
|
3564
3300
|
queryFn.mockImplementation(() => {
|
|
3565
|
-
return Promise.reject('Error test Barrett')
|
|
3301
|
+
return Promise.reject(new Error('Error test Barrett'))
|
|
3566
3302
|
})
|
|
3567
3303
|
|
|
3568
3304
|
function Page() {
|
|
3569
|
-
const state = createQuery
|
|
3305
|
+
const state = createQuery(() => ({
|
|
3306
|
+
queryKey: key,
|
|
3307
|
+
queryFn,
|
|
3570
3308
|
retry: 1,
|
|
3571
3309
|
retryDelay: 1,
|
|
3572
|
-
})
|
|
3310
|
+
}))
|
|
3573
3311
|
|
|
3574
3312
|
return (
|
|
3575
3313
|
<div>
|
|
3576
3314
|
<h1>{state.status}</h1>
|
|
3577
3315
|
<h2>Failed {state.failureCount} times</h2>
|
|
3578
|
-
<h2>Failed because {state.failureReason}</h2>
|
|
3316
|
+
<h2>Failed because {state.failureReason?.message}</h2>
|
|
3579
3317
|
</div>
|
|
3580
3318
|
)
|
|
3581
3319
|
}
|
|
@@ -3586,7 +3324,7 @@ describe('createQuery', () => {
|
|
|
3586
3324
|
</QueryClientProvider>
|
|
3587
3325
|
))
|
|
3588
3326
|
|
|
3589
|
-
await waitFor(() => screen.getByText('
|
|
3327
|
+
await waitFor(() => screen.getByText('pending'))
|
|
3590
3328
|
await waitFor(() => screen.getByText('error'))
|
|
3591
3329
|
|
|
3592
3330
|
// query should fail `retry + 1` times, since first time isn't a "retry"
|
|
@@ -3602,25 +3340,27 @@ describe('createQuery', () => {
|
|
|
3602
3340
|
const queryFn = jest.fn<unknown, unknown[]>()
|
|
3603
3341
|
|
|
3604
3342
|
queryFn.mockImplementationOnce(() => {
|
|
3605
|
-
return Promise.reject('Error test Tanner')
|
|
3343
|
+
return Promise.reject(new Error('Error test Tanner'))
|
|
3606
3344
|
})
|
|
3607
3345
|
|
|
3608
3346
|
queryFn.mockImplementation(() => {
|
|
3609
|
-
return Promise.reject('NoRetry')
|
|
3347
|
+
return Promise.reject(new Error('NoRetry'))
|
|
3610
3348
|
})
|
|
3611
3349
|
|
|
3612
3350
|
function Page() {
|
|
3613
|
-
const state = createQuery
|
|
3351
|
+
const state = createQuery(() => ({
|
|
3352
|
+
queryKey: key,
|
|
3353
|
+
queryFn,
|
|
3614
3354
|
retryDelay: 1,
|
|
3615
|
-
retry: (_failureCount, err) => err !== 'NoRetry',
|
|
3616
|
-
})
|
|
3355
|
+
retry: (_failureCount, err) => err.message !== 'NoRetry',
|
|
3356
|
+
}))
|
|
3617
3357
|
|
|
3618
3358
|
return (
|
|
3619
3359
|
<div>
|
|
3620
3360
|
<h1>{state.status}</h1>
|
|
3621
3361
|
<h2>Failed {state.failureCount} times</h2>
|
|
3622
|
-
<h2>Failed because {state.failureReason}</h2>
|
|
3623
|
-
<h2>{state.error}</h2>
|
|
3362
|
+
<h2>Failed because {state.failureReason?.message}</h2>
|
|
3363
|
+
<h2>{state.error?.message}</h2>
|
|
3624
3364
|
</div>
|
|
3625
3365
|
)
|
|
3626
3366
|
}
|
|
@@ -3631,7 +3371,7 @@ describe('createQuery', () => {
|
|
|
3631
3371
|
</QueryClientProvider>
|
|
3632
3372
|
))
|
|
3633
3373
|
|
|
3634
|
-
await waitFor(() => screen.getByText('
|
|
3374
|
+
await waitFor(() => screen.getByText('pending'))
|
|
3635
3375
|
await waitFor(() => screen.getByText('error'))
|
|
3636
3376
|
await waitFor(() => screen.getByText('Failed 2 times'))
|
|
3637
3377
|
await waitFor(() => screen.getByText('Failed because NoRetry'))
|
|
@@ -3651,10 +3391,12 @@ describe('createQuery', () => {
|
|
|
3651
3391
|
})
|
|
3652
3392
|
|
|
3653
3393
|
function Page() {
|
|
3654
|
-
const state = createQuery<unknown, DelayError>(
|
|
3394
|
+
const state = createQuery<unknown, DelayError>(() => ({
|
|
3395
|
+
queryKey: key,
|
|
3396
|
+
queryFn,
|
|
3655
3397
|
retry: 1,
|
|
3656
3398
|
retryDelay: (_, error: DelayError) => error.delay,
|
|
3657
|
-
})
|
|
3399
|
+
}))
|
|
3658
3400
|
|
|
3659
3401
|
return (
|
|
3660
3402
|
<div>
|
|
@@ -3691,17 +3433,15 @@ describe('createQuery', () => {
|
|
|
3691
3433
|
let count = 0
|
|
3692
3434
|
|
|
3693
3435
|
function Page() {
|
|
3694
|
-
const query = createQuery<unknown, string>(
|
|
3695
|
-
key,
|
|
3696
|
-
() => {
|
|
3436
|
+
const query = createQuery<unknown, string>(() => ({
|
|
3437
|
+
queryKey: key,
|
|
3438
|
+
queryFn: () => {
|
|
3697
3439
|
count++
|
|
3698
3440
|
return Promise.reject<unknown>(`fetching error ${count}`)
|
|
3699
3441
|
},
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
},
|
|
3704
|
-
)
|
|
3442
|
+
retry: 3,
|
|
3443
|
+
retryDelay: 1,
|
|
3444
|
+
}))
|
|
3705
3445
|
|
|
3706
3446
|
return (
|
|
3707
3447
|
<div>
|
|
@@ -3722,7 +3462,7 @@ describe('createQuery', () => {
|
|
|
3722
3462
|
// The query should display the first error result
|
|
3723
3463
|
await waitFor(() => screen.getByText('failureCount 1'))
|
|
3724
3464
|
await waitFor(() => screen.getByText('failureReason fetching error 1'))
|
|
3725
|
-
await waitFor(() => screen.getByText('status
|
|
3465
|
+
await waitFor(() => screen.getByText('status pending'))
|
|
3726
3466
|
await waitFor(() => screen.getByText('error null'))
|
|
3727
3467
|
|
|
3728
3468
|
// Check if the query really paused
|
|
@@ -3731,7 +3471,7 @@ describe('createQuery', () => {
|
|
|
3731
3471
|
await waitFor(() => screen.getByText('failureReason fetching error 1'))
|
|
3732
3472
|
|
|
3733
3473
|
visibilityMock.mockRestore()
|
|
3734
|
-
window.dispatchEvent(new
|
|
3474
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
3735
3475
|
|
|
3736
3476
|
// Wait for the final result
|
|
3737
3477
|
await waitFor(() => screen.getByText('failureCount 4'))
|
|
@@ -3743,19 +3483,19 @@ describe('createQuery', () => {
|
|
|
3743
3483
|
await sleep(10)
|
|
3744
3484
|
await waitFor(() => screen.getByText('failureCount 4'))
|
|
3745
3485
|
await waitFor(() => screen.getByText('failureReason fetching error 4'))
|
|
3746
|
-
|
|
3747
|
-
// Check if the error has been logged in the console
|
|
3748
|
-
expect(mockLogger.error).toHaveBeenCalledWith('fetching error 4')
|
|
3749
3486
|
})
|
|
3750
3487
|
|
|
3751
3488
|
it('should fetch on mount when a query was already created with setQueryData', async () => {
|
|
3752
3489
|
const key = queryKey()
|
|
3753
3490
|
const states: CreateQueryResult<string>[] = []
|
|
3754
3491
|
|
|
3755
|
-
queryClient.setQueryData(key
|
|
3492
|
+
queryClient.setQueryData(key, 'prefetched')
|
|
3756
3493
|
|
|
3757
3494
|
function Page() {
|
|
3758
|
-
const state = createQuery(
|
|
3495
|
+
const state = createQuery(() => ({
|
|
3496
|
+
queryKey: key,
|
|
3497
|
+
queryFn: () => 'data',
|
|
3498
|
+
}))
|
|
3759
3499
|
createRenderEffect(() => {
|
|
3760
3500
|
states.push({ ...state })
|
|
3761
3501
|
})
|
|
@@ -3793,13 +3533,16 @@ describe('createQuery', () => {
|
|
|
3793
3533
|
const visibilityMock = mockVisibilityState('hidden')
|
|
3794
3534
|
|
|
3795
3535
|
// set data in cache to check if the hook query fn is actually called
|
|
3796
|
-
queryClient.setQueryData(key
|
|
3536
|
+
queryClient.setQueryData(key, 'prefetched')
|
|
3797
3537
|
|
|
3798
3538
|
function Page() {
|
|
3799
|
-
const state = createQuery(
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
|
|
3539
|
+
const state = createQuery(() => ({
|
|
3540
|
+
queryKey: key,
|
|
3541
|
+
queryFn: async () => {
|
|
3542
|
+
await sleep(10)
|
|
3543
|
+
return 'data'
|
|
3544
|
+
},
|
|
3545
|
+
}))
|
|
3803
3546
|
createRenderEffect(() => {
|
|
3804
3547
|
states.push({ ...state })
|
|
3805
3548
|
})
|
|
@@ -3820,7 +3563,7 @@ describe('createQuery', () => {
|
|
|
3820
3563
|
|
|
3821
3564
|
// reset visibilityState to original value
|
|
3822
3565
|
visibilityMock.mockRestore()
|
|
3823
|
-
window.dispatchEvent(new
|
|
3566
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
3824
3567
|
|
|
3825
3568
|
await waitFor(() => expect(states.length).toBe(4))
|
|
3826
3569
|
|
|
@@ -3859,14 +3602,16 @@ describe('createQuery', () => {
|
|
|
3859
3602
|
const prefetchQueryFn = jest.fn<string, unknown[]>()
|
|
3860
3603
|
prefetchQueryFn.mockImplementation(() => 'not yet...')
|
|
3861
3604
|
|
|
3862
|
-
await queryClient.prefetchQuery(
|
|
3605
|
+
await queryClient.prefetchQuery({
|
|
3606
|
+
queryKey: key,
|
|
3607
|
+
queryFn: prefetchQueryFn,
|
|
3863
3608
|
staleTime: 10,
|
|
3864
3609
|
})
|
|
3865
3610
|
|
|
3866
3611
|
await sleep(11)
|
|
3867
3612
|
|
|
3868
3613
|
function Page() {
|
|
3869
|
-
const state = createQuery(key, queryFn)
|
|
3614
|
+
const state = createQuery(() => ({ queryKey: key, queryFn }))
|
|
3870
3615
|
createRenderEffect(() => {
|
|
3871
3616
|
states.push({ ...state })
|
|
3872
3617
|
})
|
|
@@ -3897,16 +3642,16 @@ describe('createQuery', () => {
|
|
|
3897
3642
|
return 'not yet...'
|
|
3898
3643
|
})
|
|
3899
3644
|
|
|
3900
|
-
await queryClient.prefetchQuery(
|
|
3645
|
+
await queryClient.prefetchQuery({
|
|
3646
|
+
queryKey: key,
|
|
3647
|
+
queryFn: prefetchQueryFn,
|
|
3901
3648
|
staleTime: 1000,
|
|
3902
3649
|
})
|
|
3903
3650
|
|
|
3904
3651
|
await sleep(0)
|
|
3905
3652
|
|
|
3906
3653
|
function Page() {
|
|
3907
|
-
createQuery(key, queryFn,
|
|
3908
|
-
staleTime: 1000,
|
|
3909
|
-
})
|
|
3654
|
+
createQuery(() => ({ queryKey: key, queryFn, staleTime: 1000 }))
|
|
3910
3655
|
return null
|
|
3911
3656
|
}
|
|
3912
3657
|
|
|
@@ -3929,9 +3674,9 @@ describe('createQuery', () => {
|
|
|
3929
3674
|
function Page() {
|
|
3930
3675
|
let counter = 0
|
|
3931
3676
|
|
|
3932
|
-
const query = createQuery<unknown, Error>(
|
|
3933
|
-
key,
|
|
3934
|
-
async () => {
|
|
3677
|
+
const query = createQuery<unknown, Error>(() => ({
|
|
3678
|
+
queryKey: key,
|
|
3679
|
+
queryFn: async () => {
|
|
3935
3680
|
if (counter < 2) {
|
|
3936
3681
|
counter++
|
|
3937
3682
|
throw new Error('error')
|
|
@@ -3939,8 +3684,8 @@ describe('createQuery', () => {
|
|
|
3939
3684
|
return 'data'
|
|
3940
3685
|
}
|
|
3941
3686
|
},
|
|
3942
|
-
|
|
3943
|
-
)
|
|
3687
|
+
retryDelay: 10,
|
|
3688
|
+
}))
|
|
3944
3689
|
|
|
3945
3690
|
return (
|
|
3946
3691
|
<div>
|
|
@@ -3971,25 +3716,22 @@ describe('createQuery', () => {
|
|
|
3971
3716
|
const [enabled, setEnabled] = createSignal(false)
|
|
3972
3717
|
const [isPrefetched, setPrefetched] = createSignal(false)
|
|
3973
3718
|
|
|
3974
|
-
const query = createQuery(
|
|
3975
|
-
key,
|
|
3976
|
-
async () => {
|
|
3719
|
+
const query = createQuery(() => ({
|
|
3720
|
+
queryKey: key,
|
|
3721
|
+
queryFn: async () => {
|
|
3977
3722
|
count++
|
|
3978
3723
|
await sleep(10)
|
|
3979
3724
|
return count
|
|
3980
3725
|
},
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
return enabled()
|
|
3984
|
-
},
|
|
3985
|
-
},
|
|
3986
|
-
)
|
|
3726
|
+
enabled: enabled(),
|
|
3727
|
+
}))
|
|
3987
3728
|
|
|
3988
3729
|
createEffect(() => {
|
|
3989
3730
|
async function prefetch() {
|
|
3990
|
-
await queryClient.prefetchQuery(
|
|
3991
|
-
|
|
3992
|
-
|
|
3731
|
+
await queryClient.prefetchQuery({
|
|
3732
|
+
queryKey: key,
|
|
3733
|
+
queryFn: () => Promise.resolve('prefetched data'),
|
|
3734
|
+
})
|
|
3993
3735
|
setPrefetched(true)
|
|
3994
3736
|
}
|
|
3995
3737
|
prefetch()
|
|
@@ -4023,11 +3765,11 @@ describe('createQuery', () => {
|
|
|
4023
3765
|
function Page() {
|
|
4024
3766
|
const [shouldFetch, setShouldFetch] = createSignal(false)
|
|
4025
3767
|
|
|
4026
|
-
const query = createQuery(
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
})
|
|
3768
|
+
const query = createQuery(() => ({
|
|
3769
|
+
queryKey: key,
|
|
3770
|
+
queryFn: () => 'data',
|
|
3771
|
+
enabled: shouldFetch(),
|
|
3772
|
+
}))
|
|
4031
3773
|
|
|
4032
3774
|
return (
|
|
4033
3775
|
<div>
|
|
@@ -4063,15 +3805,20 @@ describe('createQuery', () => {
|
|
|
4063
3805
|
const results: DefinedCreateQueryResult<string>[] = []
|
|
4064
3806
|
|
|
4065
3807
|
function Page() {
|
|
4066
|
-
const result = createQuery(
|
|
4067
|
-
|
|
4068
|
-
|
|
3808
|
+
const result = createQuery(() => ({
|
|
3809
|
+
queryKey: key,
|
|
3810
|
+
queryFn: async () => {
|
|
3811
|
+
await sleep(10)
|
|
3812
|
+
return 'serverData'
|
|
3813
|
+
},
|
|
3814
|
+
initialData: 'initialData',
|
|
3815
|
+
}))
|
|
4069
3816
|
|
|
4070
3817
|
createRenderEffect(() => {
|
|
4071
3818
|
results.push({ ...result })
|
|
4072
3819
|
})
|
|
4073
3820
|
|
|
4074
|
-
return
|
|
3821
|
+
return <div>data: {result.data}</div>
|
|
4075
3822
|
}
|
|
4076
3823
|
|
|
4077
3824
|
render(() => (
|
|
@@ -4080,10 +3827,11 @@ describe('createQuery', () => {
|
|
|
4080
3827
|
</QueryClientProvider>
|
|
4081
3828
|
))
|
|
4082
3829
|
|
|
4083
|
-
await
|
|
3830
|
+
await waitFor(() => screen.getByText('data: initialData'))
|
|
3831
|
+
await waitFor(() => screen.getByText('data: serverData'))
|
|
4084
3832
|
|
|
4085
3833
|
expect(results.length).toBe(2)
|
|
4086
|
-
expect(results[0]).toMatchObject({ data: '
|
|
3834
|
+
expect(results[0]).toMatchObject({ data: 'initialData', isFetching: true })
|
|
4087
3835
|
expect(results[1]).toMatchObject({ data: 'serverData', isFetching: false })
|
|
4088
3836
|
})
|
|
4089
3837
|
|
|
@@ -4092,7 +3840,11 @@ describe('createQuery', () => {
|
|
|
4092
3840
|
const results: DefinedCreateQueryResult<number>[] = []
|
|
4093
3841
|
|
|
4094
3842
|
function Page() {
|
|
4095
|
-
const result = createQuery(
|
|
3843
|
+
const result = createQuery(() => ({
|
|
3844
|
+
queryKey: key,
|
|
3845
|
+
queryFn: () => 1,
|
|
3846
|
+
initialData: 0,
|
|
3847
|
+
}))
|
|
4096
3848
|
|
|
4097
3849
|
createRenderEffect(() => {
|
|
4098
3850
|
results.push({ ...result })
|
|
@@ -4122,14 +3874,12 @@ describe('createQuery', () => {
|
|
|
4122
3874
|
function Page() {
|
|
4123
3875
|
const [shouldFetch, setShouldFetch] = createSignal(true)
|
|
4124
3876
|
|
|
4125
|
-
const result = createQuery(
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
},
|
|
4132
|
-
})
|
|
3877
|
+
const result = createQuery(() => ({
|
|
3878
|
+
queryKey: key,
|
|
3879
|
+
queryFn: () => 'fetched data',
|
|
3880
|
+
enabled: shouldFetch(),
|
|
3881
|
+
initialData: shouldFetch() ? 'initial' : 'initial falsy',
|
|
3882
|
+
}))
|
|
4133
3883
|
|
|
4134
3884
|
createRenderEffect(() => {
|
|
4135
3885
|
results.push({ ...result })
|
|
@@ -4163,11 +3913,12 @@ describe('createQuery', () => {
|
|
|
4163
3913
|
queryFn.mockImplementation(() => 'data')
|
|
4164
3914
|
|
|
4165
3915
|
function Page() {
|
|
4166
|
-
const { fetchStatus } = createQuery({
|
|
3916
|
+
const { fetchStatus } = createQuery(() => ({
|
|
4167
3917
|
queryKey: key,
|
|
4168
3918
|
queryFn,
|
|
4169
3919
|
enabled: false,
|
|
4170
|
-
})
|
|
3920
|
+
}))
|
|
3921
|
+
|
|
4171
3922
|
return <div>fetchStatus: {fetchStatus}</div>
|
|
4172
3923
|
}
|
|
4173
3924
|
|
|
@@ -4178,18 +3929,20 @@ describe('createQuery', () => {
|
|
|
4178
3929
|
))
|
|
4179
3930
|
|
|
4180
3931
|
expect(queryFn).not.toHaveBeenCalled()
|
|
4181
|
-
expect(queryCache.find(key
|
|
3932
|
+
expect(queryCache.find({ queryKey: key })).not.toBeUndefined()
|
|
4182
3933
|
screen.getByText('fetchStatus: idle')
|
|
4183
3934
|
})
|
|
4184
3935
|
|
|
4185
3936
|
// See https://github.com/tannerlinsley/react-query/issues/360
|
|
4186
|
-
it('should init to status:
|
|
3937
|
+
it('should init to status:pending, fetchStatus:idle when enabled is false', async () => {
|
|
4187
3938
|
const key = queryKey()
|
|
4188
3939
|
|
|
4189
3940
|
function Page() {
|
|
4190
|
-
const query = createQuery(
|
|
3941
|
+
const query = createQuery(() => ({
|
|
3942
|
+
queryKey: key,
|
|
3943
|
+
queryFn: () => 'data',
|
|
4191
3944
|
enabled: false,
|
|
4192
|
-
})
|
|
3945
|
+
}))
|
|
4193
3946
|
|
|
4194
3947
|
return (
|
|
4195
3948
|
<div>
|
|
@@ -4206,16 +3959,44 @@ describe('createQuery', () => {
|
|
|
4206
3959
|
</QueryClientProvider>
|
|
4207
3960
|
))
|
|
4208
3961
|
|
|
4209
|
-
await waitFor(() => screen.getByText('status:
|
|
3962
|
+
await waitFor(() => screen.getByText('status: pending, idle'))
|
|
4210
3963
|
})
|
|
4211
3964
|
|
|
4212
|
-
it('should not schedule garbage collection, if
|
|
3965
|
+
it('should not schedule garbage collection, if gcTimeout is set to `Infinity`', async () => {
|
|
4213
3966
|
const key = queryKey()
|
|
4214
3967
|
|
|
4215
3968
|
function Page() {
|
|
4216
|
-
const query = createQuery(
|
|
4217
|
-
|
|
4218
|
-
|
|
3969
|
+
const query = createQuery(() => ({
|
|
3970
|
+
queryKey: key,
|
|
3971
|
+
queryFn: () => 'fetched data',
|
|
3972
|
+
gcTime: Infinity,
|
|
3973
|
+
}))
|
|
3974
|
+
return <div>{query.data}</div>
|
|
3975
|
+
}
|
|
3976
|
+
|
|
3977
|
+
const result = render(() => (
|
|
3978
|
+
<QueryClientProvider client={queryClient}>
|
|
3979
|
+
<Page />
|
|
3980
|
+
</QueryClientProvider>
|
|
3981
|
+
))
|
|
3982
|
+
|
|
3983
|
+
await waitFor(() => screen.getByText('fetched data'))
|
|
3984
|
+
const setTimeoutSpy = jest.spyOn(window, 'setTimeout')
|
|
3985
|
+
|
|
3986
|
+
result.unmount()
|
|
3987
|
+
|
|
3988
|
+
expect(setTimeoutSpy).not.toHaveBeenCalled()
|
|
3989
|
+
})
|
|
3990
|
+
|
|
3991
|
+
it('should schedule garbage collection, if gcTimeout is not set to `Infinity`', async () => {
|
|
3992
|
+
const key = queryKey()
|
|
3993
|
+
|
|
3994
|
+
function Page() {
|
|
3995
|
+
const query = createQuery(() => ({
|
|
3996
|
+
queryKey: key,
|
|
3997
|
+
queryFn: () => 'fetched data',
|
|
3998
|
+
gcTime: 1000 * 60 * 10, //10 Minutes
|
|
3999
|
+
}))
|
|
4219
4000
|
return <div>{query.data}</div>
|
|
4220
4001
|
}
|
|
4221
4002
|
|
|
@@ -4226,12 +4007,14 @@ describe('createQuery', () => {
|
|
|
4226
4007
|
))
|
|
4227
4008
|
|
|
4228
4009
|
await waitFor(() => screen.getByText('fetched data'))
|
|
4010
|
+
const setTimeoutSpy = jest.spyOn(window, 'setTimeout')
|
|
4229
4011
|
|
|
4230
4012
|
result.unmount()
|
|
4231
4013
|
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4014
|
+
expect(setTimeoutSpy).toHaveBeenLastCalledWith(
|
|
4015
|
+
expect.any(Function),
|
|
4016
|
+
1000 * 60 * 10,
|
|
4017
|
+
)
|
|
4235
4018
|
})
|
|
4236
4019
|
|
|
4237
4020
|
it('should not cause memo churn when data does not change', async () => {
|
|
@@ -4240,16 +4023,19 @@ describe('createQuery', () => {
|
|
|
4240
4023
|
const memoFn = jest.fn()
|
|
4241
4024
|
|
|
4242
4025
|
function Page() {
|
|
4243
|
-
const result = createQuery(
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4026
|
+
const result = createQuery(() => ({
|
|
4027
|
+
queryKey: key,
|
|
4028
|
+
queryFn: async () => {
|
|
4029
|
+
await sleep(10)
|
|
4030
|
+
return (
|
|
4031
|
+
queryFn() || {
|
|
4032
|
+
data: {
|
|
4033
|
+
nested: true,
|
|
4034
|
+
},
|
|
4035
|
+
}
|
|
4036
|
+
)
|
|
4037
|
+
},
|
|
4038
|
+
}))
|
|
4253
4039
|
|
|
4254
4040
|
createMemo(() => {
|
|
4255
4041
|
memoFn()
|
|
@@ -4271,7 +4057,7 @@ describe('createQuery', () => {
|
|
|
4271
4057
|
</QueryClientProvider>
|
|
4272
4058
|
))
|
|
4273
4059
|
|
|
4274
|
-
await waitFor(() => screen.getByText('status
|
|
4060
|
+
await waitFor(() => screen.getByText('status pending'))
|
|
4275
4061
|
await waitFor(() => screen.getByText('status success'))
|
|
4276
4062
|
fireEvent.click(screen.getByText('refetch'))
|
|
4277
4063
|
await waitFor(() => screen.getByText('isFetching true'))
|
|
@@ -4286,11 +4072,11 @@ describe('createQuery', () => {
|
|
|
4286
4072
|
|
|
4287
4073
|
function Page() {
|
|
4288
4074
|
const [int, setInt] = createSignal(200)
|
|
4289
|
-
const state = createQuery(
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
})
|
|
4075
|
+
const state = createQuery(() => ({
|
|
4076
|
+
queryKey: key,
|
|
4077
|
+
queryFn: () => count++,
|
|
4078
|
+
refetchInterval: int(),
|
|
4079
|
+
}))
|
|
4294
4080
|
|
|
4295
4081
|
createEffect(() => {
|
|
4296
4082
|
if (state.data === 2) {
|
|
@@ -4319,16 +4105,14 @@ describe('createQuery', () => {
|
|
|
4319
4105
|
const states: CreateQueryResult<number>[] = []
|
|
4320
4106
|
|
|
4321
4107
|
function Page() {
|
|
4322
|
-
const state = createQuery(
|
|
4323
|
-
key,
|
|
4324
|
-
async () => {
|
|
4108
|
+
const state = createQuery(() => ({
|
|
4109
|
+
queryKey: key,
|
|
4110
|
+
queryFn: async () => {
|
|
4325
4111
|
await sleep(10)
|
|
4326
4112
|
return count++
|
|
4327
4113
|
},
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
},
|
|
4331
|
-
)
|
|
4114
|
+
refetchInterval: (data = 0) => (data < 2 ? 10 : false),
|
|
4115
|
+
}))
|
|
4332
4116
|
|
|
4333
4117
|
createRenderEffect(() => {
|
|
4334
4118
|
states.push({ ...state })
|
|
@@ -4356,7 +4140,7 @@ describe('createQuery', () => {
|
|
|
4356
4140
|
|
|
4357
4141
|
expect(states).toMatchObject([
|
|
4358
4142
|
{
|
|
4359
|
-
status: '
|
|
4143
|
+
status: 'pending',
|
|
4360
4144
|
isFetching: true,
|
|
4361
4145
|
data: undefined,
|
|
4362
4146
|
},
|
|
@@ -4393,9 +4177,11 @@ describe('createQuery', () => {
|
|
|
4393
4177
|
const states: CreateQueryResult<number>[] = []
|
|
4394
4178
|
|
|
4395
4179
|
function Page() {
|
|
4396
|
-
const state = createQuery(
|
|
4180
|
+
const state = createQuery(() => ({
|
|
4181
|
+
queryKey: key,
|
|
4182
|
+
queryFn: () => 1,
|
|
4397
4183
|
refetchInterval: 0,
|
|
4398
|
-
})
|
|
4184
|
+
}))
|
|
4399
4185
|
|
|
4400
4186
|
createRenderEffect(() => {
|
|
4401
4187
|
states.push({ ...state })
|
|
@@ -4418,7 +4204,7 @@ describe('createQuery', () => {
|
|
|
4418
4204
|
|
|
4419
4205
|
expect(states).toMatchObject([
|
|
4420
4206
|
{
|
|
4421
|
-
status: '
|
|
4207
|
+
status: 'pending',
|
|
4422
4208
|
isFetching: true,
|
|
4423
4209
|
data: undefined,
|
|
4424
4210
|
},
|
|
@@ -4432,10 +4218,10 @@ describe('createQuery', () => {
|
|
|
4432
4218
|
|
|
4433
4219
|
it('should accept an empty string as query key', async () => {
|
|
4434
4220
|
function Page() {
|
|
4435
|
-
const result = createQuery(
|
|
4436
|
-
|
|
4437
|
-
(ctx) => ctx.queryKey,
|
|
4438
|
-
)
|
|
4221
|
+
const result = createQuery(() => ({
|
|
4222
|
+
queryKey: [''],
|
|
4223
|
+
queryFn: (ctx) => ctx.queryKey,
|
|
4224
|
+
}))
|
|
4439
4225
|
return <>{JSON.stringify(result.data)}</>
|
|
4440
4226
|
}
|
|
4441
4227
|
|
|
@@ -4450,10 +4236,10 @@ describe('createQuery', () => {
|
|
|
4450
4236
|
|
|
4451
4237
|
it('should accept an object as query key', async () => {
|
|
4452
4238
|
function Page() {
|
|
4453
|
-
const result = createQuery(
|
|
4454
|
-
|
|
4455
|
-
(ctx) => ctx.queryKey,
|
|
4456
|
-
)
|
|
4239
|
+
const result = createQuery(() => ({
|
|
4240
|
+
queryKey: [{ a: 'a' }],
|
|
4241
|
+
queryFn: (ctx) => ctx.queryKey,
|
|
4242
|
+
}))
|
|
4457
4243
|
return <>{JSON.stringify(result.data)}</>
|
|
4458
4244
|
}
|
|
4459
4245
|
|
|
@@ -4472,17 +4258,17 @@ describe('createQuery', () => {
|
|
|
4472
4258
|
const queryFn = jest.fn<string, unknown[]>().mockReturnValue('data')
|
|
4473
4259
|
|
|
4474
4260
|
function Disabled() {
|
|
4475
|
-
createQuery(key, queryFn,
|
|
4261
|
+
createQuery(() => ({ queryKey: key, queryFn, enabled: false }))
|
|
4476
4262
|
return null
|
|
4477
4263
|
}
|
|
4478
4264
|
|
|
4479
4265
|
function Page() {
|
|
4480
4266
|
const [enabled, setEnabled] = createSignal(false)
|
|
4481
|
-
const result = createQuery(
|
|
4482
|
-
|
|
4483
|
-
|
|
4484
|
-
|
|
4485
|
-
})
|
|
4267
|
+
const result = createQuery(() => ({
|
|
4268
|
+
queryKey: key,
|
|
4269
|
+
queryFn,
|
|
4270
|
+
enabled: enabled(),
|
|
4271
|
+
}))
|
|
4486
4272
|
return (
|
|
4487
4273
|
<>
|
|
4488
4274
|
<Disabled />
|
|
@@ -4509,9 +4295,11 @@ describe('createQuery', () => {
|
|
|
4509
4295
|
const states: CreateQueryResult<string>[] = []
|
|
4510
4296
|
|
|
4511
4297
|
function Page() {
|
|
4512
|
-
const state = createQuery(
|
|
4298
|
+
const state = createQuery(() => ({
|
|
4299
|
+
queryKey: key1,
|
|
4300
|
+
queryFn: () => 'data',
|
|
4513
4301
|
placeholderData: 'placeholder',
|
|
4514
|
-
})
|
|
4302
|
+
}))
|
|
4515
4303
|
|
|
4516
4304
|
createRenderEffect(() => {
|
|
4517
4305
|
states.push({ ...state })
|
|
@@ -4554,12 +4342,12 @@ describe('createQuery', () => {
|
|
|
4554
4342
|
function Page() {
|
|
4555
4343
|
const [count, setCount] = createSignal(0)
|
|
4556
4344
|
|
|
4557
|
-
const state = createQuery(
|
|
4345
|
+
const state = createQuery(() => ({
|
|
4346
|
+
queryKey: key1,
|
|
4347
|
+
queryFn: () => 'data',
|
|
4558
4348
|
placeholderData: 'placeholder',
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
},
|
|
4562
|
-
})
|
|
4349
|
+
enabled: count() === 0,
|
|
4350
|
+
}))
|
|
4563
4351
|
|
|
4564
4352
|
createRenderEffect(() => {
|
|
4565
4353
|
states.push({ state: { ...state }, count: count() })
|
|
@@ -4618,10 +4406,12 @@ describe('createQuery', () => {
|
|
|
4618
4406
|
const states: CreateQueryResult<string>[] = []
|
|
4619
4407
|
|
|
4620
4408
|
function Page() {
|
|
4621
|
-
const state = createQuery(
|
|
4409
|
+
const state = createQuery(() => ({
|
|
4410
|
+
queryKey: key1,
|
|
4411
|
+
queryFn: () => 1,
|
|
4622
4412
|
placeholderData: 23,
|
|
4623
4413
|
select: (data) => String(data * 2),
|
|
4624
|
-
})
|
|
4414
|
+
}))
|
|
4625
4415
|
|
|
4626
4416
|
createRenderEffect(() => {
|
|
4627
4417
|
states.push({ ...state })
|
|
@@ -4663,13 +4453,15 @@ describe('createQuery', () => {
|
|
|
4663
4453
|
let placeholderFunctionRunCount = 0
|
|
4664
4454
|
|
|
4665
4455
|
function Page() {
|
|
4666
|
-
const state = createQuery(
|
|
4456
|
+
const state = createQuery(() => ({
|
|
4457
|
+
queryKey: key1,
|
|
4458
|
+
queryFn: () => 1,
|
|
4667
4459
|
placeholderData: () => {
|
|
4668
4460
|
placeholderFunctionRunCount++
|
|
4669
4461
|
return 23
|
|
4670
4462
|
},
|
|
4671
4463
|
select: (data) => String(data * 2),
|
|
4672
|
-
})
|
|
4464
|
+
}))
|
|
4673
4465
|
|
|
4674
4466
|
createRenderEffect(() => {
|
|
4675
4467
|
states.push({ ...state })
|
|
@@ -4706,60 +4498,6 @@ describe('createQuery', () => {
|
|
|
4706
4498
|
expect(placeholderFunctionRunCount).toEqual(1)
|
|
4707
4499
|
})
|
|
4708
4500
|
|
|
4709
|
-
// React Specific implementation. Not really needed since solid functions are stable
|
|
4710
|
-
it.skip('select should only run when dependencies change if memoized', async () => {
|
|
4711
|
-
const key1 = queryKey()
|
|
4712
|
-
|
|
4713
|
-
let selectRun = 0
|
|
4714
|
-
|
|
4715
|
-
function Page() {
|
|
4716
|
-
//@ts-expect-error skip this test
|
|
4717
|
-
const [count, inc] = NotReact.useReducer((prev) => prev + 1, 2)
|
|
4718
|
-
|
|
4719
|
-
const state = createQuery(
|
|
4720
|
-
key1,
|
|
4721
|
-
async () => {
|
|
4722
|
-
await sleep(10)
|
|
4723
|
-
return 0
|
|
4724
|
-
},
|
|
4725
|
-
{
|
|
4726
|
-
//@ts-expect-error skip this test
|
|
4727
|
-
select: NotReact.useCallback(
|
|
4728
|
-
(data: number) => {
|
|
4729
|
-
selectRun++
|
|
4730
|
-
return `selected ${data + count}`
|
|
4731
|
-
},
|
|
4732
|
-
[count],
|
|
4733
|
-
),
|
|
4734
|
-
placeholderData: 99,
|
|
4735
|
-
},
|
|
4736
|
-
)
|
|
4737
|
-
|
|
4738
|
-
return (
|
|
4739
|
-
<div>
|
|
4740
|
-
<h2>Data: {state.data}</h2>
|
|
4741
|
-
<button onClick={inc}>inc: {count}</button>
|
|
4742
|
-
</div>
|
|
4743
|
-
)
|
|
4744
|
-
}
|
|
4745
|
-
|
|
4746
|
-
render(() => (
|
|
4747
|
-
<QueryClientProvider client={queryClient}>
|
|
4748
|
-
<Page />
|
|
4749
|
-
</QueryClientProvider>
|
|
4750
|
-
))
|
|
4751
|
-
await waitFor(() => screen.getByText('Data: selected 101')) // 99 + 2
|
|
4752
|
-
expect(selectRun).toBe(1)
|
|
4753
|
-
|
|
4754
|
-
await waitFor(() => screen.getByText('Data: selected 2')) // 0 + 2
|
|
4755
|
-
expect(selectRun).toBe(2)
|
|
4756
|
-
|
|
4757
|
-
fireEvent.click(screen.getByRole('button', { name: /inc/i }))
|
|
4758
|
-
|
|
4759
|
-
await waitFor(() => screen.getByText('Data: selected 3')) // 0 + 3
|
|
4760
|
-
expect(selectRun).toBe(3)
|
|
4761
|
-
})
|
|
4762
|
-
|
|
4763
4501
|
it('select should always return the correct state', async () => {
|
|
4764
4502
|
const key1 = queryKey()
|
|
4765
4503
|
|
|
@@ -4775,20 +4513,18 @@ describe('createQuery', () => {
|
|
|
4775
4513
|
setForceValue((prev) => prev + 1)
|
|
4776
4514
|
}
|
|
4777
4515
|
|
|
4778
|
-
const state = createQuery(
|
|
4779
|
-
key1,
|
|
4780
|
-
async () => {
|
|
4516
|
+
const state = createQuery(() => ({
|
|
4517
|
+
queryKey: key1,
|
|
4518
|
+
queryFn: async () => {
|
|
4781
4519
|
await sleep(10)
|
|
4782
4520
|
return 0
|
|
4783
4521
|
},
|
|
4784
|
-
{
|
|
4785
|
-
|
|
4786
|
-
|
|
4787
|
-
return (data: number) => `selected ${data + currentCount}`
|
|
4788
|
-
},
|
|
4789
|
-
placeholderData: 99,
|
|
4522
|
+
get select() {
|
|
4523
|
+
const currentCount = count()
|
|
4524
|
+
return (data: number) => `selected ${data + currentCount}`
|
|
4790
4525
|
},
|
|
4791
|
-
|
|
4526
|
+
placeholderData: 99,
|
|
4527
|
+
}))
|
|
4792
4528
|
|
|
4793
4529
|
return (
|
|
4794
4530
|
<div>
|
|
@@ -4827,16 +4563,14 @@ describe('createQuery', () => {
|
|
|
4827
4563
|
function Page() {
|
|
4828
4564
|
const [forceValue, setForceValue] = createSignal(1)
|
|
4829
4565
|
|
|
4830
|
-
const state = createQuery(
|
|
4831
|
-
key1,
|
|
4832
|
-
async () => {
|
|
4566
|
+
const state = createQuery(() => ({
|
|
4567
|
+
queryKey: key1,
|
|
4568
|
+
queryFn: async () => {
|
|
4833
4569
|
await sleep(10)
|
|
4834
4570
|
return [1, 2]
|
|
4835
4571
|
},
|
|
4836
|
-
|
|
4837
|
-
|
|
4838
|
-
},
|
|
4839
|
-
)
|
|
4572
|
+
select: (res) => res.map((x) => x + 1),
|
|
4573
|
+
}))
|
|
4840
4574
|
|
|
4841
4575
|
createEffect(() => {
|
|
4842
4576
|
if (state.data) {
|
|
@@ -4889,7 +4623,7 @@ describe('createQuery', () => {
|
|
|
4889
4623
|
}
|
|
4890
4624
|
|
|
4891
4625
|
function Page() {
|
|
4892
|
-
const state = createQuery(key, queryFn)
|
|
4626
|
+
const state = createQuery(() => ({ queryKey: key, queryFn }))
|
|
4893
4627
|
return (
|
|
4894
4628
|
<div>
|
|
4895
4629
|
<h1>Status: {state.status}</h1>
|
|
@@ -4916,16 +4650,20 @@ describe('createQuery', () => {
|
|
|
4916
4650
|
|
|
4917
4651
|
const queryFn: QueryFunction<
|
|
4918
4652
|
string,
|
|
4919
|
-
readonly [
|
|
4653
|
+
readonly [typeof key, number]
|
|
4920
4654
|
> = async (ctx) => {
|
|
4921
4655
|
const [, limit] = ctx.queryKey
|
|
4656
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
4922
4657
|
const value = limit % 2 && ctx.signal ? 'abort' : `data ${limit}`
|
|
4923
4658
|
await sleep(25)
|
|
4924
4659
|
return value
|
|
4925
4660
|
}
|
|
4926
4661
|
|
|
4927
4662
|
function Page(props: { limit: number }) {
|
|
4928
|
-
const state = createQuery(() =>
|
|
4663
|
+
const state = createQuery(() => ({
|
|
4664
|
+
queryKey: [key, props.limit] as const,
|
|
4665
|
+
queryFn,
|
|
4666
|
+
}))
|
|
4929
4667
|
states[props.limit] = state
|
|
4930
4668
|
return (
|
|
4931
4669
|
<div>
|
|
@@ -4951,27 +4689,27 @@ describe('createQuery', () => {
|
|
|
4951
4689
|
|
|
4952
4690
|
await waitFor(() => expect(states).toHaveLength(4))
|
|
4953
4691
|
|
|
4954
|
-
expect(queryCache.find([key
|
|
4692
|
+
expect(queryCache.find({ queryKey: [key, 0] })?.state).toMatchObject({
|
|
4955
4693
|
data: 'data 0',
|
|
4956
4694
|
status: 'success',
|
|
4957
4695
|
dataUpdateCount: 1,
|
|
4958
4696
|
})
|
|
4959
4697
|
|
|
4960
|
-
expect(queryCache.find([key
|
|
4698
|
+
expect(queryCache.find({ queryKey: [key, 1] })?.state).toMatchObject({
|
|
4961
4699
|
data: undefined,
|
|
4962
|
-
status: '
|
|
4700
|
+
status: 'pending',
|
|
4963
4701
|
fetchStatus: 'idle',
|
|
4964
4702
|
})
|
|
4965
4703
|
|
|
4966
|
-
expect(queryCache.find([key
|
|
4704
|
+
expect(queryCache.find({ queryKey: [key, 2] })?.state).toMatchObject({
|
|
4967
4705
|
data: 'data 2',
|
|
4968
4706
|
status: 'success',
|
|
4969
4707
|
dataUpdateCount: 1,
|
|
4970
4708
|
})
|
|
4971
4709
|
|
|
4972
|
-
expect(queryCache.find([key
|
|
4710
|
+
expect(queryCache.find({ queryKey: [key, 3] })?.state).toMatchObject({
|
|
4973
4711
|
data: undefined,
|
|
4974
|
-
status: '
|
|
4712
|
+
status: 'pending',
|
|
4975
4713
|
fetchStatus: 'idle',
|
|
4976
4714
|
})
|
|
4977
4715
|
})
|
|
@@ -4989,7 +4727,7 @@ describe('createQuery', () => {
|
|
|
4989
4727
|
const [id, setId] = createSignal(1)
|
|
4990
4728
|
const [hasChanged, setHasChanged] = createSignal(false)
|
|
4991
4729
|
|
|
4992
|
-
const state = createQuery(() => [key
|
|
4730
|
+
const state = createQuery(() => ({ queryKey: [key, id()], queryFn }))
|
|
4993
4731
|
|
|
4994
4732
|
createRenderEffect(() => {
|
|
4995
4733
|
states.push({ ...state })
|
|
@@ -5015,7 +4753,7 @@ describe('createQuery', () => {
|
|
|
5015
4753
|
expect(states.length).toBe(2)
|
|
5016
4754
|
// Load query 1
|
|
5017
4755
|
expect(states[0]).toMatchObject({
|
|
5018
|
-
status: '
|
|
4756
|
+
status: 'pending',
|
|
5019
4757
|
error: null,
|
|
5020
4758
|
})
|
|
5021
4759
|
// No rerenders - No state updates
|
|
@@ -5032,15 +4770,15 @@ describe('createQuery', () => {
|
|
|
5032
4770
|
let count = 0
|
|
5033
4771
|
|
|
5034
4772
|
function Page() {
|
|
5035
|
-
const state = createQuery(
|
|
5036
|
-
key,
|
|
5037
|
-
async () => {
|
|
4773
|
+
const state = createQuery(() => ({
|
|
4774
|
+
queryKey: key,
|
|
4775
|
+
queryFn: async () => {
|
|
5038
4776
|
await sleep(10)
|
|
5039
4777
|
count++
|
|
5040
4778
|
return count
|
|
5041
4779
|
},
|
|
5042
|
-
|
|
5043
|
-
)
|
|
4780
|
+
staleTime: Infinity,
|
|
4781
|
+
}))
|
|
5044
4782
|
|
|
5045
4783
|
createRenderEffect(() => {
|
|
5046
4784
|
states.push({ ...state })
|
|
@@ -5048,7 +4786,9 @@ describe('createQuery', () => {
|
|
|
5048
4786
|
|
|
5049
4787
|
return (
|
|
5050
4788
|
<div>
|
|
5051
|
-
<button onClick={() => queryClient.resetQueries(key
|
|
4789
|
+
<button onClick={() => queryClient.resetQueries({ queryKey: key })}>
|
|
4790
|
+
reset
|
|
4791
|
+
</button>
|
|
5052
4792
|
<div>data: {state.data ?? 'null'}</div>
|
|
5053
4793
|
<div>isFetching: {state.isFetching}</div>
|
|
5054
4794
|
</div>
|
|
@@ -5071,29 +4811,27 @@ describe('createQuery', () => {
|
|
|
5071
4811
|
expect(count).toBe(2)
|
|
5072
4812
|
|
|
5073
4813
|
expect(states[0]).toMatchObject({
|
|
5074
|
-
|
|
5075
|
-
isLoading: true,
|
|
4814
|
+
isPending: true,
|
|
5076
4815
|
isFetching: true,
|
|
5077
4816
|
isSuccess: false,
|
|
5078
4817
|
isStale: true,
|
|
5079
4818
|
})
|
|
5080
4819
|
expect(states[1]).toMatchObject({
|
|
5081
4820
|
data: 1,
|
|
5082
|
-
|
|
4821
|
+
isPending: false,
|
|
5083
4822
|
isFetching: false,
|
|
5084
4823
|
isSuccess: true,
|
|
5085
4824
|
isStale: false,
|
|
5086
4825
|
})
|
|
5087
4826
|
expect(states[2]).toMatchObject({
|
|
5088
|
-
|
|
5089
|
-
isLoading: true,
|
|
4827
|
+
isPending: true,
|
|
5090
4828
|
isFetching: true,
|
|
5091
4829
|
isSuccess: false,
|
|
5092
4830
|
isStale: true,
|
|
5093
4831
|
})
|
|
5094
4832
|
expect(states[3]).toMatchObject({
|
|
5095
4833
|
data: 2,
|
|
5096
|
-
|
|
4834
|
+
isPending: false,
|
|
5097
4835
|
isFetching: false,
|
|
5098
4836
|
isSuccess: true,
|
|
5099
4837
|
isStale: false,
|
|
@@ -5106,15 +4844,17 @@ describe('createQuery', () => {
|
|
|
5106
4844
|
let count = 0
|
|
5107
4845
|
|
|
5108
4846
|
function Page() {
|
|
5109
|
-
const state = createQuery(
|
|
5110
|
-
key,
|
|
5111
|
-
async () => {
|
|
4847
|
+
const state = createQuery(() => ({
|
|
4848
|
+
queryKey: key,
|
|
4849
|
+
queryFn: async () => {
|
|
5112
4850
|
await sleep(10)
|
|
5113
4851
|
count++
|
|
5114
4852
|
return count
|
|
5115
4853
|
},
|
|
5116
|
-
|
|
5117
|
-
|
|
4854
|
+
staleTime: Infinity,
|
|
4855
|
+
enabled: false,
|
|
4856
|
+
notifyOnChangeProps: 'all',
|
|
4857
|
+
}))
|
|
5118
4858
|
|
|
5119
4859
|
createRenderEffect(() => {
|
|
5120
4860
|
states.push({ ...state })
|
|
@@ -5125,7 +4865,9 @@ describe('createQuery', () => {
|
|
|
5125
4865
|
return (
|
|
5126
4866
|
<div>
|
|
5127
4867
|
<button onClick={() => refetch()}>refetch</button>
|
|
5128
|
-
<button onClick={() => queryClient.resetQueries(key
|
|
4868
|
+
<button onClick={() => queryClient.resetQueries({ queryKey: key })}>
|
|
4869
|
+
reset
|
|
4870
|
+
</button>
|
|
5129
4871
|
<div>data: {state.data ?? 'null'}</div>
|
|
5130
4872
|
</div>
|
|
5131
4873
|
)
|
|
@@ -5149,29 +4891,26 @@ describe('createQuery', () => {
|
|
|
5149
4891
|
expect(count).toBe(1)
|
|
5150
4892
|
|
|
5151
4893
|
expect(states[0]).toMatchObject({
|
|
5152
|
-
|
|
5153
|
-
isLoading: true,
|
|
4894
|
+
isPending: true,
|
|
5154
4895
|
isFetching: false,
|
|
5155
4896
|
isSuccess: false,
|
|
5156
4897
|
isStale: true,
|
|
5157
4898
|
})
|
|
5158
4899
|
expect(states[1]).toMatchObject({
|
|
5159
|
-
|
|
5160
|
-
isLoading: true,
|
|
4900
|
+
isPending: true,
|
|
5161
4901
|
isFetching: true,
|
|
5162
4902
|
isSuccess: false,
|
|
5163
4903
|
isStale: true,
|
|
5164
4904
|
})
|
|
5165
4905
|
expect(states[2]).toMatchObject({
|
|
5166
4906
|
data: 1,
|
|
5167
|
-
|
|
4907
|
+
isPending: false,
|
|
5168
4908
|
isFetching: false,
|
|
5169
4909
|
isSuccess: true,
|
|
5170
4910
|
isStale: false,
|
|
5171
4911
|
})
|
|
5172
4912
|
expect(states[3]).toMatchObject({
|
|
5173
|
-
|
|
5174
|
-
isLoading: true,
|
|
4913
|
+
isPending: true,
|
|
5175
4914
|
isFetching: false,
|
|
5176
4915
|
isSuccess: false,
|
|
5177
4916
|
isStale: true,
|
|
@@ -5190,7 +4929,12 @@ describe('createQuery', () => {
|
|
|
5190
4929
|
}
|
|
5191
4930
|
|
|
5192
4931
|
function Page() {
|
|
5193
|
-
const state = createQuery(
|
|
4932
|
+
const state = createQuery(() => ({
|
|
4933
|
+
queryKey: key,
|
|
4934
|
+
queryFn: () => 'test',
|
|
4935
|
+
queryKeyHashFn,
|
|
4936
|
+
}))
|
|
4937
|
+
|
|
5194
4938
|
createEffect(
|
|
5195
4939
|
on(
|
|
5196
4940
|
() => state.status,
|
|
@@ -5221,20 +4965,20 @@ describe('createQuery', () => {
|
|
|
5221
4965
|
})
|
|
5222
4966
|
|
|
5223
4967
|
function Page(props: { enabled: boolean }) {
|
|
5224
|
-
const state = createQuery(() =>
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
4968
|
+
const state = createQuery(() => ({
|
|
4969
|
+
queryKey: ['key'],
|
|
4970
|
+
queryFn,
|
|
4971
|
+
enabled: props.enabled,
|
|
5228
4972
|
retry: false,
|
|
5229
4973
|
retryOnMount: false,
|
|
5230
4974
|
refetchOnMount: false,
|
|
5231
4975
|
refetchOnWindowFocus: false,
|
|
5232
|
-
})
|
|
4976
|
+
}))
|
|
5233
4977
|
|
|
5234
4978
|
return (
|
|
5235
4979
|
<Switch fallback={<div>rendered</div>}>
|
|
5236
|
-
<Match when={state.
|
|
5237
|
-
<div>status:
|
|
4980
|
+
<Match when={state.isPending}>
|
|
4981
|
+
<div>status: pending</div>
|
|
5238
4982
|
</Match>
|
|
5239
4983
|
<Match when={state.error instanceof Error}>
|
|
5240
4984
|
<div>error</div>
|
|
@@ -5264,7 +5008,7 @@ describe('createQuery', () => {
|
|
|
5264
5008
|
))
|
|
5265
5009
|
|
|
5266
5010
|
// initial state check
|
|
5267
|
-
screen.getByText('status:
|
|
5011
|
+
screen.getByText('status: pending')
|
|
5268
5012
|
|
|
5269
5013
|
// // render error state component
|
|
5270
5014
|
await waitFor(() => screen.getByText('error'))
|
|
@@ -5282,9 +5026,9 @@ describe('createQuery', () => {
|
|
|
5282
5026
|
|
|
5283
5027
|
it('should refetch when query key changed when previous status is error', async () => {
|
|
5284
5028
|
function Page(props: { id: number }) {
|
|
5285
|
-
const state = createQuery(
|
|
5286
|
-
|
|
5287
|
-
async () => {
|
|
5029
|
+
const state = createQuery(() => ({
|
|
5030
|
+
queryKey: [props.id],
|
|
5031
|
+
queryFn: async () => {
|
|
5288
5032
|
await sleep(10)
|
|
5289
5033
|
if (props.id % 2 === 1) {
|
|
5290
5034
|
return Promise.reject(new Error('Error'))
|
|
@@ -5292,18 +5036,16 @@ describe('createQuery', () => {
|
|
|
5292
5036
|
return 'data'
|
|
5293
5037
|
}
|
|
5294
5038
|
},
|
|
5295
|
-
|
|
5296
|
-
|
|
5297
|
-
|
|
5298
|
-
|
|
5299
|
-
|
|
5300
|
-
},
|
|
5301
|
-
)
|
|
5039
|
+
retry: false,
|
|
5040
|
+
retryOnMount: false,
|
|
5041
|
+
refetchOnMount: false,
|
|
5042
|
+
refetchOnWindowFocus: false,
|
|
5043
|
+
}))
|
|
5302
5044
|
|
|
5303
5045
|
return (
|
|
5304
5046
|
<Switch fallback={<div>rendered</div>}>
|
|
5305
|
-
<Match when={state.
|
|
5306
|
-
<div>status:
|
|
5047
|
+
<Match when={state.isPending}>
|
|
5048
|
+
<div>status: pending</div>
|
|
5307
5049
|
</Match>
|
|
5308
5050
|
<Match when={state.error instanceof Error}>
|
|
5309
5051
|
<div>error</div>
|
|
@@ -5333,7 +5075,7 @@ describe('createQuery', () => {
|
|
|
5333
5075
|
))
|
|
5334
5076
|
|
|
5335
5077
|
// initial state check
|
|
5336
|
-
screen.getByText('status:
|
|
5078
|
+
screen.getByText('status: pending')
|
|
5337
5079
|
|
|
5338
5080
|
// render error state component
|
|
5339
5081
|
await waitFor(() => screen.getByText('error'))
|
|
@@ -5349,19 +5091,17 @@ describe('createQuery', () => {
|
|
|
5349
5091
|
|
|
5350
5092
|
it('should refetch when query key changed when switching between erroneous queries', async () => {
|
|
5351
5093
|
function Page(props: { id: boolean }) {
|
|
5352
|
-
const state = createQuery(
|
|
5353
|
-
|
|
5354
|
-
async () => {
|
|
5094
|
+
const state = createQuery(() => ({
|
|
5095
|
+
queryKey: [props.id],
|
|
5096
|
+
queryFn: async () => {
|
|
5355
5097
|
await sleep(10)
|
|
5356
5098
|
return Promise.reject<unknown>(new Error('Error'))
|
|
5357
5099
|
},
|
|
5358
|
-
|
|
5359
|
-
|
|
5360
|
-
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
},
|
|
5364
|
-
)
|
|
5100
|
+
retry: false,
|
|
5101
|
+
retryOnMount: false,
|
|
5102
|
+
refetchOnMount: false,
|
|
5103
|
+
refetchOnWindowFocus: false,
|
|
5104
|
+
}))
|
|
5365
5105
|
return (
|
|
5366
5106
|
<Switch fallback={<div>rendered</div>}>
|
|
5367
5107
|
<Match when={state.isFetching}>
|
|
@@ -5411,7 +5151,7 @@ describe('createQuery', () => {
|
|
|
5411
5151
|
await waitFor(() => screen.getByText('error'))
|
|
5412
5152
|
})
|
|
5413
5153
|
|
|
5414
|
-
it('should have no error in
|
|
5154
|
+
it('should have no error in pending state when refetching after error occurred', async () => {
|
|
5415
5155
|
const key = queryKey()
|
|
5416
5156
|
const states: CreateQueryResult<number>[] = []
|
|
5417
5157
|
const error = new Error('oops')
|
|
@@ -5419,9 +5159,9 @@ describe('createQuery', () => {
|
|
|
5419
5159
|
let count = 0
|
|
5420
5160
|
|
|
5421
5161
|
function Page() {
|
|
5422
|
-
const state = createQuery(
|
|
5423
|
-
key,
|
|
5424
|
-
async () => {
|
|
5162
|
+
const state = createQuery(() => ({
|
|
5163
|
+
queryKey: key,
|
|
5164
|
+
queryFn: async () => {
|
|
5425
5165
|
await sleep(10)
|
|
5426
5166
|
if (count === 0) {
|
|
5427
5167
|
count++
|
|
@@ -5429,10 +5169,8 @@ describe('createQuery', () => {
|
|
|
5429
5169
|
}
|
|
5430
5170
|
return 5
|
|
5431
5171
|
},
|
|
5432
|
-
|
|
5433
|
-
|
|
5434
|
-
},
|
|
5435
|
-
)
|
|
5172
|
+
retry: false,
|
|
5173
|
+
}))
|
|
5436
5174
|
|
|
5437
5175
|
createRenderEffect(() => {
|
|
5438
5176
|
states.push({ ...state })
|
|
@@ -5440,8 +5178,8 @@ describe('createQuery', () => {
|
|
|
5440
5178
|
|
|
5441
5179
|
return (
|
|
5442
5180
|
<Switch fallback={<div>data: {state.data}</div>}>
|
|
5443
|
-
<Match when={state.
|
|
5444
|
-
<div>status:
|
|
5181
|
+
<Match when={state.isPending}>
|
|
5182
|
+
<div>status: pending</div>
|
|
5445
5183
|
</Match>
|
|
5446
5184
|
<Match when={state.error instanceof Error}>
|
|
5447
5185
|
<div>
|
|
@@ -5467,7 +5205,7 @@ describe('createQuery', () => {
|
|
|
5467
5205
|
await waitFor(() => expect(states.length).toBe(4))
|
|
5468
5206
|
|
|
5469
5207
|
expect(states[0]).toMatchObject({
|
|
5470
|
-
status: '
|
|
5208
|
+
status: 'pending',
|
|
5471
5209
|
data: undefined,
|
|
5472
5210
|
error: null,
|
|
5473
5211
|
})
|
|
@@ -5479,7 +5217,7 @@ describe('createQuery', () => {
|
|
|
5479
5217
|
})
|
|
5480
5218
|
|
|
5481
5219
|
expect(states[2]).toMatchObject({
|
|
5482
|
-
status: '
|
|
5220
|
+
status: 'pending',
|
|
5483
5221
|
data: undefined,
|
|
5484
5222
|
error: null,
|
|
5485
5223
|
})
|
|
@@ -5499,13 +5237,13 @@ describe('createQuery', () => {
|
|
|
5499
5237
|
const states: Array<any> = []
|
|
5500
5238
|
|
|
5501
5239
|
function Page() {
|
|
5502
|
-
const state = createQuery({
|
|
5240
|
+
const state = createQuery(() => ({
|
|
5503
5241
|
queryKey: key,
|
|
5504
5242
|
queryFn: async () => {
|
|
5505
5243
|
await sleep(10)
|
|
5506
5244
|
return 'data'
|
|
5507
5245
|
},
|
|
5508
|
-
})
|
|
5246
|
+
}))
|
|
5509
5247
|
|
|
5510
5248
|
createEffect(() => {
|
|
5511
5249
|
states.push(state.fetchStatus)
|
|
@@ -5527,7 +5265,7 @@ describe('createQuery', () => {
|
|
|
5527
5265
|
</QueryClientProvider>
|
|
5528
5266
|
))
|
|
5529
5267
|
|
|
5530
|
-
await waitFor(() => screen.getByText('status:
|
|
5268
|
+
await waitFor(() => screen.getByText('status: pending, isPaused: true'))
|
|
5531
5269
|
|
|
5532
5270
|
onlineMock.mockReturnValue(true)
|
|
5533
5271
|
window.dispatchEvent(new Event('online'))
|
|
@@ -5547,14 +5285,14 @@ describe('createQuery', () => {
|
|
|
5547
5285
|
let count = 0
|
|
5548
5286
|
|
|
5549
5287
|
function Page() {
|
|
5550
|
-
const state = createQuery<unknown, string, string>({
|
|
5288
|
+
const state = createQuery<unknown, string, string>(() => ({
|
|
5551
5289
|
queryKey: key,
|
|
5552
5290
|
queryFn: async () => {
|
|
5553
5291
|
count++
|
|
5554
5292
|
await sleep(10)
|
|
5555
5293
|
return 'data' + count
|
|
5556
5294
|
},
|
|
5557
|
-
})
|
|
5295
|
+
}))
|
|
5558
5296
|
|
|
5559
5297
|
return (
|
|
5560
5298
|
<div>
|
|
@@ -5565,7 +5303,7 @@ describe('createQuery', () => {
|
|
|
5565
5303
|
<div>failureReason: {state.failureReason ?? 'null'}</div>
|
|
5566
5304
|
<div>data: {state.data}</div>
|
|
5567
5305
|
<button
|
|
5568
|
-
onClick={() => queryClient.invalidateQueries({ queryKey: key
|
|
5306
|
+
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
5569
5307
|
>
|
|
5570
5308
|
invalidate
|
|
5571
5309
|
</button>
|
|
@@ -5617,14 +5355,14 @@ describe('createQuery', () => {
|
|
|
5617
5355
|
let count = 0
|
|
5618
5356
|
|
|
5619
5357
|
function Page() {
|
|
5620
|
-
const state = createQuery({
|
|
5358
|
+
const state = createQuery(() => ({
|
|
5621
5359
|
queryKey: key,
|
|
5622
5360
|
queryFn: async () => {
|
|
5623
5361
|
count++
|
|
5624
5362
|
await sleep(10)
|
|
5625
5363
|
return 'data' + count
|
|
5626
5364
|
},
|
|
5627
|
-
})
|
|
5365
|
+
}))
|
|
5628
5366
|
|
|
5629
5367
|
return (
|
|
5630
5368
|
<div>
|
|
@@ -5633,7 +5371,7 @@ describe('createQuery', () => {
|
|
|
5633
5371
|
</div>
|
|
5634
5372
|
<div>data: {state.data}</div>
|
|
5635
5373
|
<button
|
|
5636
|
-
onClick={() => queryClient.invalidateQueries({ queryKey: key
|
|
5374
|
+
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
5637
5375
|
>
|
|
5638
5376
|
invalidate
|
|
5639
5377
|
</button>
|
|
@@ -5656,7 +5394,7 @@ describe('createQuery', () => {
|
|
|
5656
5394
|
screen.getByText('status: success, fetchStatus: paused'),
|
|
5657
5395
|
)
|
|
5658
5396
|
|
|
5659
|
-
window.dispatchEvent(new
|
|
5397
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
5660
5398
|
await sleep(15)
|
|
5661
5399
|
|
|
5662
5400
|
await waitFor(() =>
|
|
@@ -5671,14 +5409,14 @@ describe('createQuery', () => {
|
|
|
5671
5409
|
let count = 0
|
|
5672
5410
|
|
|
5673
5411
|
function Page() {
|
|
5674
|
-
const state = createQuery({
|
|
5412
|
+
const state = createQuery(() => ({
|
|
5675
5413
|
queryKey: key,
|
|
5676
5414
|
queryFn: async () => {
|
|
5677
5415
|
count++
|
|
5678
5416
|
await sleep(10)
|
|
5679
5417
|
return 'data' + count
|
|
5680
5418
|
},
|
|
5681
|
-
})
|
|
5419
|
+
}))
|
|
5682
5420
|
|
|
5683
5421
|
return (
|
|
5684
5422
|
<div>
|
|
@@ -5687,7 +5425,7 @@ describe('createQuery', () => {
|
|
|
5687
5425
|
</div>
|
|
5688
5426
|
<div>data: {state.data}</div>
|
|
5689
5427
|
<button
|
|
5690
|
-
onClick={() => queryClient.invalidateQueries({ queryKey: key
|
|
5428
|
+
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
5691
5429
|
>
|
|
5692
5430
|
invalidate
|
|
5693
5431
|
</button>
|
|
@@ -5704,7 +5442,7 @@ describe('createQuery', () => {
|
|
|
5704
5442
|
))
|
|
5705
5443
|
|
|
5706
5444
|
await waitFor(() =>
|
|
5707
|
-
screen.getByText('status:
|
|
5445
|
+
screen.getByText('status: pending, fetchStatus: paused'),
|
|
5708
5446
|
)
|
|
5709
5447
|
|
|
5710
5448
|
fireEvent.click(screen.getByRole('button', { name: /invalidate/i }))
|
|
@@ -5713,7 +5451,7 @@ describe('createQuery', () => {
|
|
|
5713
5451
|
|
|
5714
5452
|
// invalidation should not trigger a refetch
|
|
5715
5453
|
await waitFor(() =>
|
|
5716
|
-
screen.getByText('status:
|
|
5454
|
+
screen.getByText('status: pending, fetchStatus: paused'),
|
|
5717
5455
|
)
|
|
5718
5456
|
|
|
5719
5457
|
expect(count).toBe(0)
|
|
@@ -5725,7 +5463,7 @@ describe('createQuery', () => {
|
|
|
5725
5463
|
let count = 0
|
|
5726
5464
|
|
|
5727
5465
|
function Page() {
|
|
5728
|
-
const state = createQuery({
|
|
5466
|
+
const state = createQuery(() => ({
|
|
5729
5467
|
queryKey: key,
|
|
5730
5468
|
queryFn: async () => {
|
|
5731
5469
|
count++
|
|
@@ -5733,7 +5471,7 @@ describe('createQuery', () => {
|
|
|
5733
5471
|
return 'data' + count
|
|
5734
5472
|
},
|
|
5735
5473
|
initialData: 'initial',
|
|
5736
|
-
})
|
|
5474
|
+
}))
|
|
5737
5475
|
|
|
5738
5476
|
return (
|
|
5739
5477
|
<div>
|
|
@@ -5742,7 +5480,7 @@ describe('createQuery', () => {
|
|
|
5742
5480
|
</div>
|
|
5743
5481
|
<div>data: {state.data}</div>
|
|
5744
5482
|
<button
|
|
5745
|
-
onClick={() => queryClient.invalidateQueries({ queryKey: key
|
|
5483
|
+
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
5746
5484
|
>
|
|
5747
5485
|
invalidate
|
|
5748
5486
|
</button>
|
|
@@ -5783,7 +5521,7 @@ describe('createQuery', () => {
|
|
|
5783
5521
|
let count = 0
|
|
5784
5522
|
|
|
5785
5523
|
function Page() {
|
|
5786
|
-
const state = createQuery({
|
|
5524
|
+
const state = createQuery(() => ({
|
|
5787
5525
|
queryKey: key,
|
|
5788
5526
|
queryFn: async () => {
|
|
5789
5527
|
count++
|
|
@@ -5791,7 +5529,7 @@ describe('createQuery', () => {
|
|
|
5791
5529
|
return 'data' + count
|
|
5792
5530
|
},
|
|
5793
5531
|
initialData: 'initial',
|
|
5794
|
-
})
|
|
5532
|
+
}))
|
|
5795
5533
|
|
|
5796
5534
|
return (
|
|
5797
5535
|
<div>
|
|
@@ -5800,7 +5538,7 @@ describe('createQuery', () => {
|
|
|
5800
5538
|
</div>
|
|
5801
5539
|
<div>data: {state.data}</div>
|
|
5802
5540
|
<button
|
|
5803
|
-
onClick={() => queryClient.invalidateQueries({ queryKey: key
|
|
5541
|
+
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
5804
5542
|
>
|
|
5805
5543
|
invalidate
|
|
5806
5544
|
</button>
|
|
@@ -5833,7 +5571,7 @@ describe('createQuery', () => {
|
|
|
5833
5571
|
)
|
|
5834
5572
|
|
|
5835
5573
|
// triggers a second pause
|
|
5836
|
-
window.dispatchEvent(new
|
|
5574
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
5837
5575
|
|
|
5838
5576
|
onlineMock.mockReturnValue(true)
|
|
5839
5577
|
window.dispatchEvent(new Event('online'))
|
|
@@ -5854,7 +5592,7 @@ describe('createQuery', () => {
|
|
|
5854
5592
|
let count = 0
|
|
5855
5593
|
|
|
5856
5594
|
function Page() {
|
|
5857
|
-
const state = createQuery<unknown, Error>({
|
|
5595
|
+
const state = createQuery<unknown, Error>(() => ({
|
|
5858
5596
|
queryKey: key,
|
|
5859
5597
|
queryFn: async (): Promise<unknown> => {
|
|
5860
5598
|
count++
|
|
@@ -5863,7 +5601,7 @@ describe('createQuery', () => {
|
|
|
5863
5601
|
},
|
|
5864
5602
|
retry: 2,
|
|
5865
5603
|
retryDelay: 10,
|
|
5866
|
-
})
|
|
5604
|
+
}))
|
|
5867
5605
|
|
|
5868
5606
|
return (
|
|
5869
5607
|
<div>
|
|
@@ -5884,7 +5622,7 @@ describe('createQuery', () => {
|
|
|
5884
5622
|
|
|
5885
5623
|
await waitFor(() =>
|
|
5886
5624
|
screen.getByText(
|
|
5887
|
-
'status:
|
|
5625
|
+
'status: pending, fetchStatus: fetching, failureCount: 1',
|
|
5888
5626
|
),
|
|
5889
5627
|
)
|
|
5890
5628
|
await waitFor(() => screen.getByText('failureReason: failed1'))
|
|
@@ -5895,7 +5633,7 @@ describe('createQuery', () => {
|
|
|
5895
5633
|
|
|
5896
5634
|
await waitFor(() =>
|
|
5897
5635
|
screen.getByText(
|
|
5898
|
-
'status:
|
|
5636
|
+
'status: pending, fetchStatus: paused, failureCount: 1',
|
|
5899
5637
|
),
|
|
5900
5638
|
)
|
|
5901
5639
|
await waitFor(() => screen.getByText('failureReason: failed1'))
|
|
@@ -5920,14 +5658,14 @@ describe('createQuery', () => {
|
|
|
5920
5658
|
let count = 0
|
|
5921
5659
|
|
|
5922
5660
|
function Component() {
|
|
5923
|
-
const state = createQuery({
|
|
5661
|
+
const state = createQuery(() => ({
|
|
5924
5662
|
queryKey: key,
|
|
5925
5663
|
queryFn: async () => {
|
|
5926
5664
|
count++
|
|
5927
5665
|
await sleep(10)
|
|
5928
5666
|
return 'data' + count
|
|
5929
5667
|
},
|
|
5930
|
-
})
|
|
5668
|
+
}))
|
|
5931
5669
|
|
|
5932
5670
|
return (
|
|
5933
5671
|
<div>
|
|
@@ -5959,7 +5697,7 @@ describe('createQuery', () => {
|
|
|
5959
5697
|
))
|
|
5960
5698
|
|
|
5961
5699
|
await waitFor(() =>
|
|
5962
|
-
screen.getByText('status:
|
|
5700
|
+
screen.getByText('status: pending, fetchStatus: paused'),
|
|
5963
5701
|
)
|
|
5964
5702
|
|
|
5965
5703
|
fireEvent.click(screen.getByRole('button', { name: /hide/i }))
|
|
@@ -5969,7 +5707,7 @@ describe('createQuery', () => {
|
|
|
5969
5707
|
|
|
5970
5708
|
await sleep(15)
|
|
5971
5709
|
|
|
5972
|
-
expect(queryClient.getQueryState(key
|
|
5710
|
+
expect(queryClient.getQueryState(key)).toMatchObject({
|
|
5973
5711
|
fetchStatus: 'idle',
|
|
5974
5712
|
status: 'success',
|
|
5975
5713
|
})
|
|
@@ -5984,7 +5722,7 @@ describe('createQuery', () => {
|
|
|
5984
5722
|
let count = 0
|
|
5985
5723
|
|
|
5986
5724
|
function Page() {
|
|
5987
|
-
const state = createQuery({
|
|
5725
|
+
const state = createQuery(() => ({
|
|
5988
5726
|
queryKey: key,
|
|
5989
5727
|
queryFn: async () => {
|
|
5990
5728
|
count++
|
|
@@ -5992,12 +5730,12 @@ describe('createQuery', () => {
|
|
|
5992
5730
|
return 'data' + count
|
|
5993
5731
|
},
|
|
5994
5732
|
refetchOnReconnect: false,
|
|
5995
|
-
})
|
|
5733
|
+
}))
|
|
5996
5734
|
|
|
5997
5735
|
return (
|
|
5998
5736
|
<div>
|
|
5999
5737
|
<button
|
|
6000
|
-
onClick={() => queryClient.cancelQueries({ queryKey: key
|
|
5738
|
+
onClick={() => queryClient.cancelQueries({ queryKey: key })}
|
|
6001
5739
|
>
|
|
6002
5740
|
cancel
|
|
6003
5741
|
</button>
|
|
@@ -6018,13 +5756,13 @@ describe('createQuery', () => {
|
|
|
6018
5756
|
))
|
|
6019
5757
|
|
|
6020
5758
|
await waitFor(() =>
|
|
6021
|
-
screen.getByText('status:
|
|
5759
|
+
screen.getByText('status: pending, fetchStatus: paused'),
|
|
6022
5760
|
)
|
|
6023
5761
|
|
|
6024
5762
|
fireEvent.click(screen.getByRole('button', { name: /cancel/i }))
|
|
6025
5763
|
|
|
6026
5764
|
await waitFor(() =>
|
|
6027
|
-
screen.getByText('status:
|
|
5765
|
+
screen.getByText('status: pending, fetchStatus: idle'),
|
|
6028
5766
|
)
|
|
6029
5767
|
|
|
6030
5768
|
expect(count).toBe(0)
|
|
@@ -6035,7 +5773,7 @@ describe('createQuery', () => {
|
|
|
6035
5773
|
await sleep(15)
|
|
6036
5774
|
|
|
6037
5775
|
await waitFor(() =>
|
|
6038
|
-
screen.getByText('status:
|
|
5776
|
+
screen.getByText('status: pending, fetchStatus: idle'),
|
|
6039
5777
|
)
|
|
6040
5778
|
|
|
6041
5779
|
expect(count).toBe(0)
|
|
@@ -6048,14 +5786,14 @@ describe('createQuery', () => {
|
|
|
6048
5786
|
let count = 0
|
|
6049
5787
|
|
|
6050
5788
|
function Component() {
|
|
6051
|
-
const state = createQuery({
|
|
5789
|
+
const state = createQuery(() => ({
|
|
6052
5790
|
queryKey: key,
|
|
6053
|
-
queryFn: async ({ signal }) => {
|
|
5791
|
+
queryFn: async ({ signal: _signal }) => {
|
|
6054
5792
|
count++
|
|
6055
5793
|
await sleep(10)
|
|
6056
|
-
return
|
|
5794
|
+
return `signal${count}`
|
|
6057
5795
|
},
|
|
6058
|
-
})
|
|
5796
|
+
}))
|
|
6059
5797
|
|
|
6060
5798
|
return (
|
|
6061
5799
|
<div>
|
|
@@ -6075,7 +5813,7 @@ describe('createQuery', () => {
|
|
|
6075
5813
|
{show() && <Component />}
|
|
6076
5814
|
<button onClick={() => setShow(false)}>hide</button>
|
|
6077
5815
|
<button
|
|
6078
|
-
onClick={() => queryClient.invalidateQueries({ queryKey: key
|
|
5816
|
+
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
6079
5817
|
>
|
|
6080
5818
|
invalidate
|
|
6081
5819
|
</button>
|
|
@@ -6110,7 +5848,7 @@ describe('createQuery', () => {
|
|
|
6110
5848
|
|
|
6111
5849
|
await sleep(15)
|
|
6112
5850
|
|
|
6113
|
-
expect(queryClient.getQueryState(key
|
|
5851
|
+
expect(queryClient.getQueryState(key)).toMatchObject({
|
|
6114
5852
|
fetchStatus: 'idle',
|
|
6115
5853
|
status: 'success',
|
|
6116
5854
|
})
|
|
@@ -6129,7 +5867,7 @@ describe('createQuery', () => {
|
|
|
6129
5867
|
let count = 0
|
|
6130
5868
|
|
|
6131
5869
|
function Page() {
|
|
6132
|
-
const state = createQuery({
|
|
5870
|
+
const state = createQuery(() => ({
|
|
6133
5871
|
queryKey: key,
|
|
6134
5872
|
queryFn: async () => {
|
|
6135
5873
|
count++
|
|
@@ -6137,7 +5875,7 @@ describe('createQuery', () => {
|
|
|
6137
5875
|
return 'data ' + count
|
|
6138
5876
|
},
|
|
6139
5877
|
networkMode: 'always',
|
|
6140
|
-
})
|
|
5878
|
+
}))
|
|
6141
5879
|
|
|
6142
5880
|
return (
|
|
6143
5881
|
<div>
|
|
@@ -6171,7 +5909,7 @@ describe('createQuery', () => {
|
|
|
6171
5909
|
let count = 0
|
|
6172
5910
|
|
|
6173
5911
|
function Page() {
|
|
6174
|
-
const state = createQuery({
|
|
5912
|
+
const state = createQuery(() => ({
|
|
6175
5913
|
queryKey: key,
|
|
6176
5914
|
queryFn: async (): Promise<unknown> => {
|
|
6177
5915
|
count++
|
|
@@ -6181,7 +5919,7 @@ describe('createQuery', () => {
|
|
|
6181
5919
|
networkMode: 'always',
|
|
6182
5920
|
retry: 1,
|
|
6183
5921
|
retryDelay: 5,
|
|
6184
|
-
})
|
|
5922
|
+
}))
|
|
6185
5923
|
|
|
6186
5924
|
return (
|
|
6187
5925
|
<div>
|
|
@@ -6221,7 +5959,7 @@ describe('createQuery', () => {
|
|
|
6221
5959
|
let count = 0
|
|
6222
5960
|
|
|
6223
5961
|
function Page() {
|
|
6224
|
-
const state = createQuery<unknown, Error>({
|
|
5962
|
+
const state = createQuery<unknown, Error>(() => ({
|
|
6225
5963
|
queryKey: key,
|
|
6226
5964
|
queryFn: async (): Promise<unknown> => {
|
|
6227
5965
|
count++
|
|
@@ -6231,7 +5969,7 @@ describe('createQuery', () => {
|
|
|
6231
5969
|
retry: 2,
|
|
6232
5970
|
retryDelay: 1,
|
|
6233
5971
|
networkMode: 'offlineFirst',
|
|
6234
|
-
})
|
|
5972
|
+
}))
|
|
6235
5973
|
|
|
6236
5974
|
return (
|
|
6237
5975
|
<div>
|
|
@@ -6252,7 +5990,7 @@ describe('createQuery', () => {
|
|
|
6252
5990
|
|
|
6253
5991
|
await waitFor(() =>
|
|
6254
5992
|
screen.getByText(
|
|
6255
|
-
'status:
|
|
5993
|
+
'status: pending, fetchStatus: paused, failureCount: 1',
|
|
6256
5994
|
),
|
|
6257
5995
|
)
|
|
6258
5996
|
await waitFor(() => screen.getByText('failureReason: failed1'))
|
|
@@ -6283,10 +6021,12 @@ describe('createQuery', () => {
|
|
|
6283
6021
|
}
|
|
6284
6022
|
|
|
6285
6023
|
function Page() {
|
|
6286
|
-
const state = createQuery(
|
|
6024
|
+
const state = createQuery(() => ({
|
|
6025
|
+
queryKey: key,
|
|
6026
|
+
queryFn,
|
|
6287
6027
|
retry: false,
|
|
6288
6028
|
retryOnMount: false,
|
|
6289
|
-
})
|
|
6029
|
+
}))
|
|
6290
6030
|
|
|
6291
6031
|
createRenderEffect(() => {
|
|
6292
6032
|
states.push({ ...state })
|
|
@@ -6295,7 +6035,7 @@ describe('createQuery', () => {
|
|
|
6295
6035
|
return <></>
|
|
6296
6036
|
}
|
|
6297
6037
|
|
|
6298
|
-
await queryClient.prefetchQuery(key
|
|
6038
|
+
await queryClient.prefetchQuery({ queryKey: key, queryFn })
|
|
6299
6039
|
render(() => (
|
|
6300
6040
|
<QueryClientProvider client={queryClient}>
|
|
6301
6041
|
<Page />
|
|
@@ -6315,11 +6055,15 @@ describe('createQuery', () => {
|
|
|
6315
6055
|
const onSuccess = jest.fn()
|
|
6316
6056
|
|
|
6317
6057
|
function Page() {
|
|
6318
|
-
const state = createQuery(
|
|
6058
|
+
const state = createQuery(() => ({
|
|
6059
|
+
queryKey: key,
|
|
6060
|
+
queryFn: () => 'data',
|
|
6061
|
+
onSuccess,
|
|
6062
|
+
}))
|
|
6319
6063
|
return (
|
|
6320
6064
|
<div>
|
|
6321
6065
|
<div>data: {state.data}</div>
|
|
6322
|
-
<button onClick={() => queryClient.setQueryData(key
|
|
6066
|
+
<button onClick={() => queryClient.setQueryData(key, 'newData')}>
|
|
6323
6067
|
setQueryData
|
|
6324
6068
|
</button>
|
|
6325
6069
|
</div>
|
|
@@ -6344,14 +6088,19 @@ describe('createQuery', () => {
|
|
|
6344
6088
|
const key = queryKey()
|
|
6345
6089
|
|
|
6346
6090
|
function Page() {
|
|
6347
|
-
const state = createQuery(
|
|
6091
|
+
const state = createQuery(() => ({
|
|
6092
|
+
queryKey: key,
|
|
6093
|
+
queryFn: () => 'data',
|
|
6094
|
+
}))
|
|
6348
6095
|
return (
|
|
6349
6096
|
<div>
|
|
6350
6097
|
<div>data: {state.data}</div>
|
|
6351
6098
|
<div>dataUpdatedAt: {state.dataUpdatedAt}</div>
|
|
6352
6099
|
<button
|
|
6353
6100
|
onClick={() =>
|
|
6354
|
-
queryClient.setQueryData(key
|
|
6101
|
+
queryClient.setQueryData(key, 'newData', {
|
|
6102
|
+
updatedAt: 100,
|
|
6103
|
+
})
|
|
6355
6104
|
}
|
|
6356
6105
|
>
|
|
6357
6106
|
setQueryData
|
|
@@ -6379,15 +6128,13 @@ describe('createQuery', () => {
|
|
|
6379
6128
|
const error = new Error('oops')
|
|
6380
6129
|
|
|
6381
6130
|
function Page() {
|
|
6382
|
-
const state = createQuery(
|
|
6383
|
-
key,
|
|
6384
|
-
async (): Promise<unknown> => {
|
|
6131
|
+
const state = createQuery(() => ({
|
|
6132
|
+
queryKey: key,
|
|
6133
|
+
queryFn: async (): Promise<unknown> => {
|
|
6385
6134
|
throw error
|
|
6386
6135
|
},
|
|
6387
|
-
|
|
6388
|
-
|
|
6389
|
-
},
|
|
6390
|
-
)
|
|
6136
|
+
retry: false,
|
|
6137
|
+
}))
|
|
6391
6138
|
return (
|
|
6392
6139
|
<div>
|
|
6393
6140
|
<button onClick={() => state.refetch()}>refetch</button>
|
|
@@ -6407,4 +6154,27 @@ describe('createQuery', () => {
|
|
|
6407
6154
|
fireEvent.click(fetchBtn)
|
|
6408
6155
|
await waitFor(() => screen.getByText('data: 3'))
|
|
6409
6156
|
})
|
|
6157
|
+
|
|
6158
|
+
it('should use provided custom queryClient', async () => {
|
|
6159
|
+
const key = queryKey()
|
|
6160
|
+
const queryFn = () => {
|
|
6161
|
+
return Promise.resolve('custom client')
|
|
6162
|
+
}
|
|
6163
|
+
|
|
6164
|
+
function Page() {
|
|
6165
|
+
const state = createQuery(
|
|
6166
|
+
() => ({ queryKey: key, queryFn }),
|
|
6167
|
+
() => queryClient,
|
|
6168
|
+
)
|
|
6169
|
+
return (
|
|
6170
|
+
<div>
|
|
6171
|
+
<h1>Status: {state.data}</h1>
|
|
6172
|
+
</div>
|
|
6173
|
+
)
|
|
6174
|
+
}
|
|
6175
|
+
|
|
6176
|
+
render(() => <Page />)
|
|
6177
|
+
|
|
6178
|
+
await waitFor(() => screen.getByText('Status: custom client'))
|
|
6179
|
+
})
|
|
6410
6180
|
})
|