@tanstack/react-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/lib/HydrationBoundary.d.ts +9 -0
- package/build/lib/{Hydrate.esm.js → HydrationBoundary.esm.js} +14 -17
- package/build/lib/HydrationBoundary.esm.js.map +1 -0
- package/build/lib/{Hydrate.js → HydrationBoundary.js} +17 -24
- package/build/lib/HydrationBoundary.js.map +1 -0
- package/build/lib/{Hydrate.mjs → HydrationBoundary.mjs} +14 -17
- package/build/lib/HydrationBoundary.mjs.map +1 -0
- package/build/lib/QueryClientProvider.d.ts +4 -19
- package/build/lib/QueryClientProvider.esm.js +11 -46
- package/build/lib/QueryClientProvider.esm.js.map +1 -1
- package/build/lib/QueryClientProvider.js +14 -52
- package/build/lib/QueryClientProvider.js.map +1 -1
- package/build/lib/QueryClientProvider.mjs +11 -46
- package/build/lib/QueryClientProvider.mjs.map +1 -1
- package/build/lib/QueryErrorResetBoundary.esm.js +7 -2
- package/build/lib/QueryErrorResetBoundary.esm.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.js +10 -8
- package/build/lib/QueryErrorResetBoundary.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.mjs +7 -2
- package/build/lib/QueryErrorResetBoundary.mjs.map +1 -1
- package/build/lib/__tests__/useQuery.types.test.d.ts +1 -2
- package/build/lib/__tests__/utils.d.ts +7 -10
- package/build/lib/errorBoundaryUtils.d.ts +3 -3
- package/build/lib/errorBoundaryUtils.esm.js +3 -3
- package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
- package/build/lib/errorBoundaryUtils.js +6 -9
- package/build/lib/errorBoundaryUtils.js.map +1 -1
- package/build/lib/errorBoundaryUtils.mjs +3 -3
- package/build/lib/errorBoundaryUtils.mjs.map +1 -1
- package/build/lib/index.d.ts +4 -5
- package/build/lib/index.esm.js +3 -4
- package/build/lib/index.esm.js.map +1 -1
- package/build/lib/index.js +6 -9
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +3 -4
- package/build/lib/index.mjs.map +1 -1
- package/build/lib/isRestoring.esm.js.map +1 -1
- package/build/lib/isRestoring.js +3 -6
- package/build/lib/isRestoring.js.map +1 -1
- package/build/lib/isRestoring.mjs.map +1 -1
- package/build/lib/suspense.esm.js +5 -5
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +5 -7
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/suspense.mjs +5 -5
- package/build/lib/suspense.mjs.map +1 -1
- package/build/lib/types.d.ts +14 -22
- package/build/lib/useBaseQuery.d.ts +2 -2
- package/build/lib/useBaseQuery.esm.js +15 -18
- package/build/lib/useBaseQuery.esm.js.map +1 -1
- package/build/lib/useBaseQuery.js +18 -24
- package/build/lib/useBaseQuery.js.map +1 -1
- package/build/lib/useBaseQuery.mjs +15 -18
- package/build/lib/useBaseQuery.mjs.map +1 -1
- package/build/lib/useInfiniteQuery.d.ts +2 -4
- package/build/lib/useInfiniteQuery.esm.js +6 -4
- package/build/lib/useInfiniteQuery.esm.js.map +1 -1
- package/build/lib/useInfiniteQuery.js +5 -5
- package/build/lib/useInfiniteQuery.js.map +1 -1
- package/build/lib/useInfiniteQuery.mjs +6 -4
- package/build/lib/useInfiniteQuery.mjs.map +1 -1
- package/build/lib/useIsFetching.d.ts +2 -7
- package/build/lib/useIsFetching.esm.js +5 -9
- package/build/lib/useIsFetching.esm.js.map +1 -1
- package/build/lib/useIsFetching.js +7 -14
- package/build/lib/useIsFetching.js.map +1 -1
- package/build/lib/useIsFetching.mjs +5 -9
- package/build/lib/useIsFetching.mjs.map +1 -1
- package/build/lib/useMutation.d.ts +2 -5
- package/build/lib/useMutation.esm.js +12 -14
- package/build/lib/useMutation.esm.js.map +1 -1
- package/build/lib/useMutation.js +14 -19
- package/build/lib/useMutation.js.map +1 -1
- package/build/lib/useMutation.mjs +12 -14
- package/build/lib/useMutation.mjs.map +1 -1
- package/build/lib/useMutationState.d.ts +8 -0
- package/build/lib/useMutationState.esm.js +37 -0
- package/build/lib/useMutationState.esm.js.map +1 -0
- package/build/lib/useMutationState.js +59 -0
- package/build/lib/useMutationState.js.map +1 -0
- package/build/lib/useMutationState.mjs +37 -0
- package/build/lib/useMutationState.mjs.map +1 -0
- package/build/lib/useQueries.d.ts +9 -7
- package/build/lib/useQueries.esm.js +16 -27
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +19 -33
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQueries.mjs +16 -27
- package/build/lib/useQueries.mjs.map +1 -1
- package/build/lib/useQuery.d.ts +9 -21
- package/build/lib/useQuery.esm.js +5 -4
- package/build/lib/useQuery.esm.js.map +1 -1
- package/build/lib/useQuery.js +4 -5
- package/build/lib/useQuery.js.map +1 -1
- package/build/lib/useQuery.mjs +5 -4
- package/build/lib/useQuery.mjs.map +1 -1
- package/build/lib/utils.d.ts +1 -1
- package/build/lib/utils.esm.js +5 -6
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +5 -8
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +5 -6
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +1003 -1827
- 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 +6 -18
- package/src/{Hydrate.tsx → HydrationBoundary.tsx} +17 -18
- package/src/QueryClientProvider.tsx +11 -65
- package/src/__tests__/{Hydrate.test.tsx → HydrationBoundary.test.tsx} +89 -77
- package/src/__tests__/QueryClientProvider.test.tsx +33 -147
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +118 -85
- package/src/__tests__/ssr-hydration.test.tsx +27 -33
- package/src/__tests__/ssr.test.tsx +23 -9
- package/src/__tests__/suspense.test.tsx +194 -171
- package/src/__tests__/useInfiniteQuery.test.tsx +249 -494
- package/src/__tests__/useInfiniteQuery.type.test.tsx +131 -0
- package/src/__tests__/useIsFetching.test.tsx +65 -108
- package/src/__tests__/useMutation.test.tsx +200 -268
- package/src/__tests__/useMutationState.test.tsx +284 -0
- package/src/__tests__/useQueries.test.tsx +43 -347
- package/src/__tests__/useQuery.test.tsx +890 -686
- package/src/__tests__/useQuery.types.test.tsx +23 -24
- package/src/__tests__/utils.tsx +14 -23
- package/src/errorBoundaryUtils.ts +5 -10
- package/src/index.ts +4 -7
- package/src/types.ts +33 -38
- package/src/useBaseQuery.ts +7 -7
- package/src/useInfiniteQuery.ts +13 -77
- package/src/useIsFetching.ts +8 -20
- package/src/useMutation.ts +8 -66
- package/src/useMutationState.ts +81 -0
- package/src/useQueries.ts +29 -21
- package/src/useQuery.ts +27 -104
- package/src/utils.ts +5 -5
- package/build/lib/Hydrate.d.ts +0 -10
- package/build/lib/Hydrate.esm.js.map +0 -1
- package/build/lib/Hydrate.js.map +0 -1
- package/build/lib/Hydrate.mjs.map +0 -1
- package/build/lib/reactBatchedUpdates.d.ts +0 -2
- package/build/lib/reactBatchedUpdates.esm.js +0 -6
- package/build/lib/reactBatchedUpdates.esm.js.map +0 -1
- package/build/lib/reactBatchedUpdates.js +0 -30
- package/build/lib/reactBatchedUpdates.js.map +0 -1
- package/build/lib/reactBatchedUpdates.mjs +0 -6
- package/build/lib/reactBatchedUpdates.mjs.map +0 -1
- package/build/lib/reactBatchedUpdates.native.d.ts +0 -2
- package/build/lib/reactBatchedUpdates.native.esm.js +0 -2
- package/build/lib/reactBatchedUpdates.native.esm.js.map +0 -1
- package/build/lib/reactBatchedUpdates.native.js +0 -13
- package/build/lib/reactBatchedUpdates.native.js.map +0 -1
- package/build/lib/reactBatchedUpdates.native.mjs +0 -2
- package/build/lib/reactBatchedUpdates.native.mjs.map +0 -1
- package/build/lib/setBatchUpdatesFn.esm.js +0 -5
- package/build/lib/setBatchUpdatesFn.esm.js.map +0 -1
- package/build/lib/setBatchUpdatesFn.js +0 -7
- package/build/lib/setBatchUpdatesFn.js.map +0 -1
- package/build/lib/setBatchUpdatesFn.mjs +0 -5
- package/build/lib/setBatchUpdatesFn.mjs.map +0 -1
- package/build/lib/useIsMutating.d.ts +0 -7
- package/build/lib/useIsMutating.esm.js +0 -16
- package/build/lib/useIsMutating.esm.js.map +0 -1
- package/build/lib/useIsMutating.js +0 -40
- package/build/lib/useIsMutating.js.map +0 -1
- package/build/lib/useIsMutating.mjs +0 -16
- package/build/lib/useIsMutating.mjs.map +0 -1
- package/build/lib/useSyncExternalStore.d.ts +0 -2
- package/build/lib/useSyncExternalStore.esm.js +0 -7
- package/build/lib/useSyncExternalStore.esm.js.map +0 -1
- package/build/lib/useSyncExternalStore.js +0 -11
- package/build/lib/useSyncExternalStore.js.map +0 -1
- package/build/lib/useSyncExternalStore.mjs +0 -7
- package/build/lib/useSyncExternalStore.mjs.map +0 -1
- package/build/lib/useSyncExternalStore.native.d.ts +0 -2
- package/build/lib/useSyncExternalStore.native.esm.js +0 -2
- package/build/lib/useSyncExternalStore.native.esm.js.map +0 -1
- package/build/lib/useSyncExternalStore.native.js +0 -13
- package/build/lib/useSyncExternalStore.native.js.map +0 -1
- package/build/lib/useSyncExternalStore.native.mjs +0 -2
- package/build/lib/useSyncExternalStore.native.mjs.map +0 -1
- package/codemods/v4/key-transformation.js +0 -138
- package/codemods/v4/replace-import-specifier.js +0 -25
- package/codemods/v4/utils/index.js +0 -166
- package/codemods/v4/utils/replacers/key-replacer.js +0 -160
- package/codemods/v4/utils/transformers/query-cache-transformer.js +0 -115
- package/codemods/v4/utils/transformers/query-client-transformer.js +0 -49
- package/codemods/v4/utils/transformers/use-query-like-transformer.js +0 -32
- package/codemods/v4/utils/unprocessable-key-error.js +0 -8
- package/src/__tests__/useIsMutating.test.tsx +0 -259
- package/src/reactBatchedUpdates.native.ts +0 -4
- package/src/reactBatchedUpdates.ts +0 -2
- package/src/setBatchUpdatesFn.ts +0 -4
- package/src/useIsMutating.ts +0 -39
- package/src/useSyncExternalStore.native.ts +0 -5
- package/src/useSyncExternalStore.ts +0 -4
- /package/build/lib/__tests__/{Hydrate.test.d.ts → HydrationBoundary.test.d.ts} +0 -0
- /package/build/lib/__tests__/{useIsMutating.test.d.ts → useInfiniteQuery.type.test.d.ts} +0 -0
- /package/build/lib/{setBatchUpdatesFn.d.ts → __tests__/useMutationState.test.d.ts} +0 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { InfiniteData } from '@tanstack/query-core'
|
|
2
|
+
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
+
import { useInfiniteQuery } from '../useInfiniteQuery'
|
|
4
|
+
import { useQuery } from '../useQuery'
|
|
5
|
+
import type { Expect, Equal } from './utils'
|
|
6
|
+
import { doNotExecute } from './utils'
|
|
7
|
+
|
|
8
|
+
describe('pageParam', () => {
|
|
9
|
+
it('defaultPageParam should define type of param passed to queryFunctionContext', () => {
|
|
10
|
+
doNotExecute(() => {
|
|
11
|
+
useInfiniteQuery({
|
|
12
|
+
queryKey: ['key'],
|
|
13
|
+
queryFn: ({ pageParam }) => {
|
|
14
|
+
const result: Expect<Equal<number, typeof pageParam>> = true
|
|
15
|
+
return result
|
|
16
|
+
},
|
|
17
|
+
defaultPageParam: 1,
|
|
18
|
+
getNextPageParam: () => undefined,
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('there should be no pageParam passed to queryFn of useQuery', () => {
|
|
24
|
+
doNotExecute(() => {
|
|
25
|
+
useQuery({
|
|
26
|
+
queryKey: ['key'],
|
|
27
|
+
// @ts-expect-error there should be no pageParam passed to queryFn of useQuery
|
|
28
|
+
queryFn: ({ pageParam }) => {
|
|
29
|
+
return String(pageParam)
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('defaultPageParam should define type of param passed to queryFunctionContext for fetchInfiniteQuery', () => {
|
|
36
|
+
doNotExecute(() => {
|
|
37
|
+
const queryClient = new QueryClient()
|
|
38
|
+
queryClient.fetchInfiniteQuery({
|
|
39
|
+
queryKey: ['key'],
|
|
40
|
+
queryFn: ({ pageParam }) => {
|
|
41
|
+
const result: Expect<Equal<number, typeof pageParam>> = true
|
|
42
|
+
return result
|
|
43
|
+
},
|
|
44
|
+
defaultPageParam: 1,
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('defaultPageParam should define type of param passed to queryFunctionContext for prefetchInfiniteQuery', () => {
|
|
50
|
+
doNotExecute(() => {
|
|
51
|
+
const queryClient = new QueryClient()
|
|
52
|
+
queryClient.prefetchInfiniteQuery({
|
|
53
|
+
queryKey: ['key'],
|
|
54
|
+
queryFn: ({ pageParam }) => {
|
|
55
|
+
const result: Expect<Equal<number, typeof pageParam>> = true
|
|
56
|
+
return result
|
|
57
|
+
},
|
|
58
|
+
defaultPageParam: 1,
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
describe('select', () => {
|
|
65
|
+
it('should still return paginated data if no select result', () => {
|
|
66
|
+
doNotExecute(() => {
|
|
67
|
+
const infiniteQuery = useInfiniteQuery({
|
|
68
|
+
queryKey: ['key'],
|
|
69
|
+
queryFn: ({ pageParam }) => {
|
|
70
|
+
return pageParam * 5
|
|
71
|
+
},
|
|
72
|
+
defaultPageParam: 1,
|
|
73
|
+
getNextPageParam: () => undefined,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const result: Expect<
|
|
77
|
+
Equal<InfiniteData<number> | undefined, (typeof infiniteQuery)['data']>
|
|
78
|
+
> = true
|
|
79
|
+
return result
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('should be able to transform data to arbitrary result', () => {
|
|
84
|
+
doNotExecute(() => {
|
|
85
|
+
const infiniteQuery = useInfiniteQuery({
|
|
86
|
+
queryKey: ['key'],
|
|
87
|
+
queryFn: ({ pageParam }) => {
|
|
88
|
+
return pageParam * 5
|
|
89
|
+
},
|
|
90
|
+
defaultPageParam: 1,
|
|
91
|
+
getNextPageParam: () => undefined,
|
|
92
|
+
select: (data) => {
|
|
93
|
+
const result: Expect<Equal<InfiniteData<number>, typeof data>> = true
|
|
94
|
+
return result
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
const result: Expect<
|
|
99
|
+
Equal<true | undefined, (typeof infiniteQuery)['data']>
|
|
100
|
+
> = true
|
|
101
|
+
return result
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
it('should pass transformed data to onSuccess', () => {
|
|
105
|
+
doNotExecute(() => {
|
|
106
|
+
const infiniteQuery = useInfiniteQuery({
|
|
107
|
+
queryKey: ['key'],
|
|
108
|
+
queryFn: ({ pageParam }) => {
|
|
109
|
+
return pageParam * 5
|
|
110
|
+
},
|
|
111
|
+
defaultPageParam: 1,
|
|
112
|
+
getNextPageParam: () => undefined,
|
|
113
|
+
select: (data) => {
|
|
114
|
+
return {
|
|
115
|
+
...data,
|
|
116
|
+
pages: data.pages.map((page) => page.toString()),
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
onSuccess: (data) => {
|
|
120
|
+
const result: Expect<Equal<InfiniteData<string>, typeof data>> = true
|
|
121
|
+
doNotExecute(() => result)
|
|
122
|
+
},
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
const result: Expect<
|
|
126
|
+
Equal<InfiniteData<string> | undefined, (typeof infiniteQuery)['data']>
|
|
127
|
+
> = true
|
|
128
|
+
return result
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
})
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { fireEvent, waitFor } from '@testing-library/react'
|
|
1
|
+
import { fireEvent, render, waitFor } from '@testing-library/react'
|
|
2
2
|
import * as React from 'react'
|
|
3
|
-
import { ErrorBoundary } from 'react-error-boundary'
|
|
4
3
|
|
|
5
4
|
import {
|
|
6
5
|
createQueryClient,
|
|
@@ -9,7 +8,6 @@ import {
|
|
|
9
8
|
setActTimeout,
|
|
10
9
|
sleep,
|
|
11
10
|
} from './utils'
|
|
12
|
-
import type { QueryClient } from '..'
|
|
13
11
|
import { QueryCache, useIsFetching, useQuery } from '..'
|
|
14
12
|
|
|
15
13
|
describe('useIsFetching', () => {
|
|
@@ -27,16 +25,14 @@ describe('useIsFetching', () => {
|
|
|
27
25
|
function Query() {
|
|
28
26
|
const [ready, setReady] = React.useState(false)
|
|
29
27
|
|
|
30
|
-
useQuery(
|
|
31
|
-
key,
|
|
32
|
-
async () => {
|
|
28
|
+
useQuery({
|
|
29
|
+
queryKey: key,
|
|
30
|
+
queryFn: async () => {
|
|
33
31
|
await sleep(50)
|
|
34
32
|
return 'test'
|
|
35
33
|
},
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
)
|
|
34
|
+
enabled: ready,
|
|
35
|
+
})
|
|
40
36
|
|
|
41
37
|
return <button onClick={() => setReady(true)}>setReady</button>
|
|
42
38
|
}
|
|
@@ -74,17 +70,23 @@ describe('useIsFetching', () => {
|
|
|
74
70
|
}
|
|
75
71
|
|
|
76
72
|
function FirstQuery() {
|
|
77
|
-
useQuery(
|
|
78
|
-
|
|
79
|
-
|
|
73
|
+
useQuery({
|
|
74
|
+
queryKey: key1,
|
|
75
|
+
queryFn: async () => {
|
|
76
|
+
await sleep(100)
|
|
77
|
+
return 'data'
|
|
78
|
+
},
|
|
80
79
|
})
|
|
81
80
|
return null
|
|
82
81
|
}
|
|
83
82
|
|
|
84
83
|
function SecondQuery() {
|
|
85
|
-
useQuery(
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
useQuery({
|
|
85
|
+
queryKey: key2,
|
|
86
|
+
queryFn: async () => {
|
|
87
|
+
await sleep(100)
|
|
88
|
+
return 'data'
|
|
89
|
+
},
|
|
88
90
|
})
|
|
89
91
|
return null
|
|
90
92
|
}
|
|
@@ -119,24 +121,30 @@ describe('useIsFetching', () => {
|
|
|
119
121
|
const isFetchings: number[] = []
|
|
120
122
|
|
|
121
123
|
function One() {
|
|
122
|
-
useQuery(
|
|
123
|
-
|
|
124
|
-
|
|
124
|
+
useQuery({
|
|
125
|
+
queryKey: key1,
|
|
126
|
+
queryFn: async () => {
|
|
127
|
+
await sleep(10)
|
|
128
|
+
return 'test'
|
|
129
|
+
},
|
|
125
130
|
})
|
|
126
131
|
return null
|
|
127
132
|
}
|
|
128
133
|
|
|
129
134
|
function Two() {
|
|
130
|
-
useQuery(
|
|
131
|
-
|
|
132
|
-
|
|
135
|
+
useQuery({
|
|
136
|
+
queryKey: key2,
|
|
137
|
+
queryFn: async () => {
|
|
138
|
+
await sleep(20)
|
|
139
|
+
return 'test'
|
|
140
|
+
},
|
|
133
141
|
})
|
|
134
142
|
return null
|
|
135
143
|
}
|
|
136
144
|
|
|
137
145
|
function Page() {
|
|
138
146
|
const [started, setStarted] = React.useState(false)
|
|
139
|
-
const isFetching = useIsFetching(key1)
|
|
147
|
+
const isFetching = useIsFetching({ queryKey: key1 })
|
|
140
148
|
|
|
141
149
|
isFetchings.push(isFetching)
|
|
142
150
|
|
|
@@ -164,101 +172,51 @@ describe('useIsFetching', () => {
|
|
|
164
172
|
expect(isFetchings).toEqual(expect.not.arrayContaining([2]))
|
|
165
173
|
})
|
|
166
174
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const queryCache = new QueryCache()
|
|
172
|
-
const queryClient = createQueryClient({ queryCache })
|
|
173
|
-
const key = queryKey()
|
|
174
|
-
|
|
175
|
-
function Page() {
|
|
176
|
-
const [ready, setReady] = React.useState(false)
|
|
177
|
-
|
|
178
|
-
const isFetching = useIsFetching(undefined, { context: context })
|
|
179
|
-
|
|
180
|
-
useQuery(
|
|
181
|
-
key,
|
|
182
|
-
async () => {
|
|
183
|
-
await sleep(50)
|
|
184
|
-
return 'test'
|
|
185
|
-
},
|
|
186
|
-
{
|
|
187
|
-
enabled: ready,
|
|
188
|
-
context,
|
|
189
|
-
},
|
|
190
|
-
)
|
|
191
|
-
|
|
192
|
-
return (
|
|
193
|
-
<div>
|
|
194
|
-
<div>isFetching: {isFetching}</div>
|
|
195
|
-
<button onClick={() => setReady(true)}>setReady</button>
|
|
196
|
-
</div>
|
|
197
|
-
)
|
|
198
|
-
}
|
|
175
|
+
it('should show the correct fetching state when mounted after a query', async () => {
|
|
176
|
+
const queryClient = createQueryClient()
|
|
177
|
+
const key = queryKey()
|
|
199
178
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
{
|
|
204
|
-
|
|
179
|
+
function Page() {
|
|
180
|
+
useQuery({
|
|
181
|
+
queryKey: key,
|
|
182
|
+
queryFn: async () => {
|
|
183
|
+
await sleep(10)
|
|
184
|
+
return 'test'
|
|
205
185
|
},
|
|
206
|
-
)
|
|
207
|
-
|
|
208
|
-
await findByText('isFetching: 0')
|
|
209
|
-
fireEvent.click(getByRole('button', { name: /setReady/i }))
|
|
210
|
-
await findByText('isFetching: 1')
|
|
211
|
-
await findByText('isFetching: 0')
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
it('should throw if the context is not passed to useIsFetching', async () => {
|
|
215
|
-
const context = React.createContext<QueryClient | undefined>(undefined)
|
|
216
|
-
|
|
217
|
-
const queryCache = new QueryCache()
|
|
218
|
-
const queryClient = createQueryClient({ queryCache })
|
|
219
|
-
const key = queryKey()
|
|
220
|
-
|
|
221
|
-
function Page() {
|
|
222
|
-
const isFetching = useIsFetching()
|
|
223
|
-
|
|
224
|
-
useQuery(key, async () => 'test', {
|
|
225
|
-
enabled: true,
|
|
226
|
-
context,
|
|
227
|
-
useErrorBoundary: true,
|
|
228
|
-
})
|
|
186
|
+
})
|
|
229
187
|
|
|
230
|
-
|
|
231
|
-
<div>
|
|
232
|
-
<div>isFetching: {isFetching}</div>
|
|
233
|
-
</div>
|
|
234
|
-
)
|
|
235
|
-
}
|
|
188
|
+
const isFetching = useIsFetching()
|
|
236
189
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
</ErrorBoundary>,
|
|
242
|
-
{
|
|
243
|
-
context,
|
|
244
|
-
},
|
|
190
|
+
return (
|
|
191
|
+
<div>
|
|
192
|
+
<div>isFetching: {isFetching}</div>
|
|
193
|
+
</div>
|
|
245
194
|
)
|
|
195
|
+
}
|
|
246
196
|
|
|
247
|
-
|
|
248
|
-
|
|
197
|
+
const rendered = renderWithClient(queryClient, <Page />)
|
|
198
|
+
|
|
199
|
+
await rendered.findByText('isFetching: 1')
|
|
200
|
+
await rendered.findByText('isFetching: 0')
|
|
249
201
|
})
|
|
250
202
|
|
|
251
|
-
it('should
|
|
203
|
+
it('should use provided custom queryClient', async () => {
|
|
252
204
|
const queryClient = createQueryClient()
|
|
253
205
|
const key = queryKey()
|
|
254
206
|
|
|
255
207
|
function Page() {
|
|
256
|
-
useQuery(
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
208
|
+
useQuery(
|
|
209
|
+
{
|
|
210
|
+
queryKey: key,
|
|
211
|
+
queryFn: async () => {
|
|
212
|
+
await sleep(10)
|
|
213
|
+
return 'test'
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
queryClient,
|
|
217
|
+
)
|
|
260
218
|
|
|
261
|
-
const isFetching = useIsFetching()
|
|
219
|
+
const isFetching = useIsFetching({}, queryClient)
|
|
262
220
|
|
|
263
221
|
return (
|
|
264
222
|
<div>
|
|
@@ -267,9 +225,8 @@ describe('useIsFetching', () => {
|
|
|
267
225
|
)
|
|
268
226
|
}
|
|
269
227
|
|
|
270
|
-
const rendered =
|
|
228
|
+
const rendered = render(<Page></Page>)
|
|
271
229
|
|
|
272
|
-
await rendered.
|
|
273
|
-
await rendered.findByText('isFetching: 0')
|
|
230
|
+
await waitFor(() => rendered.getByText('isFetching: 1'))
|
|
274
231
|
})
|
|
275
232
|
})
|