@tanstack/query-core 5.0.0-alpha.1 → 5.0.0-alpha.18
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/lib/_virtual/_rollupPluginBabelHelpers.esm.js +13 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js +16 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
- package/build/lib/focusManager.esm.js +31 -15
- package/build/lib/focusManager.esm.js.map +1 -1
- package/build/lib/focusManager.js +31 -15
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/hydration.d.ts +2 -6
- package/build/lib/hydration.esm.js +9 -20
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +9 -20
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +4 -18
- package/build/lib/hydration.mjs.map +1 -1
- package/build/lib/index.d.ts +4 -4
- package/build/lib/index.esm.js +1 -1
- package/build/lib/index.js +1 -0
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -1
- package/build/lib/infiniteQueryBehavior.d.ts +2 -2
- package/build/lib/infiniteQueryBehavior.esm.js +52 -48
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +52 -48
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +48 -45
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +7 -7
- package/build/lib/infiniteQueryObserver.esm.js +7 -8
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +7 -8
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +4 -6
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +5 -4
- package/build/lib/mutation.esm.js +138 -99
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +138 -99
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +16 -2
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +5 -4
- package/build/lib/mutationCache.esm.js +34 -20
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +34 -20
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +5 -4
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.d.ts +2 -2
- package/build/lib/mutationObserver.esm.js +81 -50
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +81 -50
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +1 -0
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/onlineManager.esm.js +29 -14
- package/build/lib/onlineManager.esm.js.map +1 -1
- package/build/lib/onlineManager.js +29 -14
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/queriesObserver.esm.js +94 -69
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +94 -69
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +1 -5
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +8 -7
- package/build/lib/query.esm.js +210 -159
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +210 -159
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +2 -0
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +5 -4
- package/build/lib/queryCache.esm.js +21 -13
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +21 -13
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +5 -4
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/lib/queryClient.d.ts +7 -7
- package/build/lib/queryClient.esm.js +107 -66
- package/build/lib/queryClient.esm.js.map +1 -1
- package/build/lib/queryClient.js +107 -66
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.mjs +5 -4
- package/build/lib/queryClient.mjs.map +1 -1
- package/build/lib/queryObserver.d.ts +2 -2
- package/build/lib/queryObserver.esm.js +308 -207
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +307 -206
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.esm.js +13 -6
- package/build/lib/removable.esm.js.map +1 -1
- package/build/lib/removable.js +13 -6
- package/build/lib/removable.js.map +1 -1
- package/build/lib/retryer.d.ts +5 -5
- package/build/lib/retryer.esm.js +15 -14
- package/build/lib/retryer.esm.js.map +1 -1
- package/build/lib/retryer.js +15 -14
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/retryer.mjs.map +1 -1
- package/build/lib/tests/utils.d.ts +4 -5
- package/build/lib/types.d.ts +42 -40
- package/build/lib/utils.esm.js +1 -1
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +1 -1
- package/build/lib/utils.js.map +1 -1
- package/build/umd/index.development.js +92 -88
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/hydration.ts +18 -37
- package/src/index.ts +9 -5
- package/src/infiniteQueryBehavior.ts +52 -60
- package/src/infiniteQueryObserver.ts +17 -12
- package/src/mutation.ts +34 -7
- package/src/mutationCache.ts +15 -8
- package/src/mutationObserver.ts +3 -2
- package/src/queriesObserver.ts +3 -7
- package/src/query.ts +24 -9
- package/src/queryCache.ts +16 -10
- package/src/queryClient.ts +9 -11
- package/src/queryObserver.ts +2 -2
- package/src/retryer.ts +5 -5
- package/src/tests/focusManager.test.tsx +12 -14
- package/src/tests/hydration.test.tsx +22 -17
- package/src/tests/infiniteQueryBehavior.test.tsx +16 -9
- package/src/tests/infiniteQueryObserver.test.tsx +62 -1
- package/src/tests/mutationCache.test.tsx +61 -16
- package/src/tests/mutationObserver.test.tsx +3 -2
- package/src/tests/mutations.test.tsx +41 -9
- package/src/tests/notifyManager.test.tsx +7 -6
- package/src/tests/onlineManager.test.tsx +12 -17
- package/src/tests/queriesObserver.test.tsx +18 -17
- package/src/tests/query.test.tsx +18 -17
- package/src/tests/queryCache.test.tsx +28 -15
- package/src/tests/queryClient.test.tsx +49 -48
- package/src/tests/queryObserver.test.tsx +10 -9
- package/src/tests/utils.test.tsx +2 -1
- package/src/tests/utils.ts +5 -4
- package/src/types.ts +53 -37
package/src/query.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
CancelOptions,
|
|
10
10
|
SetDataOptions,
|
|
11
11
|
FetchStatus,
|
|
12
|
-
|
|
12
|
+
DefaultError,
|
|
13
13
|
} from './types'
|
|
14
14
|
import type { QueryCache } from './queryCache'
|
|
15
15
|
import type { QueryObserver } from './queryObserver'
|
|
@@ -34,7 +34,7 @@ interface QueryConfig<
|
|
|
34
34
|
state?: QueryState<TData, TError>
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
export interface QueryState<TData = unknown, TError =
|
|
37
|
+
export interface QueryState<TData = unknown, TError = DefaultError> {
|
|
38
38
|
data: TData | undefined
|
|
39
39
|
dataUpdateCount: number
|
|
40
40
|
dataUpdatedAt: number
|
|
@@ -65,7 +65,7 @@ export interface FetchContext<
|
|
|
65
65
|
|
|
66
66
|
export interface QueryBehavior<
|
|
67
67
|
TQueryFnData = unknown,
|
|
68
|
-
TError =
|
|
68
|
+
TError = DefaultError,
|
|
69
69
|
TData = TQueryFnData,
|
|
70
70
|
TQueryKey extends QueryKey = QueryKey,
|
|
71
71
|
> {
|
|
@@ -74,8 +74,10 @@ export interface QueryBehavior<
|
|
|
74
74
|
) => void
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
export type FetchDirection = 'forward' | 'backward'
|
|
78
|
+
|
|
77
79
|
export interface FetchMeta {
|
|
78
|
-
fetchMore?: { direction:
|
|
80
|
+
fetchMore?: { direction: FetchDirection }
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
export interface FetchOptions {
|
|
@@ -120,7 +122,7 @@ interface ContinueAction {
|
|
|
120
122
|
|
|
121
123
|
interface SetStateAction<TData, TError> {
|
|
122
124
|
type: 'setState'
|
|
123
|
-
state: QueryState<TData, TError
|
|
125
|
+
state: Partial<QueryState<TData, TError>>
|
|
124
126
|
setStateOptions?: SetStateOptions
|
|
125
127
|
}
|
|
126
128
|
|
|
@@ -142,7 +144,7 @@ export interface SetStateOptions {
|
|
|
142
144
|
|
|
143
145
|
export class Query<
|
|
144
146
|
TQueryFnData = unknown,
|
|
145
|
-
TError =
|
|
147
|
+
TError = DefaultError,
|
|
146
148
|
TData = TQueryFnData,
|
|
147
149
|
TQueryKey extends QueryKey = QueryKey,
|
|
148
150
|
> extends Removable {
|
|
@@ -211,7 +213,7 @@ export class Query<
|
|
|
211
213
|
}
|
|
212
214
|
|
|
213
215
|
setState(
|
|
214
|
-
state: QueryState<TData, TError
|
|
216
|
+
state: Partial<QueryState<TData, TError>>,
|
|
215
217
|
setStateOptions?: SetStateOptions,
|
|
216
218
|
): void {
|
|
217
219
|
this.#dispatch({ type: 'setState', state, setStateOptions })
|
|
@@ -433,7 +435,15 @@ export class Query<
|
|
|
433
435
|
|
|
434
436
|
if (!isCancelledError(error)) {
|
|
435
437
|
// Notify cache callback
|
|
436
|
-
this.#cache.config.onError?.(
|
|
438
|
+
this.#cache.config.onError?.(
|
|
439
|
+
error as any,
|
|
440
|
+
this as Query<any, any, any, any>,
|
|
441
|
+
)
|
|
442
|
+
this.#cache.config.onSettled?.(
|
|
443
|
+
this.state.data,
|
|
444
|
+
error as any,
|
|
445
|
+
this as Query<any, any, any, any>,
|
|
446
|
+
)
|
|
437
447
|
}
|
|
438
448
|
|
|
439
449
|
if (!this.isFetchingOptimistic) {
|
|
@@ -445,7 +455,7 @@ export class Query<
|
|
|
445
455
|
|
|
446
456
|
// Try to fetch the data
|
|
447
457
|
this.#retryer = createRetryer({
|
|
448
|
-
fn: context.fetchFn as () => TData
|
|
458
|
+
fn: context.fetchFn as () => Promise<TData>,
|
|
449
459
|
abort: abortController.abort.bind(abortController),
|
|
450
460
|
onSuccess: (data) => {
|
|
451
461
|
if (typeof data === 'undefined') {
|
|
@@ -462,6 +472,11 @@ export class Query<
|
|
|
462
472
|
|
|
463
473
|
// Notify cache callback
|
|
464
474
|
this.#cache.config.onSuccess?.(data, this as Query<any, any, any, any>)
|
|
475
|
+
this.#cache.config.onSettled?.(
|
|
476
|
+
data,
|
|
477
|
+
this.state.error as any,
|
|
478
|
+
this as Query<any, any, any, any>,
|
|
479
|
+
)
|
|
465
480
|
|
|
466
481
|
if (!this.isFetchingOptimistic) {
|
|
467
482
|
// Schedule query gc after fetching
|
package/src/queryCache.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
NotifyEvent,
|
|
7
7
|
QueryKey,
|
|
8
8
|
QueryOptions,
|
|
9
|
-
|
|
9
|
+
DefaultError,
|
|
10
10
|
WithRequired,
|
|
11
11
|
} from './types'
|
|
12
12
|
import { notifyManager } from './notifyManager'
|
|
@@ -17,8 +17,16 @@ import type { QueryObserver } from './queryObserver'
|
|
|
17
17
|
// TYPES
|
|
18
18
|
|
|
19
19
|
interface QueryCacheConfig {
|
|
20
|
-
onError?: (
|
|
20
|
+
onError?: (
|
|
21
|
+
error: DefaultError,
|
|
22
|
+
query: Query<unknown, unknown, unknown>,
|
|
23
|
+
) => void
|
|
21
24
|
onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void
|
|
25
|
+
onSettled?: (
|
|
26
|
+
data: unknown | undefined,
|
|
27
|
+
error: DefaultError | null,
|
|
28
|
+
query: Query<unknown, unknown, unknown>,
|
|
29
|
+
) => void
|
|
22
30
|
createStore?: () => QueryStore
|
|
23
31
|
}
|
|
24
32
|
|
|
@@ -150,7 +158,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
150
158
|
|
|
151
159
|
get<
|
|
152
160
|
TQueryFnData = unknown,
|
|
153
|
-
TError =
|
|
161
|
+
TError = DefaultError,
|
|
154
162
|
TData = TQueryFnData,
|
|
155
163
|
TQueryKey extends QueryKey = QueryKey,
|
|
156
164
|
>(
|
|
@@ -165,16 +173,14 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
165
173
|
return [...this.#queries.values()]
|
|
166
174
|
}
|
|
167
175
|
|
|
168
|
-
find<TQueryFnData = unknown, TError =
|
|
176
|
+
find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(
|
|
169
177
|
filters: WithRequired<QueryFilters, 'queryKey'>,
|
|
170
178
|
): Query<TQueryFnData, TError, TData> | undefined {
|
|
171
|
-
|
|
172
|
-
filters.exact = true
|
|
173
|
-
}
|
|
179
|
+
const defaultedFilters = { exact: true, ...filters }
|
|
174
180
|
|
|
175
|
-
return this.getAll().find((query) =>
|
|
176
|
-
|
|
177
|
-
|
|
181
|
+
return this.getAll().find((query) =>
|
|
182
|
+
matchQuery(defaultedFilters, query),
|
|
183
|
+
) as Query<TQueryFnData, TError, TData> | undefined
|
|
178
184
|
}
|
|
179
185
|
|
|
180
186
|
findAll(filters: QueryFilters = {}): Query[] {
|
package/src/queryClient.ts
CHANGED
|
@@ -24,7 +24,7 @@ import type {
|
|
|
24
24
|
RefetchQueryFilters,
|
|
25
25
|
ResetOptions,
|
|
26
26
|
SetDataOptions,
|
|
27
|
-
|
|
27
|
+
DefaultError,
|
|
28
28
|
} from './types'
|
|
29
29
|
import type { QueryState } from './query'
|
|
30
30
|
import { QueryCache } from './queryCache'
|
|
@@ -177,7 +177,7 @@ export class QueryClient {
|
|
|
177
177
|
)
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
getQueryState<TQueryFnData = unknown, TError =
|
|
180
|
+
getQueryState<TQueryFnData = unknown, TError = DefaultError>(
|
|
181
181
|
queryKey: QueryKey,
|
|
182
182
|
): QueryState<TQueryFnData, TError> | undefined {
|
|
183
183
|
return this.#queryCache.find<TQueryFnData, TError>({ queryKey })?.state
|
|
@@ -212,14 +212,12 @@ export class QueryClient {
|
|
|
212
212
|
filters: QueryFilters = {},
|
|
213
213
|
cancelOptions: CancelOptions = {},
|
|
214
214
|
): Promise<void> {
|
|
215
|
-
|
|
216
|
-
cancelOptions.revert = true
|
|
217
|
-
}
|
|
215
|
+
const defaultedCancelOptions = { revert: true, ...cancelOptions }
|
|
218
216
|
|
|
219
217
|
const promises = notifyManager.batch(() =>
|
|
220
218
|
this.#queryCache
|
|
221
219
|
.findAll(filters)
|
|
222
|
-
.map((query) => query.cancel(
|
|
220
|
+
.map((query) => query.cancel(defaultedCancelOptions)),
|
|
223
221
|
)
|
|
224
222
|
|
|
225
223
|
return Promise.all(promises).then(noop).catch(noop)
|
|
@@ -272,7 +270,7 @@ export class QueryClient {
|
|
|
272
270
|
|
|
273
271
|
fetchQuery<
|
|
274
272
|
TQueryFnData,
|
|
275
|
-
TError =
|
|
273
|
+
TError = DefaultError,
|
|
276
274
|
TData = TQueryFnData,
|
|
277
275
|
TQueryKey extends QueryKey = QueryKey,
|
|
278
276
|
TPageParam = never,
|
|
@@ -301,7 +299,7 @@ export class QueryClient {
|
|
|
301
299
|
|
|
302
300
|
prefetchQuery<
|
|
303
301
|
TQueryFnData = unknown,
|
|
304
|
-
TError =
|
|
302
|
+
TError = DefaultError,
|
|
305
303
|
TData = TQueryFnData,
|
|
306
304
|
TQueryKey extends QueryKey = QueryKey,
|
|
307
305
|
>(
|
|
@@ -312,7 +310,7 @@ export class QueryClient {
|
|
|
312
310
|
|
|
313
311
|
fetchInfiniteQuery<
|
|
314
312
|
TQueryFnData,
|
|
315
|
-
TError =
|
|
313
|
+
TError = DefaultError,
|
|
316
314
|
TData = TQueryFnData,
|
|
317
315
|
TQueryKey extends QueryKey = QueryKey,
|
|
318
316
|
TPageParam = unknown,
|
|
@@ -331,7 +329,7 @@ export class QueryClient {
|
|
|
331
329
|
|
|
332
330
|
prefetchInfiniteQuery<
|
|
333
331
|
TQueryFnData,
|
|
334
|
-
TError =
|
|
332
|
+
TError = DefaultError,
|
|
335
333
|
TData = TQueryFnData,
|
|
336
334
|
TQueryKey extends QueryKey = QueryKey,
|
|
337
335
|
TPageParam = unknown,
|
|
@@ -422,7 +420,7 @@ export class QueryClient {
|
|
|
422
420
|
|
|
423
421
|
defaultQueryOptions<
|
|
424
422
|
TQueryFnData = unknown,
|
|
425
|
-
TError =
|
|
423
|
+
TError = DefaultError,
|
|
426
424
|
TData = TQueryFnData,
|
|
427
425
|
TQueryData = TQueryFnData,
|
|
428
426
|
TQueryKey extends QueryKey = QueryKey,
|
package/src/queryObserver.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DefaultedQueryObserverOptions,
|
|
1
|
+
import type { DefaultedQueryObserverOptions, DefaultError } from './types'
|
|
2
2
|
import {
|
|
3
3
|
isServer,
|
|
4
4
|
isValidTimeout,
|
|
@@ -39,7 +39,7 @@ export interface ObserverFetchOptions extends FetchOptions {
|
|
|
39
39
|
|
|
40
40
|
export class QueryObserver<
|
|
41
41
|
TQueryFnData = unknown,
|
|
42
|
-
TError =
|
|
42
|
+
TError = DefaultError,
|
|
43
43
|
TData = TQueryFnData,
|
|
44
44
|
TQueryData = TQueryFnData,
|
|
45
45
|
TQueryKey extends QueryKey = QueryKey,
|
package/src/retryer.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { focusManager } from './focusManager'
|
|
2
2
|
import { onlineManager } from './onlineManager'
|
|
3
3
|
import { sleep } from './utils'
|
|
4
|
-
import type { CancelOptions, NetworkMode,
|
|
4
|
+
import type { CancelOptions, NetworkMode, DefaultError } from './types'
|
|
5
5
|
|
|
6
6
|
// TYPES
|
|
7
7
|
|
|
8
|
-
interface RetryerConfig<TData = unknown, TError =
|
|
8
|
+
interface RetryerConfig<TData = unknown, TError = DefaultError> {
|
|
9
9
|
fn: () => TData | Promise<TData>
|
|
10
10
|
abort?: () => void
|
|
11
11
|
onError?: (error: TError) => void
|
|
@@ -28,14 +28,14 @@ export interface Retryer<TData = unknown> {
|
|
|
28
28
|
|
|
29
29
|
export type RetryValue<TError> = boolean | number | ShouldRetryFunction<TError>
|
|
30
30
|
|
|
31
|
-
type ShouldRetryFunction<TError =
|
|
31
|
+
type ShouldRetryFunction<TError = DefaultError> = (
|
|
32
32
|
failureCount: number,
|
|
33
33
|
error: TError,
|
|
34
34
|
) => boolean
|
|
35
35
|
|
|
36
36
|
export type RetryDelayValue<TError> = number | RetryDelayFunction<TError>
|
|
37
37
|
|
|
38
|
-
type RetryDelayFunction<TError =
|
|
38
|
+
type RetryDelayFunction<TError = DefaultError> = (
|
|
39
39
|
failureCount: number,
|
|
40
40
|
error: TError,
|
|
41
41
|
) => number
|
|
@@ -63,7 +63,7 @@ export function isCancelledError(value: any): value is CancelledError {
|
|
|
63
63
|
return value instanceof CancelledError
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
export function createRetryer<TData = unknown, TError =
|
|
66
|
+
export function createRetryer<TData = unknown, TError = DefaultError>(
|
|
67
67
|
config: RetryerConfig<TData, TError>,
|
|
68
68
|
): Retryer<TData> {
|
|
69
69
|
let isRetryCancelled = false
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { sleep } from '../utils'
|
|
2
2
|
import { FocusManager } from '../focusManager'
|
|
3
3
|
import { setIsServer } from './utils'
|
|
4
|
+
import { vi } from 'vitest'
|
|
4
5
|
|
|
5
6
|
describe('focusManager', () => {
|
|
6
7
|
let focusManager: FocusManager
|
|
7
8
|
beforeEach(() => {
|
|
8
|
-
|
|
9
|
+
vi.resetModules()
|
|
9
10
|
focusManager = new FocusManager()
|
|
10
11
|
})
|
|
11
12
|
|
|
12
13
|
it('should call previous remove handler when replacing an event listener', () => {
|
|
13
|
-
const remove1Spy =
|
|
14
|
-
const remove2Spy =
|
|
14
|
+
const remove1Spy = vi.fn()
|
|
15
|
+
const remove2Spy = vi.fn()
|
|
15
16
|
|
|
16
17
|
focusManager.setEventListener(() => remove1Spy)
|
|
17
18
|
focusManager.setEventListener(() => remove2Spy)
|
|
@@ -39,7 +40,7 @@ describe('focusManager', () => {
|
|
|
39
40
|
})
|
|
40
41
|
|
|
41
42
|
it('should not notify listeners on focus if already focused', async () => {
|
|
42
|
-
const subscriptionSpy =
|
|
43
|
+
const subscriptionSpy = vi.fn()
|
|
43
44
|
const unsubscribe = focusManager.subscribe(subscriptionSpy)
|
|
44
45
|
|
|
45
46
|
focusManager.setFocused(true)
|
|
@@ -66,7 +67,7 @@ describe('focusManager', () => {
|
|
|
66
67
|
test('cleanup (removeEventListener) should not be called if window is not defined', async () => {
|
|
67
68
|
const restoreIsServer = setIsServer(true)
|
|
68
69
|
|
|
69
|
-
const removeEventListenerSpy =
|
|
70
|
+
const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
|
|
70
71
|
|
|
71
72
|
const unsubscribe = focusManager.subscribe(() => undefined)
|
|
72
73
|
|
|
@@ -83,7 +84,7 @@ describe('focusManager', () => {
|
|
|
83
84
|
// @ts-expect-error
|
|
84
85
|
globalThis.window.addEventListener = undefined
|
|
85
86
|
|
|
86
|
-
const removeEventListenerSpy =
|
|
87
|
+
const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
|
|
87
88
|
|
|
88
89
|
const unsubscribe = focusManager.subscribe(() => undefined)
|
|
89
90
|
|
|
@@ -95,8 +96,8 @@ describe('focusManager', () => {
|
|
|
95
96
|
})
|
|
96
97
|
|
|
97
98
|
it('should replace default window listener when a new event listener is set', async () => {
|
|
98
|
-
const unsubscribeSpy =
|
|
99
|
-
const handlerSpy =
|
|
99
|
+
const unsubscribeSpy = vi.fn().mockImplementation(() => undefined)
|
|
100
|
+
const handlerSpy = vi.fn().mockImplementation(() => unsubscribeSpy)
|
|
100
101
|
|
|
101
102
|
focusManager.setEventListener(() => handlerSpy())
|
|
102
103
|
|
|
@@ -115,12 +116,9 @@ describe('focusManager', () => {
|
|
|
115
116
|
})
|
|
116
117
|
|
|
117
118
|
test('should call removeEventListener when last listener unsubscribes', () => {
|
|
118
|
-
const addEventListenerSpy =
|
|
119
|
-
globalThis.window,
|
|
120
|
-
'addEventListener',
|
|
121
|
-
)
|
|
119
|
+
const addEventListenerSpy = vi.spyOn(globalThis.window, 'addEventListener')
|
|
122
120
|
|
|
123
|
-
const removeEventListenerSpy =
|
|
121
|
+
const removeEventListenerSpy = vi.spyOn(
|
|
124
122
|
globalThis.window,
|
|
125
123
|
'removeEventListener',
|
|
126
124
|
)
|
|
@@ -136,7 +134,7 @@ describe('focusManager', () => {
|
|
|
136
134
|
})
|
|
137
135
|
|
|
138
136
|
test('should keep setup function even if last listener unsubscribes', () => {
|
|
139
|
-
const setupSpy =
|
|
137
|
+
const setupSpy = vi.fn().mockImplementation(() => () => undefined)
|
|
140
138
|
|
|
141
139
|
focusManager.setEventListener(setupSpy)
|
|
142
140
|
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from './utils'
|
|
7
7
|
import { QueryCache } from '../queryCache'
|
|
8
8
|
import { dehydrate, hydrate } from '../hydration'
|
|
9
|
+
import { vi } from 'vitest'
|
|
9
10
|
|
|
10
11
|
async function fetchData<TData>(value: TData, ms?: number): Promise<TData> {
|
|
11
12
|
await sleep(ms || 0)
|
|
@@ -67,7 +68,7 @@ describe('dehydration and rehydration', () => {
|
|
|
67
68
|
key: [{ nestedKey: 1 }],
|
|
68
69
|
})
|
|
69
70
|
|
|
70
|
-
const fetchDataAfterHydration =
|
|
71
|
+
const fetchDataAfterHydration = vi.fn<unknown[], unknown>()
|
|
71
72
|
await hydrationClient.prefetchQuery({
|
|
72
73
|
queryKey: ['string'],
|
|
73
74
|
queryFn: fetchDataAfterHydration,
|
|
@@ -112,7 +113,9 @@ describe('dehydration and rehydration', () => {
|
|
|
112
113
|
queryFn: () => fetchData('string'),
|
|
113
114
|
})
|
|
114
115
|
|
|
115
|
-
const dehydrated = dehydrate(queryClient, {
|
|
116
|
+
const dehydrated = dehydrate(queryClient, {
|
|
117
|
+
shouldDehydrateQuery: () => false,
|
|
118
|
+
})
|
|
116
119
|
|
|
117
120
|
expect(dehydrated.queries.length).toBe(0)
|
|
118
121
|
|
|
@@ -192,7 +195,7 @@ describe('dehydration and rehydration', () => {
|
|
|
192
195
|
})?.state.data,
|
|
193
196
|
).toBe('string')
|
|
194
197
|
|
|
195
|
-
const fetchDataAfterHydration =
|
|
198
|
+
const fetchDataAfterHydration = vi.fn<unknown[], unknown>()
|
|
196
199
|
await hydrationClient.prefetchQuery({
|
|
197
200
|
queryKey: ['string', { key: ['string'], key2: 0 }],
|
|
198
201
|
queryFn: fetchDataAfterHydration,
|
|
@@ -205,7 +208,7 @@ describe('dehydration and rehydration', () => {
|
|
|
205
208
|
})
|
|
206
209
|
|
|
207
210
|
test('should only hydrate successful queries by default', async () => {
|
|
208
|
-
const consoleMock =
|
|
211
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
209
212
|
consoleMock.mockImplementation(() => undefined)
|
|
210
213
|
|
|
211
214
|
const queryCache = new QueryCache()
|
|
@@ -243,7 +246,7 @@ describe('dehydration and rehydration', () => {
|
|
|
243
246
|
consoleMock.mockRestore()
|
|
244
247
|
})
|
|
245
248
|
|
|
246
|
-
test('should filter queries via
|
|
249
|
+
test('should filter queries via dehydrateQuery', async () => {
|
|
247
250
|
const queryCache = new QueryCache()
|
|
248
251
|
const queryClient = createQueryClient({ queryCache })
|
|
249
252
|
await queryClient.prefetchQuery({
|
|
@@ -341,18 +344,18 @@ describe('dehydration and rehydration', () => {
|
|
|
341
344
|
})
|
|
342
345
|
|
|
343
346
|
test('should be able to dehydrate mutations and continue on hydration', async () => {
|
|
344
|
-
const consoleMock =
|
|
347
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
345
348
|
consoleMock.mockImplementation(() => undefined)
|
|
346
349
|
const onlineMock = mockNavigatorOnLine(false)
|
|
347
350
|
|
|
348
|
-
const serverAddTodo =
|
|
351
|
+
const serverAddTodo = vi
|
|
349
352
|
.fn()
|
|
350
353
|
.mockImplementation(() => Promise.reject(new Error('offline')))
|
|
351
|
-
const serverOnMutate =
|
|
354
|
+
const serverOnMutate = vi.fn().mockImplementation((variables) => {
|
|
352
355
|
const optimisticTodo = { id: 1, text: variables.text }
|
|
353
356
|
return { optimisticTodo }
|
|
354
357
|
})
|
|
355
|
-
const serverOnSuccess =
|
|
358
|
+
const serverOnSuccess = vi.fn()
|
|
356
359
|
|
|
357
360
|
const serverClient = createQueryClient()
|
|
358
361
|
|
|
@@ -386,14 +389,14 @@ describe('dehydration and rehydration', () => {
|
|
|
386
389
|
const parsed = JSON.parse(stringified)
|
|
387
390
|
const client = createQueryClient()
|
|
388
391
|
|
|
389
|
-
const clientAddTodo =
|
|
392
|
+
const clientAddTodo = vi.fn().mockImplementation((variables) => {
|
|
390
393
|
return { id: 2, text: variables.text }
|
|
391
394
|
})
|
|
392
|
-
const clientOnMutate =
|
|
395
|
+
const clientOnMutate = vi.fn().mockImplementation((variables) => {
|
|
393
396
|
const optimisticTodo = { id: 1, text: variables.text }
|
|
394
397
|
return { optimisticTodo }
|
|
395
398
|
})
|
|
396
|
-
const clientOnSuccess =
|
|
399
|
+
const clientOnSuccess = vi.fn()
|
|
397
400
|
|
|
398
401
|
client.setMutationDefaults(['addTodo'], {
|
|
399
402
|
mutationFn: clientAddTodo,
|
|
@@ -422,10 +425,10 @@ describe('dehydration and rehydration', () => {
|
|
|
422
425
|
})
|
|
423
426
|
|
|
424
427
|
test('should not dehydrate mutations if dehydrateMutations is set to false', async () => {
|
|
425
|
-
const consoleMock =
|
|
428
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
426
429
|
consoleMock.mockImplementation(() => undefined)
|
|
427
430
|
|
|
428
|
-
const serverAddTodo =
|
|
431
|
+
const serverAddTodo = vi
|
|
429
432
|
.fn()
|
|
430
433
|
.mockImplementation(() => Promise.reject(new Error('offline')))
|
|
431
434
|
|
|
@@ -445,7 +448,9 @@ describe('dehydration and rehydration', () => {
|
|
|
445
448
|
).catch(() => undefined)
|
|
446
449
|
|
|
447
450
|
await sleep(1)
|
|
448
|
-
const dehydrated = dehydrate(queryClient, {
|
|
451
|
+
const dehydrated = dehydrate(queryClient, {
|
|
452
|
+
shouldDehydrateMutation: () => false,
|
|
453
|
+
})
|
|
449
454
|
|
|
450
455
|
expect(dehydrated.mutations.length).toBe(0)
|
|
451
456
|
|
|
@@ -454,10 +459,10 @@ describe('dehydration and rehydration', () => {
|
|
|
454
459
|
})
|
|
455
460
|
|
|
456
461
|
test('should not dehydrate mutation if mutation state is set to pause', async () => {
|
|
457
|
-
const consoleMock =
|
|
462
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
458
463
|
consoleMock.mockImplementation(() => undefined)
|
|
459
464
|
|
|
460
|
-
const serverAddTodo =
|
|
465
|
+
const serverAddTodo = vi
|
|
461
466
|
.fn()
|
|
462
467
|
.mockImplementation(() => Promise.reject(new Error('offline')))
|
|
463
468
|
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { waitFor } from '@testing-library/react'
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
InfiniteQueryObserverResult,
|
|
5
|
-
} from '@tanstack/query-core'
|
|
6
|
-
import { InfiniteQueryObserver, CancelledError } from '@tanstack/query-core'
|
|
2
|
+
import type { QueryClient, InfiniteQueryObserverResult } from '..'
|
|
3
|
+
import { InfiniteQueryObserver, CancelledError } from '..'
|
|
7
4
|
import { createQueryClient, queryKey, sleep } from './utils'
|
|
5
|
+
import { vi } from 'vitest'
|
|
8
6
|
|
|
9
7
|
describe('InfiniteQueryBehavior', () => {
|
|
10
8
|
let queryClient: QueryClient
|
|
@@ -50,7 +48,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
50
48
|
const key = queryKey()
|
|
51
49
|
let abortSignal: AbortSignal | null = null
|
|
52
50
|
|
|
53
|
-
const queryFnSpy =
|
|
51
|
+
const queryFnSpy = vi.fn().mockImplementation(({ pageParam, signal }) => {
|
|
54
52
|
abortSignal = signal
|
|
55
53
|
return pageParam
|
|
56
54
|
})
|
|
@@ -84,6 +82,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
84
82
|
queryKey: key,
|
|
85
83
|
pageParam: 1,
|
|
86
84
|
meta: undefined,
|
|
85
|
+
direction: 'forward',
|
|
87
86
|
signal: abortSignal,
|
|
88
87
|
})
|
|
89
88
|
|
|
@@ -95,6 +94,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
95
94
|
expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
|
|
96
95
|
queryKey: key,
|
|
97
96
|
pageParam: 2,
|
|
97
|
+
direction: 'forward',
|
|
98
98
|
meta: undefined,
|
|
99
99
|
signal: abortSignal,
|
|
100
100
|
})
|
|
@@ -112,6 +112,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
112
112
|
expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
|
|
113
113
|
queryKey: key,
|
|
114
114
|
pageParam: 0,
|
|
115
|
+
direction: 'backward',
|
|
115
116
|
meta: undefined,
|
|
116
117
|
signal: abortSignal,
|
|
117
118
|
})
|
|
@@ -131,6 +132,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
131
132
|
queryKey: key,
|
|
132
133
|
pageParam: -1,
|
|
133
134
|
meta: undefined,
|
|
135
|
+
direction: 'backward',
|
|
134
136
|
signal: abortSignal,
|
|
135
137
|
})
|
|
136
138
|
|
|
@@ -148,6 +150,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
148
150
|
queryKey: key,
|
|
149
151
|
pageParam: 1,
|
|
150
152
|
meta: undefined,
|
|
153
|
+
direction: 'forward',
|
|
151
154
|
signal: abortSignal,
|
|
152
155
|
})
|
|
153
156
|
|
|
@@ -168,6 +171,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
168
171
|
queryKey: key,
|
|
169
172
|
pageParam: 0,
|
|
170
173
|
meta: undefined,
|
|
174
|
+
direction: 'forward',
|
|
171
175
|
signal: abortSignal,
|
|
172
176
|
})
|
|
173
177
|
|
|
@@ -175,6 +179,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
175
179
|
queryKey: key,
|
|
176
180
|
pageParam: 1,
|
|
177
181
|
meta: undefined,
|
|
182
|
+
direction: 'forward',
|
|
178
183
|
signal: abortSignal,
|
|
179
184
|
})
|
|
180
185
|
|
|
@@ -185,7 +190,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
185
190
|
const key = queryKey()
|
|
186
191
|
let abortSignal: AbortSignal | null = null
|
|
187
192
|
|
|
188
|
-
const queryFnSpy =
|
|
193
|
+
const queryFnSpy = vi.fn().mockImplementation(({ pageParam, signal }) => {
|
|
189
194
|
abortSignal = signal
|
|
190
195
|
sleep(10)
|
|
191
196
|
return pageParam
|
|
@@ -226,6 +231,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
226
231
|
queryKey: key,
|
|
227
232
|
pageParam: 1,
|
|
228
233
|
meta: undefined,
|
|
234
|
+
direction: 'forward',
|
|
229
235
|
signal: abortSignal,
|
|
230
236
|
})
|
|
231
237
|
|
|
@@ -236,7 +242,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
236
242
|
const key = queryKey()
|
|
237
243
|
let abortSignal: AbortSignal | null = null
|
|
238
244
|
|
|
239
|
-
let queryFnSpy =
|
|
245
|
+
let queryFnSpy = vi.fn().mockImplementation(({ pageParam, signal }) => {
|
|
240
246
|
abortSignal = signal
|
|
241
247
|
return pageParam
|
|
242
248
|
})
|
|
@@ -281,10 +287,11 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
281
287
|
queryKey: key,
|
|
282
288
|
pageParam: 2,
|
|
283
289
|
meta: undefined,
|
|
290
|
+
direction: 'forward',
|
|
284
291
|
signal: abortSignal,
|
|
285
292
|
})
|
|
286
293
|
|
|
287
|
-
queryFnSpy =
|
|
294
|
+
queryFnSpy = vi.fn().mockImplementation(({ pageParam = 1, signal }) => {
|
|
288
295
|
abortSignal = signal
|
|
289
296
|
sleep(10)
|
|
290
297
|
return pageParam
|