@tanstack/react-query 5.0.0-alpha.7 → 5.0.0-alpha.70
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/codemods/__testfixtures__/default-import.input.tsx +94 -0
- package/build/codemods/__testfixtures__/default-import.output.tsx +96 -0
- package/build/codemods/__testfixtures__/named-import.input.tsx +96 -0
- package/build/codemods/__testfixtures__/named-import.output.tsx +98 -0
- package/build/codemods/__testfixtures__/namespaced-import.input.tsx +86 -0
- package/build/codemods/__testfixtures__/namespaced-import.output.tsx +88 -0
- package/build/codemods/__testfixtures__/parameter-is-identifier.input.tsx +49 -0
- package/build/codemods/__testfixtures__/parameter-is-identifier.output.tsx +49 -0
- package/build/codemods/__testfixtures__/parameter-is-object-expression.input.tsx +128 -0
- package/build/codemods/__testfixtures__/parameter-is-object-expression.output.tsx +175 -0
- package/build/codemods/__testfixtures__/replace-import-specifier.input.tsx +10 -0
- package/build/codemods/__testfixtures__/replace-import-specifier.output.tsx +10 -0
- package/build/codemods/__testfixtures__/type-arguments.input.tsx +25 -0
- package/build/codemods/__testfixtures__/type-arguments.output.tsx +31 -0
- package/build/codemods/__tests__/key-transformation.test.js +32 -0
- package/build/codemods/__tests__/replace-import-specifier.test.js +12 -0
- package/build/codemods/remove-overloads/__testfixtures__/default-import.input.tsx +199 -0
- package/build/codemods/remove-overloads/__testfixtures__/default-import.output.tsx +484 -0
- package/build/codemods/remove-overloads/__tests__/remove-overloads.test.js +6 -0
- package/build/codemods/remove-overloads/remove-overloads.js +59 -0
- package/build/codemods/remove-overloads/transformers/filter-aware-usage-transformer.js +153 -0
- package/build/codemods/remove-overloads/transformers/query-fn-aware-usage-transformer.js +188 -0
- package/build/codemods/remove-overloads/utils/index.js +124 -0
- package/build/codemods/remove-overloads/utils/unknown-usage-error.js +27 -0
- package/build/codemods/src/utils/index.js +205 -0
- package/build/codemods/src/v4/key-transformation.js +138 -0
- package/build/codemods/src/v4/replace-import-specifier.js +25 -0
- package/build/codemods/transformers/query-cache-transformer.js +116 -0
- package/build/codemods/transformers/query-client-transformer.js +48 -0
- package/build/codemods/transformers/use-query-like-transformer.js +32 -0
- package/build/codemods/utils/replacers/key-replacer.js +164 -0
- package/build/lib/HydrationBoundary.cjs +50 -0
- package/build/lib/{HydrationBoundary.mjs.map → HydrationBoundary.cjs.map} +1 -1
- package/build/lib/HydrationBoundary.d.ts +1 -0
- package/build/lib/HydrationBoundary.d.ts.map +1 -0
- package/build/lib/HydrationBoundary.js +9 -29
- package/build/lib/HydrationBoundary.js.map +1 -1
- package/build/lib/HydrationBoundary.legacy.cjs +50 -0
- package/build/lib/HydrationBoundary.legacy.cjs.map +1 -0
- package/build/lib/{HydrationBoundary.esm.js → HydrationBoundary.legacy.js} +3 -2
- package/build/lib/HydrationBoundary.legacy.js.map +1 -0
- package/build/lib/QueryClientProvider.cjs +54 -0
- package/build/lib/QueryClientProvider.cjs.map +1 -0
- package/build/lib/QueryClientProvider.d.ts +3 -1
- package/build/lib/QueryClientProvider.d.ts.map +1 -0
- package/build/lib/QueryClientProvider.js +7 -29
- package/build/lib/QueryClientProvider.js.map +1 -1
- package/build/lib/QueryClientProvider.legacy.cjs +54 -0
- package/build/lib/QueryClientProvider.legacy.cjs.map +1 -0
- package/build/lib/{QueryClientProvider.esm.js → QueryClientProvider.legacy.js} +2 -1
- package/build/lib/QueryClientProvider.legacy.js.map +1 -0
- package/build/lib/QueryErrorResetBoundary.cjs +60 -0
- package/build/lib/{QueryErrorResetBoundary.mjs.map → QueryErrorResetBoundary.cjs.map} +1 -1
- package/build/lib/QueryErrorResetBoundary.d.ts +1 -0
- package/build/lib/QueryErrorResetBoundary.d.ts.map +1 -0
- package/build/lib/QueryErrorResetBoundary.js +7 -28
- package/build/lib/QueryErrorResetBoundary.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.legacy.cjs +60 -0
- package/build/lib/QueryErrorResetBoundary.legacy.cjs.map +1 -0
- package/build/lib/{QueryErrorResetBoundary.esm.js → QueryErrorResetBoundary.legacy.js} +2 -1
- package/build/lib/QueryErrorResetBoundary.legacy.js.map +1 -0
- package/build/lib/__tests__/HydrationBoundary.test.d.ts +1 -0
- package/build/lib/__tests__/HydrationBoundary.test.d.ts.map +1 -0
- package/build/lib/__tests__/QueryClientProvider.test.d.ts +1 -0
- package/build/lib/__tests__/QueryClientProvider.test.d.ts.map +1 -0
- package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts +1 -0
- package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts.map +1 -0
- package/build/lib/__tests__/ssr-hydration.test.d.ts +1 -0
- package/build/lib/__tests__/ssr-hydration.test.d.ts.map +1 -0
- package/build/lib/__tests__/ssr.test.d.ts +1 -0
- package/build/lib/__tests__/ssr.test.d.ts.map +1 -0
- package/build/lib/__tests__/suspense.test.d.ts +1 -0
- package/build/lib/__tests__/suspense.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts.map +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutationState.test.d.ts +1 -0
- package/build/lib/__tests__/useMutationState.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts.map +1 -0
- package/build/lib/__tests__/utils.d.ts +3 -2
- package/build/lib/__tests__/utils.d.ts.map +1 -0
- package/build/lib/errorBoundaryUtils.cjs +51 -0
- package/build/lib/errorBoundaryUtils.cjs.map +1 -0
- package/build/lib/errorBoundaryUtils.d.ts +4 -3
- package/build/lib/errorBoundaryUtils.d.ts.map +1 -0
- package/build/lib/errorBoundaryUtils.js +8 -30
- package/build/lib/errorBoundaryUtils.js.map +1 -1
- package/build/lib/errorBoundaryUtils.legacy.cjs +51 -0
- package/build/lib/errorBoundaryUtils.legacy.cjs.map +1 -0
- package/build/lib/{errorBoundaryUtils.esm.js → errorBoundaryUtils.legacy.js} +6 -5
- package/build/lib/errorBoundaryUtils.legacy.js.map +1 -0
- package/build/lib/index.cjs +40 -0
- package/build/lib/index.cjs.map +1 -0
- package/build/lib/index.d.ts +2 -0
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.js +12 -37
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.legacy.cjs +40 -0
- package/build/lib/index.legacy.cjs.map +1 -0
- package/build/lib/index.legacy.js +13 -0
- package/build/lib/index.legacy.js.map +1 -0
- package/build/lib/isRestoring.cjs +31 -0
- package/build/lib/isRestoring.cjs.map +1 -0
- package/build/lib/isRestoring.d.ts +1 -0
- package/build/lib/isRestoring.d.ts.map +1 -0
- package/build/lib/isRestoring.js +5 -26
- package/build/lib/isRestoring.js.map +1 -1
- package/build/lib/isRestoring.legacy.cjs +31 -0
- package/build/lib/isRestoring.legacy.cjs.map +1 -0
- package/build/lib/{isRestoring.esm.js → isRestoring.legacy.js} +2 -1
- package/build/lib/isRestoring.legacy.js.map +1 -0
- package/build/lib/queryOptions.cjs +8 -0
- package/build/lib/queryOptions.cjs.map +1 -0
- package/build/lib/queryOptions.d.ts +11 -0
- package/build/lib/queryOptions.d.ts.map +1 -0
- package/build/lib/queryOptions.js +6 -0
- package/build/lib/queryOptions.js.map +1 -0
- package/build/lib/queryOptions.legacy.cjs +8 -0
- package/build/lib/queryOptions.legacy.cjs.map +1 -0
- package/build/lib/queryOptions.legacy.js +6 -0
- package/build/lib/queryOptions.legacy.js.map +1 -0
- package/build/lib/suspense.cjs +22 -0
- package/build/lib/suspense.cjs.map +1 -0
- package/build/lib/suspense.d.ts +3 -5
- package/build/lib/suspense.d.ts.map +1 -0
- package/build/lib/suspense.js +3 -15
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/{suspense.esm.js → suspense.legacy.cjs} +8 -10
- package/build/lib/suspense.legacy.cjs.map +1 -0
- package/build/lib/{suspense.mjs → suspense.legacy.js} +3 -10
- package/build/lib/suspense.legacy.js.map +1 -0
- package/build/lib/types.d.ts +11 -10
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/useBaseQuery.cjs +80 -0
- package/build/lib/useBaseQuery.cjs.map +1 -0
- package/build/lib/useBaseQuery.d.ts +2 -0
- package/build/lib/useBaseQuery.d.ts.map +1 -0
- package/build/lib/useBaseQuery.js +30 -54
- package/build/lib/useBaseQuery.js.map +1 -1
- package/build/lib/useBaseQuery.legacy.cjs +80 -0
- package/build/lib/useBaseQuery.legacy.cjs.map +1 -0
- package/build/lib/{useBaseQuery.mjs → useBaseQuery.legacy.js} +16 -19
- package/build/lib/useBaseQuery.legacy.js.map +1 -0
- package/build/lib/useInfiniteQuery.cjs +15 -0
- package/build/lib/useInfiniteQuery.cjs.map +1 -0
- package/build/lib/useInfiniteQuery.d.ts +2 -0
- package/build/lib/useInfiniteQuery.d.ts.map +1 -0
- package/build/lib/useInfiniteQuery.js +6 -7
- package/build/lib/useInfiniteQuery.js.map +1 -1
- package/build/lib/useInfiniteQuery.legacy.cjs +15 -0
- package/build/lib/useInfiniteQuery.legacy.cjs.map +1 -0
- package/build/lib/{useInfiniteQuery.mjs → useInfiniteQuery.legacy.js} +3 -2
- package/build/lib/useInfiniteQuery.legacy.js.map +1 -0
- package/build/lib/useIsFetching.cjs +34 -0
- package/build/lib/useIsFetching.cjs.map +1 -0
- package/build/lib/useIsFetching.d.ts +2 -0
- package/build/lib/useIsFetching.d.ts.map +1 -0
- package/build/lib/useIsFetching.js +7 -27
- package/build/lib/useIsFetching.js.map +1 -1
- package/build/lib/useIsFetching.legacy.cjs +34 -0
- package/build/lib/useIsFetching.legacy.cjs.map +1 -0
- package/build/lib/{useIsFetching.esm.js → useIsFetching.legacy.js} +3 -2
- package/build/lib/useIsFetching.legacy.js.map +1 -0
- package/build/lib/useMutation.cjs +54 -0
- package/build/lib/useMutation.cjs.map +1 -0
- package/build/lib/useMutation.d.ts +2 -0
- package/build/lib/useMutation.d.ts.map +1 -0
- package/build/lib/useMutation.js +12 -32
- package/build/lib/useMutation.js.map +1 -1
- package/build/lib/useMutation.legacy.cjs +54 -0
- package/build/lib/useMutation.legacy.cjs.map +1 -0
- package/build/lib/{useMutation.esm.js → useMutation.legacy.js} +5 -4
- package/build/lib/useMutation.legacy.js.map +1 -0
- package/build/lib/useMutationState.cjs +60 -0
- package/build/lib/useMutationState.cjs.map +1 -0
- package/build/lib/useMutationState.d.ts +5 -3
- package/build/lib/useMutationState.d.ts.map +1 -0
- package/build/lib/useMutationState.js +13 -34
- package/build/lib/useMutationState.js.map +1 -1
- package/build/lib/useMutationState.legacy.cjs +60 -0
- package/build/lib/useMutationState.legacy.cjs.map +1 -0
- package/build/lib/{useMutationState.esm.js → useMutationState.legacy.js} +3 -2
- package/build/lib/useMutationState.legacy.js.map +1 -0
- package/build/lib/useQueries.cjs +99 -0
- package/build/lib/useQueries.cjs.map +1 -0
- package/build/lib/useQueries.d.ts +11 -8
- package/build/lib/useQueries.d.ts.map +1 -0
- package/build/lib/useQueries.js +52 -67
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQueries.legacy.cjs +102 -0
- package/build/lib/useQueries.legacy.cjs.map +1 -0
- package/build/lib/{useQueries.esm.js → useQueries.legacy.js} +37 -28
- package/build/lib/useQueries.legacy.js.map +1 -0
- package/build/lib/useQuery.cjs +14 -0
- package/build/lib/useQuery.cjs.map +1 -0
- package/build/lib/useQuery.d.ts +4 -8
- package/build/lib/useQuery.d.ts.map +1 -0
- package/build/lib/useQuery.js +5 -6
- package/build/lib/useQuery.js.map +1 -1
- package/build/lib/useQuery.legacy.cjs +14 -0
- package/build/lib/useQuery.legacy.cjs.map +1 -0
- package/build/lib/{useQuery.esm.js → useQuery.legacy.js} +3 -2
- package/build/lib/useQuery.legacy.js.map +1 -0
- package/build/lib/{utils.mjs → utils.cjs} +4 -2
- package/build/lib/{utils.mjs.map → utils.cjs.map} +1 -1
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.d.ts.map +1 -0
- package/build/lib/utils.js +1 -3
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.legacy.cjs +12 -0
- package/build/lib/utils.legacy.cjs.map +1 -0
- package/build/lib/{utils.esm.js → utils.legacy.js} +1 -1
- package/build/lib/utils.legacy.js.map +1 -0
- package/package.json +25 -15
- package/src/QueryClientProvider.tsx +1 -1
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +752 -620
- package/src/__tests__/ssr-hydration.test.tsx +2 -2
- package/src/__tests__/suspense.test.tsx +6 -88
- package/src/__tests__/useInfiniteQuery.type.test.tsx +0 -27
- package/src/__tests__/useMutation.test.tsx +4 -4
- package/src/__tests__/useMutationState.test.tsx +23 -0
- package/src/__tests__/useQueries.test.tsx +206 -83
- package/src/__tests__/useQuery.test.tsx +246 -338
- package/src/__tests__/useQuery.types.test.tsx +21 -0
- package/src/errorBoundaryUtils.ts +6 -5
- package/src/index.ts +1 -0
- package/src/queryOptions.ts +42 -0
- package/src/suspense.ts +9 -15
- package/src/useBaseQuery.ts +12 -23
- package/src/useInfiniteQuery.ts +1 -0
- package/src/useIsFetching.ts +1 -0
- package/src/useMutation.ts +2 -1
- package/src/useMutationState.ts +4 -3
- package/src/useQueries.ts +44 -26
- package/src/useQuery.ts +5 -17
- package/build/lib/HydrationBoundary.esm.js.map +0 -1
- package/build/lib/HydrationBoundary.mjs +0 -28
- package/build/lib/QueryClientProvider.esm.js.map +0 -1
- package/build/lib/QueryClientProvider.mjs +0 -30
- package/build/lib/QueryClientProvider.mjs.map +0 -1
- package/build/lib/QueryErrorResetBoundary.esm.js.map +0 -1
- package/build/lib/QueryErrorResetBoundary.mjs +0 -37
- package/build/lib/errorBoundaryUtils.esm.js.map +0 -1
- package/build/lib/errorBoundaryUtils.mjs +0 -27
- package/build/lib/errorBoundaryUtils.mjs.map +0 -1
- package/build/lib/index.esm.js +0 -12
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.mjs +0 -12
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/isRestoring.esm.js.map +0 -1
- package/build/lib/isRestoring.mjs +0 -8
- package/build/lib/isRestoring.mjs.map +0 -1
- package/build/lib/suspense.esm.js.map +0 -1
- package/build/lib/suspense.mjs.map +0 -1
- package/build/lib/useBaseQuery.esm.js +0 -62
- package/build/lib/useBaseQuery.esm.js.map +0 -1
- package/build/lib/useBaseQuery.mjs.map +0 -1
- package/build/lib/useInfiniteQuery.esm.js +0 -12
- package/build/lib/useInfiniteQuery.esm.js.map +0 -1
- package/build/lib/useInfiniteQuery.mjs.map +0 -1
- package/build/lib/useIsFetching.esm.js.map +0 -1
- package/build/lib/useIsFetching.mjs +0 -12
- package/build/lib/useIsFetching.mjs.map +0 -1
- package/build/lib/useMutation.esm.js.map +0 -1
- package/build/lib/useMutation.mjs +0 -32
- package/build/lib/useMutation.mjs.map +0 -1
- package/build/lib/useMutationState.esm.js.map +0 -1
- package/build/lib/useMutationState.mjs +0 -37
- package/build/lib/useMutationState.mjs.map +0 -1
- package/build/lib/useQueries.esm.js.map +0 -1
- package/build/lib/useQueries.mjs +0 -69
- package/build/lib/useQueries.mjs.map +0 -1
- package/build/lib/useQuery.esm.js.map +0 -1
- package/build/lib/useQuery.mjs +0 -11
- package/build/lib/useQuery.mjs.map +0 -1
- package/build/lib/utils.esm.js.map +0 -1
- package/build/umd/index.development.js +0 -3040
- 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/src/{isRestoring.tsx → isRestoring.ts} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { render, waitFor } from '@testing-library/react'
|
|
1
|
+
import { fireEvent, render, waitFor } from '@testing-library/react'
|
|
2
2
|
import * as React from 'react'
|
|
3
3
|
import { ErrorBoundary } from 'react-error-boundary'
|
|
4
4
|
|
|
@@ -71,6 +71,52 @@ describe('useQueries', () => {
|
|
|
71
71
|
expect(results[2]).toMatchObject([{ data: 1 }, { data: 2 }])
|
|
72
72
|
})
|
|
73
73
|
|
|
74
|
+
it('should track results', async () => {
|
|
75
|
+
const key1 = queryKey()
|
|
76
|
+
const results: UseQueryResult[][] = []
|
|
77
|
+
let count = 0
|
|
78
|
+
|
|
79
|
+
function Page() {
|
|
80
|
+
const result = useQueries({
|
|
81
|
+
queries: [
|
|
82
|
+
{
|
|
83
|
+
queryKey: key1,
|
|
84
|
+
queryFn: async () => {
|
|
85
|
+
await sleep(10)
|
|
86
|
+
count++
|
|
87
|
+
return count
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
})
|
|
92
|
+
results.push(result)
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<div>
|
|
96
|
+
<div>data: {String(result[0].data ?? 'null')} </div>
|
|
97
|
+
<button onClick={() => result[0].refetch()}>refetch</button>
|
|
98
|
+
</div>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const rendered = renderWithClient(queryClient, <Page />)
|
|
103
|
+
|
|
104
|
+
await waitFor(() => rendered.getByText('data: 1'))
|
|
105
|
+
|
|
106
|
+
expect(results.length).toBe(2)
|
|
107
|
+
expect(results[0]).toMatchObject([{ data: undefined }])
|
|
108
|
+
expect(results[1]).toMatchObject([{ data: 1 }])
|
|
109
|
+
|
|
110
|
+
fireEvent.click(rendered.getByRole('button', { name: /refetch/i }))
|
|
111
|
+
|
|
112
|
+
await waitFor(() => rendered.getByText('data: 2'))
|
|
113
|
+
|
|
114
|
+
// only one render for data update, no render for isFetching transition
|
|
115
|
+
expect(results.length).toBe(3)
|
|
116
|
+
|
|
117
|
+
expect(results[2]).toMatchObject([{ data: 2 }])
|
|
118
|
+
})
|
|
119
|
+
|
|
74
120
|
it('handles type parameter - tuple of tuples', async () => {
|
|
75
121
|
const key1 = queryKey()
|
|
76
122
|
const key2 = queryKey()
|
|
@@ -144,10 +190,6 @@ describe('useQueries', () => {
|
|
|
144
190
|
expectTypeNotAny(a)
|
|
145
191
|
return a.toLowerCase()
|
|
146
192
|
},
|
|
147
|
-
onSuccess: (a) => {
|
|
148
|
-
expectType<string>(a)
|
|
149
|
-
expectTypeNotAny(a)
|
|
150
|
-
},
|
|
151
193
|
placeholderData: 'string',
|
|
152
194
|
// @ts-expect-error (initialData: string)
|
|
153
195
|
initialData: 123,
|
|
@@ -160,14 +202,6 @@ describe('useQueries', () => {
|
|
|
160
202
|
expectTypeNotAny(a)
|
|
161
203
|
return parseInt(a)
|
|
162
204
|
},
|
|
163
|
-
onSuccess: (a) => {
|
|
164
|
-
expectType<number>(a)
|
|
165
|
-
expectTypeNotAny(a)
|
|
166
|
-
},
|
|
167
|
-
onError: (e) => {
|
|
168
|
-
expectType<boolean>(e)
|
|
169
|
-
expectTypeNotAny(e)
|
|
170
|
-
},
|
|
171
205
|
placeholderData: 'string',
|
|
172
206
|
// @ts-expect-error (initialData: string)
|
|
173
207
|
initialData: 123,
|
|
@@ -304,10 +338,6 @@ describe('useQueries', () => {
|
|
|
304
338
|
expectTypeNotAny(a)
|
|
305
339
|
return a.toLowerCase()
|
|
306
340
|
},
|
|
307
|
-
onSuccess: (a) => {
|
|
308
|
-
expectType<string>(a)
|
|
309
|
-
expectTypeNotAny(a)
|
|
310
|
-
},
|
|
311
341
|
placeholderData: 'string',
|
|
312
342
|
// @ts-expect-error (initialData: string)
|
|
313
343
|
initialData: 123,
|
|
@@ -320,14 +350,6 @@ describe('useQueries', () => {
|
|
|
320
350
|
expectTypeNotAny(a)
|
|
321
351
|
return parseInt(a)
|
|
322
352
|
},
|
|
323
|
-
onSuccess: (a) => {
|
|
324
|
-
expectType<number>(a)
|
|
325
|
-
expectTypeNotAny(a)
|
|
326
|
-
},
|
|
327
|
-
onError: (e) => {
|
|
328
|
-
expectType<boolean>(e)
|
|
329
|
-
expectTypeNotAny(e)
|
|
330
|
-
},
|
|
331
353
|
placeholderData: 'string',
|
|
332
354
|
// @ts-expect-error (initialData: string)
|
|
333
355
|
initialData: 123,
|
|
@@ -423,60 +445,38 @@ describe('useQueries', () => {
|
|
|
423
445
|
],
|
|
424
446
|
})
|
|
425
447
|
|
|
426
|
-
// select
|
|
448
|
+
// select params are "indirectly" enforced
|
|
427
449
|
useQueries({
|
|
428
450
|
queries: [
|
|
429
451
|
// unfortunately TS will not suggest the type for you
|
|
430
452
|
{
|
|
431
453
|
queryKey: key1,
|
|
432
454
|
queryFn: () => 'string',
|
|
433
|
-
// @ts-expect-error (noImplicitAny)
|
|
434
|
-
onSuccess: (a) => null,
|
|
435
|
-
// @ts-expect-error (noImplicitAny)
|
|
436
|
-
onSettled: (a) => null,
|
|
437
455
|
},
|
|
438
456
|
// however you can add a type to the callback
|
|
439
457
|
{
|
|
440
458
|
queryKey: key2,
|
|
441
459
|
queryFn: () => 'string',
|
|
442
|
-
onSuccess: (a: string) => {
|
|
443
|
-
expectType<string>(a)
|
|
444
|
-
expectTypeNotAny(a)
|
|
445
|
-
},
|
|
446
|
-
onSettled: (a: string | undefined) => {
|
|
447
|
-
expectType<string | undefined>(a)
|
|
448
|
-
expectTypeNotAny(a)
|
|
449
|
-
},
|
|
450
460
|
},
|
|
451
461
|
// the type you do pass is enforced
|
|
452
462
|
{
|
|
453
463
|
queryKey: key3,
|
|
454
464
|
queryFn: () => 'string',
|
|
455
|
-
// @ts-expect-error (only accepts string)
|
|
456
|
-
onSuccess: (a: number) => null,
|
|
457
465
|
},
|
|
458
466
|
{
|
|
459
467
|
queryKey: key4,
|
|
460
468
|
queryFn: () => 'string',
|
|
461
469
|
select: (a: string) => parseInt(a),
|
|
462
|
-
// @ts-expect-error (select is defined => only accepts number)
|
|
463
|
-
onSuccess: (a: string) => null,
|
|
464
|
-
onSettled: (a: number | undefined) => {
|
|
465
|
-
expectType<number | undefined>(a)
|
|
466
|
-
expectTypeNotAny(a)
|
|
467
|
-
},
|
|
468
470
|
},
|
|
469
471
|
],
|
|
470
472
|
})
|
|
471
473
|
|
|
472
474
|
// callbacks are also indirectly enforced with Array.map
|
|
473
475
|
useQueries({
|
|
474
|
-
// @ts-expect-error (onSuccess only accepts string)
|
|
475
476
|
queries: Array(50).map((_, i) => ({
|
|
476
477
|
queryKey: ['key', i] as const,
|
|
477
478
|
queryFn: () => i + 10,
|
|
478
479
|
select: (data: number) => data.toString(),
|
|
479
|
-
onSuccess: (_data: number) => null,
|
|
480
480
|
})),
|
|
481
481
|
})
|
|
482
482
|
useQueries({
|
|
@@ -484,7 +484,6 @@ describe('useQueries', () => {
|
|
|
484
484
|
queryKey: ['key', i] as const,
|
|
485
485
|
queryFn: () => i + 10,
|
|
486
486
|
select: (data: number) => data.toString(),
|
|
487
|
-
onSuccess: (_data: string) => null,
|
|
488
487
|
})),
|
|
489
488
|
})
|
|
490
489
|
|
|
@@ -494,32 +493,15 @@ describe('useQueries', () => {
|
|
|
494
493
|
{
|
|
495
494
|
queryKey: key1,
|
|
496
495
|
queryFn: () => 'string',
|
|
497
|
-
// @ts-expect-error (noImplicitAny)
|
|
498
|
-
onSuccess: (a) => null,
|
|
499
|
-
// @ts-expect-error (noImplicitAny)
|
|
500
|
-
onSettled: (a) => null,
|
|
501
496
|
},
|
|
502
497
|
{
|
|
503
498
|
queryKey: key2,
|
|
504
499
|
queryFn: () => 'string',
|
|
505
|
-
onSuccess: (a: string) => {
|
|
506
|
-
expectType<string>(a)
|
|
507
|
-
expectTypeNotAny(a)
|
|
508
|
-
},
|
|
509
|
-
onSettled: (a: string | undefined) => {
|
|
510
|
-
expectType<string | undefined>(a)
|
|
511
|
-
expectTypeNotAny(a)
|
|
512
|
-
},
|
|
513
500
|
},
|
|
514
501
|
{
|
|
515
502
|
queryKey: key4,
|
|
516
503
|
queryFn: () => 'string',
|
|
517
504
|
select: (a: string) => parseInt(a),
|
|
518
|
-
onSuccess: (_a: number) => null,
|
|
519
|
-
onSettled: (a: number | undefined) => {
|
|
520
|
-
expectType<number | undefined>(a)
|
|
521
|
-
expectTypeNotAny(a)
|
|
522
|
-
},
|
|
523
505
|
},
|
|
524
506
|
],
|
|
525
507
|
})
|
|
@@ -533,12 +515,6 @@ describe('useQueries', () => {
|
|
|
533
515
|
{
|
|
534
516
|
queryKey: key1,
|
|
535
517
|
queryFn: () => Promise.resolve('string'),
|
|
536
|
-
onSuccess: (a: string) => {
|
|
537
|
-
expectType<string>(a)
|
|
538
|
-
expectTypeNotAny(a)
|
|
539
|
-
},
|
|
540
|
-
// @ts-expect-error (refuses to accept a Promise)
|
|
541
|
-
onSettled: (a: Promise<string>) => null,
|
|
542
518
|
},
|
|
543
519
|
],
|
|
544
520
|
})
|
|
@@ -645,11 +621,10 @@ describe('useQueries', () => {
|
|
|
645
621
|
queries: queries.map(
|
|
646
622
|
// no need to type the mapped query
|
|
647
623
|
(query) => {
|
|
648
|
-
const { queryFn: fn, queryKey: key
|
|
624
|
+
const { queryFn: fn, queryKey: key } = query
|
|
649
625
|
expectType<QueryFunction<TQueryFnData, TQueryKey> | undefined>(fn)
|
|
650
626
|
return {
|
|
651
627
|
queryKey: key,
|
|
652
|
-
onError: err,
|
|
653
628
|
queryFn: fn
|
|
654
629
|
? (ctx: QueryFunctionContext<TQueryKey>) => {
|
|
655
630
|
expectType<TQueryKey>(ctx.queryKey)
|
|
@@ -715,7 +690,7 @@ describe('useQueries', () => {
|
|
|
715
690
|
}
|
|
716
691
|
})
|
|
717
692
|
|
|
718
|
-
it("should throw error if in one of queries' queryFn throws and
|
|
693
|
+
it("should throw error if in one of queries' queryFn throws and throwOnError is in use", async () => {
|
|
719
694
|
const consoleMock = vi
|
|
720
695
|
.spyOn(console, 'error')
|
|
721
696
|
.mockImplementation(() => undefined)
|
|
@@ -732,14 +707,14 @@ describe('useQueries', () => {
|
|
|
732
707
|
queryFn: () =>
|
|
733
708
|
Promise.reject(
|
|
734
709
|
new Error(
|
|
735
|
-
'this should not throw because
|
|
710
|
+
'this should not throw because throwOnError is not set',
|
|
736
711
|
),
|
|
737
712
|
),
|
|
738
713
|
},
|
|
739
714
|
{
|
|
740
715
|
queryKey: key2,
|
|
741
716
|
queryFn: () => Promise.reject(new Error('single query error')),
|
|
742
|
-
|
|
717
|
+
throwOnError: true,
|
|
743
718
|
retry: false,
|
|
744
719
|
},
|
|
745
720
|
{
|
|
@@ -752,7 +727,7 @@ describe('useQueries', () => {
|
|
|
752
727
|
Promise.reject(
|
|
753
728
|
new Error('this should not throw because query#2 already did'),
|
|
754
729
|
),
|
|
755
|
-
|
|
730
|
+
throwOnError: true,
|
|
756
731
|
retry: false,
|
|
757
732
|
},
|
|
758
733
|
],
|
|
@@ -780,7 +755,7 @@ describe('useQueries', () => {
|
|
|
780
755
|
consoleMock.mockRestore()
|
|
781
756
|
})
|
|
782
757
|
|
|
783
|
-
it("should throw error if in one of queries' queryFn throws and
|
|
758
|
+
it("should throw error if in one of queries' queryFn throws and throwOnError function resolves to true", async () => {
|
|
784
759
|
const consoleMock = vi
|
|
785
760
|
.spyOn(console, 'error')
|
|
786
761
|
.mockImplementation(() => undefined)
|
|
@@ -797,10 +772,10 @@ describe('useQueries', () => {
|
|
|
797
772
|
queryFn: () =>
|
|
798
773
|
Promise.reject(
|
|
799
774
|
new Error(
|
|
800
|
-
'this should not throw because
|
|
775
|
+
'this should not throw because throwOnError function resolves to false',
|
|
801
776
|
),
|
|
802
777
|
),
|
|
803
|
-
|
|
778
|
+
throwOnError: () => false,
|
|
804
779
|
retry: false,
|
|
805
780
|
},
|
|
806
781
|
{
|
|
@@ -810,7 +785,7 @@ describe('useQueries', () => {
|
|
|
810
785
|
{
|
|
811
786
|
queryKey: key3,
|
|
812
787
|
queryFn: () => Promise.reject(new Error('single query error')),
|
|
813
|
-
|
|
788
|
+
throwOnError: () => true,
|
|
814
789
|
retry: false,
|
|
815
790
|
},
|
|
816
791
|
{
|
|
@@ -819,7 +794,7 @@ describe('useQueries', () => {
|
|
|
819
794
|
Promise.reject(
|
|
820
795
|
new Error('this should not throw because query#3 already did'),
|
|
821
796
|
),
|
|
822
|
-
|
|
797
|
+
throwOnError: true,
|
|
823
798
|
retry: false,
|
|
824
799
|
},
|
|
825
800
|
],
|
|
@@ -873,4 +848,152 @@ describe('useQueries', () => {
|
|
|
873
848
|
|
|
874
849
|
await waitFor(() => rendered.getByText('data: custom client'))
|
|
875
850
|
})
|
|
851
|
+
|
|
852
|
+
it('should combine queries', async () => {
|
|
853
|
+
const key1 = queryKey()
|
|
854
|
+
const key2 = queryKey()
|
|
855
|
+
|
|
856
|
+
function Page() {
|
|
857
|
+
const queries = useQueries(
|
|
858
|
+
{
|
|
859
|
+
queries: [
|
|
860
|
+
{
|
|
861
|
+
queryKey: key1,
|
|
862
|
+
queryFn: () => Promise.resolve('first result'),
|
|
863
|
+
},
|
|
864
|
+
{
|
|
865
|
+
queryKey: key2,
|
|
866
|
+
queryFn: () => Promise.resolve('second result'),
|
|
867
|
+
},
|
|
868
|
+
],
|
|
869
|
+
combine: (results) => {
|
|
870
|
+
return {
|
|
871
|
+
combined: true,
|
|
872
|
+
res: results.map((res) => res.data).join(','),
|
|
873
|
+
}
|
|
874
|
+
},
|
|
875
|
+
},
|
|
876
|
+
queryClient,
|
|
877
|
+
)
|
|
878
|
+
|
|
879
|
+
return (
|
|
880
|
+
<div>
|
|
881
|
+
<div>
|
|
882
|
+
data: {String(queries.combined)} {queries.res}
|
|
883
|
+
</div>
|
|
884
|
+
</div>
|
|
885
|
+
)
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
const rendered = render(<Page />)
|
|
889
|
+
|
|
890
|
+
await waitFor(() =>
|
|
891
|
+
rendered.getByText('data: true first result,second result'),
|
|
892
|
+
)
|
|
893
|
+
})
|
|
894
|
+
|
|
895
|
+
it('should track property access through combine function', async () => {
|
|
896
|
+
const key1 = queryKey()
|
|
897
|
+
const key2 = queryKey()
|
|
898
|
+
let count = 0
|
|
899
|
+
const results: Array<unknown> = []
|
|
900
|
+
|
|
901
|
+
function Page() {
|
|
902
|
+
const queries = useQueries(
|
|
903
|
+
{
|
|
904
|
+
queries: [
|
|
905
|
+
{
|
|
906
|
+
queryKey: key1,
|
|
907
|
+
queryFn: async () => {
|
|
908
|
+
await sleep(5)
|
|
909
|
+
return Promise.resolve('first result ' + count)
|
|
910
|
+
},
|
|
911
|
+
},
|
|
912
|
+
{
|
|
913
|
+
queryKey: key2,
|
|
914
|
+
queryFn: async () => {
|
|
915
|
+
await sleep(50)
|
|
916
|
+
return Promise.resolve('second result ' + count)
|
|
917
|
+
},
|
|
918
|
+
},
|
|
919
|
+
],
|
|
920
|
+
combine: (queryResults) => {
|
|
921
|
+
return {
|
|
922
|
+
combined: true,
|
|
923
|
+
refetch: () => queryResults.forEach((res) => res.refetch()),
|
|
924
|
+
res: queryResults
|
|
925
|
+
.flatMap((res) => (res.data ? [res.data] : []))
|
|
926
|
+
.join(','),
|
|
927
|
+
}
|
|
928
|
+
},
|
|
929
|
+
},
|
|
930
|
+
queryClient,
|
|
931
|
+
)
|
|
932
|
+
|
|
933
|
+
results.push(queries)
|
|
934
|
+
|
|
935
|
+
return (
|
|
936
|
+
<div>
|
|
937
|
+
<div>
|
|
938
|
+
data: {String(queries.combined)} {queries.res}
|
|
939
|
+
</div>
|
|
940
|
+
<button onClick={() => queries.refetch()}>refetch</button>
|
|
941
|
+
</div>
|
|
942
|
+
)
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
const rendered = render(<Page />)
|
|
946
|
+
|
|
947
|
+
await waitFor(() =>
|
|
948
|
+
rendered.getByText('data: true first result 0,second result 0'),
|
|
949
|
+
)
|
|
950
|
+
|
|
951
|
+
expect(results.length).toBe(3)
|
|
952
|
+
|
|
953
|
+
expect(results[0]).toStrictEqual({
|
|
954
|
+
combined: true,
|
|
955
|
+
refetch: expect.any(Function),
|
|
956
|
+
res: '',
|
|
957
|
+
})
|
|
958
|
+
|
|
959
|
+
expect(results[1]).toStrictEqual({
|
|
960
|
+
combined: true,
|
|
961
|
+
refetch: expect.any(Function),
|
|
962
|
+
res: 'first result 0',
|
|
963
|
+
})
|
|
964
|
+
|
|
965
|
+
expect(results[2]).toStrictEqual({
|
|
966
|
+
combined: true,
|
|
967
|
+
refetch: expect.any(Function),
|
|
968
|
+
res: 'first result 0,second result 0',
|
|
969
|
+
})
|
|
970
|
+
|
|
971
|
+
count++
|
|
972
|
+
|
|
973
|
+
fireEvent.click(rendered.getByRole('button', { name: /refetch/i }))
|
|
974
|
+
|
|
975
|
+
await waitFor(() =>
|
|
976
|
+
rendered.getByText('data: true first result 1,second result 1'),
|
|
977
|
+
)
|
|
978
|
+
|
|
979
|
+
expect(results.length).toBe(5)
|
|
980
|
+
|
|
981
|
+
expect(results[3]).toStrictEqual({
|
|
982
|
+
combined: true,
|
|
983
|
+
refetch: expect.any(Function),
|
|
984
|
+
res: 'first result 1,second result 0',
|
|
985
|
+
})
|
|
986
|
+
|
|
987
|
+
expect(results[4]).toStrictEqual({
|
|
988
|
+
combined: true,
|
|
989
|
+
refetch: expect.any(Function),
|
|
990
|
+
res: 'first result 1,second result 1',
|
|
991
|
+
})
|
|
992
|
+
|
|
993
|
+
fireEvent.click(rendered.getByRole('button', { name: /refetch/i }))
|
|
994
|
+
|
|
995
|
+
await sleep(100)
|
|
996
|
+
// no further re-render because data didn't change
|
|
997
|
+
expect(results.length).toBe(5)
|
|
998
|
+
})
|
|
876
999
|
})
|