@pyreon/query 0.11.5 → 0.11.7
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/README.md +58 -56
- package/lib/index.js.map +1 -1
- package/lib/types/index.d.ts +7 -7
- package/package.json +16 -16
- package/src/index.ts +21 -33
- package/src/query-client.ts +5 -5
- package/src/tests/query-additional.test.tsx +59 -59
- package/src/tests/query.test.tsx +243 -243
- package/src/tests/sse.test.tsx +131 -131
- package/src/tests/subscription.test.tsx +97 -97
- package/src/use-infinite-query.ts +7 -7
- package/src/use-is-fetching.ts +5 -5
- package/src/use-mutation.ts +8 -8
- package/src/use-queries.ts +6 -6
- package/src/use-query-error-reset-boundary.ts +6 -6
- package/src/use-query.ts +8 -8
- package/src/use-sse.ts +19 -19
- package/src/use-subscription.ts +18 -18
- package/src/use-suspense-query.ts +12 -12
package/package.json
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/query",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.7",
|
|
4
4
|
"description": "Pyreon adapter for TanStack Query",
|
|
5
|
+
"homepage": "https://github.com/pyreon/pyreon/tree/main/packages/query#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/pyreon/pyreon/issues"
|
|
8
|
+
},
|
|
5
9
|
"license": "MIT",
|
|
6
10
|
"repository": {
|
|
7
11
|
"type": "git",
|
|
8
12
|
"url": "https://github.com/pyreon/pyreon.git",
|
|
9
13
|
"directory": "packages/fundamentals/query"
|
|
10
14
|
},
|
|
11
|
-
"homepage": "https://github.com/pyreon/pyreon/tree/main/packages/query#readme",
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/pyreon/pyreon/issues"
|
|
14
|
-
},
|
|
15
|
-
"publishConfig": {
|
|
16
|
-
"access": "public"
|
|
17
|
-
},
|
|
18
15
|
"files": [
|
|
19
16
|
"lib",
|
|
20
17
|
"src",
|
|
@@ -33,24 +30,27 @@
|
|
|
33
30
|
"types": "./lib/types/index.d.ts"
|
|
34
31
|
}
|
|
35
32
|
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "vl_rolldown_build",
|
|
38
38
|
"dev": "vl_rolldown_build-watch",
|
|
39
39
|
"test": "vitest run",
|
|
40
40
|
"typecheck": "tsc --noEmit",
|
|
41
|
-
"lint": "
|
|
41
|
+
"lint": "oxlint ."
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@tanstack/query-core": "^5.0.0"
|
|
45
45
|
},
|
|
46
|
-
"peerDependencies": {
|
|
47
|
-
"@pyreon/core": "^0.11.5",
|
|
48
|
-
"@pyreon/reactivity": "^0.11.5"
|
|
49
|
-
},
|
|
50
46
|
"devDependencies": {
|
|
51
47
|
"@happy-dom/global-registrator": "^20.8.3",
|
|
52
|
-
"@pyreon/core": "^0.11.
|
|
53
|
-
"@pyreon/reactivity": "^0.11.
|
|
54
|
-
"@pyreon/runtime-dom": "^0.11.
|
|
48
|
+
"@pyreon/core": "^0.11.7",
|
|
49
|
+
"@pyreon/reactivity": "^0.11.7",
|
|
50
|
+
"@pyreon/runtime-dom": "^0.11.7"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@pyreon/core": "^0.11.7",
|
|
54
|
+
"@pyreon/reactivity": "^0.11.7"
|
|
55
55
|
}
|
|
56
56
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type {
|
|
|
12
12
|
QueryKey,
|
|
13
13
|
RefetchOptions,
|
|
14
14
|
RefetchQueryFilters,
|
|
15
|
-
} from
|
|
15
|
+
} from '@tanstack/query-core'
|
|
16
16
|
export {
|
|
17
17
|
CancelledError,
|
|
18
18
|
defaultShouldDehydrateMutation,
|
|
@@ -25,49 +25,37 @@ export {
|
|
|
25
25
|
MutationCache,
|
|
26
26
|
QueryCache,
|
|
27
27
|
QueryClient,
|
|
28
|
-
} from
|
|
28
|
+
} from '@tanstack/query-core'
|
|
29
29
|
|
|
30
30
|
// ─── Pyreon adapter ─────────────────────────────────────────────────────────────
|
|
31
31
|
|
|
32
|
-
export type { QueryClientProviderProps } from
|
|
33
|
-
export {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
} from
|
|
38
|
-
export
|
|
39
|
-
export {
|
|
40
|
-
export {
|
|
41
|
-
export type {
|
|
42
|
-
export {
|
|
43
|
-
export type {
|
|
44
|
-
export { useQueries } from "./use-queries"
|
|
45
|
-
export type { UseQueryResult } from "./use-query"
|
|
46
|
-
export { useQuery } from "./use-query"
|
|
47
|
-
export type { QueryErrorResetBoundaryProps } from "./use-query-error-reset-boundary"
|
|
32
|
+
export type { QueryClientProviderProps } from './query-client'
|
|
33
|
+
export { QueryClientContext, QueryClientProvider, useQueryClient } from './query-client'
|
|
34
|
+
export type { UseInfiniteQueryResult } from './use-infinite-query'
|
|
35
|
+
export { useInfiniteQuery } from './use-infinite-query'
|
|
36
|
+
export { useIsFetching, useIsMutating } from './use-is-fetching'
|
|
37
|
+
export type { UseMutationResult } from './use-mutation'
|
|
38
|
+
export { useMutation } from './use-mutation'
|
|
39
|
+
export type { UseQueriesOptions } from './use-queries'
|
|
40
|
+
export { useQueries } from './use-queries'
|
|
41
|
+
export type { UseQueryResult } from './use-query'
|
|
42
|
+
export { useQuery } from './use-query'
|
|
43
|
+
export type { QueryErrorResetBoundaryProps } from './use-query-error-reset-boundary'
|
|
48
44
|
export {
|
|
49
45
|
QueryErrorResetBoundary,
|
|
50
46
|
useQueryErrorResetBoundary,
|
|
51
|
-
} from
|
|
52
|
-
export type {
|
|
53
|
-
|
|
54
|
-
UseSSEOptions,
|
|
55
|
-
UseSSEResult,
|
|
56
|
-
} from "./use-sse"
|
|
57
|
-
export { useSSE } from "./use-sse"
|
|
47
|
+
} from './use-query-error-reset-boundary'
|
|
48
|
+
export type { SSEStatus, UseSSEOptions, UseSSEResult } from './use-sse'
|
|
49
|
+
export { useSSE } from './use-sse'
|
|
58
50
|
export type {
|
|
59
51
|
SubscriptionStatus,
|
|
60
52
|
UseSubscriptionOptions,
|
|
61
53
|
UseSubscriptionResult,
|
|
62
|
-
} from
|
|
63
|
-
export { useSubscription } from
|
|
54
|
+
} from './use-subscription'
|
|
55
|
+
export { useSubscription } from './use-subscription'
|
|
64
56
|
export type {
|
|
65
57
|
QuerySuspenseProps,
|
|
66
58
|
UseSuspenseInfiniteQueryResult,
|
|
67
59
|
UseSuspenseQueryResult,
|
|
68
|
-
} from
|
|
69
|
-
export {
|
|
70
|
-
QuerySuspense,
|
|
71
|
-
useSuspenseInfiniteQuery,
|
|
72
|
-
useSuspenseQuery,
|
|
73
|
-
} from "./use-suspense-query"
|
|
60
|
+
} from './use-suspense-query'
|
|
61
|
+
export { QuerySuspense, useSuspenseInfiniteQuery, useSuspenseQuery } from './use-suspense-query'
|
package/src/query-client.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Props, VNode, VNodeChild } from
|
|
2
|
-
import { createContext, onMount, provide, useContext } from
|
|
3
|
-
import type { QueryClient } from
|
|
1
|
+
import type { Props, VNode, VNodeChild } from '@pyreon/core'
|
|
2
|
+
import { createContext, onMount, provide, useContext } from '@pyreon/core'
|
|
3
|
+
import type { QueryClient } from '@tanstack/query-core'
|
|
4
4
|
|
|
5
5
|
export interface QueryClientProviderProps extends Props {
|
|
6
6
|
client: QueryClient
|
|
@@ -28,7 +28,7 @@ export function QueryClientProvider(props: QueryClientProviderProps): VNode {
|
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
const ch = props.children
|
|
31
|
-
return (typeof ch ===
|
|
31
|
+
return (typeof ch === 'function' ? (ch as () => VNodeChild)() : ch) as VNode
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/**
|
|
@@ -39,7 +39,7 @@ export function useQueryClient(): QueryClient {
|
|
|
39
39
|
const client = useContext(QueryClientContext)
|
|
40
40
|
if (!client) {
|
|
41
41
|
throw new Error(
|
|
42
|
-
|
|
42
|
+
'[@pyreon/query] No QueryClient found. Wrap your app with <QueryClientProvider client={client}>.',
|
|
43
43
|
)
|
|
44
44
|
}
|
|
45
45
|
return client
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { mount } from
|
|
2
|
-
import { QueryClient } from
|
|
1
|
+
import { mount } from '@pyreon/runtime-dom'
|
|
2
|
+
import { QueryClient } from '@tanstack/query-core'
|
|
3
3
|
import {
|
|
4
4
|
QueryClientProvider,
|
|
5
5
|
QuerySuspense,
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
useQuery,
|
|
11
11
|
useQueryClient,
|
|
12
12
|
useSuspenseQuery,
|
|
13
|
-
} from
|
|
13
|
+
} from '../index'
|
|
14
14
|
|
|
15
15
|
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
16
16
|
|
|
@@ -32,27 +32,27 @@ function deferred<T>() {
|
|
|
32
32
|
|
|
33
33
|
// ─── useInfiniteQuery — additional ───────────────────────────────────────────
|
|
34
34
|
|
|
35
|
-
describe(
|
|
35
|
+
describe('useInfiniteQuery — additional', () => {
|
|
36
36
|
let client: QueryClient
|
|
37
37
|
beforeEach(() => {
|
|
38
38
|
client = makeClient()
|
|
39
39
|
})
|
|
40
40
|
|
|
41
|
-
it(
|
|
41
|
+
it('isFetchingNextPage is true during fetchNextPage', async () => {
|
|
42
42
|
const { promise: pagePromise, resolve: resolveNextPage } = deferred<string>()
|
|
43
43
|
let callCount = 0
|
|
44
44
|
let query: ReturnType<typeof useInfiniteQuery<string>> | undefined
|
|
45
45
|
|
|
46
|
-
const el = document.createElement(
|
|
46
|
+
const el = document.createElement('div')
|
|
47
47
|
document.body.appendChild(el)
|
|
48
48
|
const unmount = mount(
|
|
49
49
|
<QueryClientProvider client={client}>
|
|
50
50
|
{() => {
|
|
51
51
|
query = useInfiniteQuery(() => ({
|
|
52
|
-
queryKey: [
|
|
52
|
+
queryKey: ['inf-fetching-next'],
|
|
53
53
|
queryFn: ({ pageParam }: { pageParam: number }) => {
|
|
54
54
|
callCount++
|
|
55
|
-
if (callCount === 1) return Promise.resolve(
|
|
55
|
+
if (callCount === 1) return Promise.resolve('page-0')
|
|
56
56
|
return pagePromise
|
|
57
57
|
},
|
|
58
58
|
initialPageParam: 0,
|
|
@@ -77,26 +77,26 @@ describe("useInfiniteQuery — additional", () => {
|
|
|
77
77
|
expect(query!.isFetching()).toBe(true)
|
|
78
78
|
|
|
79
79
|
// Resolve and verify
|
|
80
|
-
resolveNextPage(
|
|
80
|
+
resolveNextPage('page-1')
|
|
81
81
|
await nextPromise
|
|
82
82
|
await new Promise((r) => setTimeout(r, 10))
|
|
83
83
|
expect(query!.isFetchingNextPage()).toBe(false)
|
|
84
|
-
expect(query!.data()?.pages).toEqual([
|
|
84
|
+
expect(query!.data()?.pages).toEqual(['page-0', 'page-1'])
|
|
85
85
|
|
|
86
86
|
unmount()
|
|
87
87
|
el.remove()
|
|
88
88
|
})
|
|
89
89
|
|
|
90
|
-
it(
|
|
90
|
+
it('hasNextPage is false when getNextPageParam returns undefined', async () => {
|
|
91
91
|
let query: ReturnType<typeof useInfiniteQuery<string>> | undefined
|
|
92
|
-
const el = document.createElement(
|
|
92
|
+
const el = document.createElement('div')
|
|
93
93
|
document.body.appendChild(el)
|
|
94
94
|
const unmount = mount(
|
|
95
95
|
<QueryClientProvider client={client}>
|
|
96
96
|
{() => {
|
|
97
97
|
query = useInfiniteQuery(() => ({
|
|
98
|
-
queryKey: [
|
|
99
|
-
queryFn: () => Promise.resolve(
|
|
98
|
+
queryKey: ['inf-no-next'],
|
|
99
|
+
queryFn: () => Promise.resolve('only-page'),
|
|
100
100
|
initialPageParam: 0,
|
|
101
101
|
getNextPageParam: () => undefined, // No more pages
|
|
102
102
|
}))
|
|
@@ -108,20 +108,20 @@ describe("useInfiniteQuery — additional", () => {
|
|
|
108
108
|
|
|
109
109
|
await new Promise((r) => setTimeout(r, 20))
|
|
110
110
|
expect(query!.hasNextPage()).toBe(false)
|
|
111
|
-
expect(query!.data()?.pages).toEqual([
|
|
111
|
+
expect(query!.data()?.pages).toEqual(['only-page'])
|
|
112
112
|
unmount()
|
|
113
113
|
el.remove()
|
|
114
114
|
})
|
|
115
115
|
|
|
116
|
-
it(
|
|
116
|
+
it('multiple fetchNextPage calls accumulate pages', async () => {
|
|
117
117
|
let query: ReturnType<typeof useInfiniteQuery<string>> | undefined
|
|
118
|
-
const el = document.createElement(
|
|
118
|
+
const el = document.createElement('div')
|
|
119
119
|
document.body.appendChild(el)
|
|
120
120
|
const unmount = mount(
|
|
121
121
|
<QueryClientProvider client={client}>
|
|
122
122
|
{() => {
|
|
123
123
|
query = useInfiniteQuery(() => ({
|
|
124
|
-
queryKey: [
|
|
124
|
+
queryKey: ['inf-multi-fetch'],
|
|
125
125
|
queryFn: ({ pageParam }: { pageParam: number }) => Promise.resolve(`p${pageParam}`),
|
|
126
126
|
initialPageParam: 0,
|
|
127
127
|
getNextPageParam: (_last: string, _all: string[], lastParam: number) =>
|
|
@@ -134,7 +134,7 @@ describe("useInfiniteQuery — additional", () => {
|
|
|
134
134
|
)
|
|
135
135
|
|
|
136
136
|
await new Promise((r) => setTimeout(r, 20))
|
|
137
|
-
expect(query!.data()?.pages).toEqual([
|
|
137
|
+
expect(query!.data()?.pages).toEqual(['p0'])
|
|
138
138
|
|
|
139
139
|
// Fetch pages 1, 2, 3 sequentially
|
|
140
140
|
for (let i = 1; i <= 3; i++) {
|
|
@@ -142,12 +142,12 @@ describe("useInfiniteQuery — additional", () => {
|
|
|
142
142
|
await new Promise((r) => setTimeout(r, 10))
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
expect(query!.data()?.pages).toEqual([
|
|
145
|
+
expect(query!.data()?.pages).toEqual(['p0', 'p1', 'p2', 'p3'])
|
|
146
146
|
expect(query!.hasNextPage()).toBe(true) // page 4 is available
|
|
147
147
|
|
|
148
148
|
await query!.fetchNextPage()
|
|
149
149
|
await new Promise((r) => setTimeout(r, 10))
|
|
150
|
-
expect(query!.data()?.pages).toEqual([
|
|
150
|
+
expect(query!.data()?.pages).toEqual(['p0', 'p1', 'p2', 'p3', 'p4'])
|
|
151
151
|
expect(query!.hasNextPage()).toBe(false) // No more pages
|
|
152
152
|
unmount()
|
|
153
153
|
el.remove()
|
|
@@ -156,23 +156,23 @@ describe("useInfiniteQuery — additional", () => {
|
|
|
156
156
|
|
|
157
157
|
// ─── useSuspenseQuery — additional ───────────────────────────────────────────
|
|
158
158
|
|
|
159
|
-
describe(
|
|
159
|
+
describe('useSuspenseQuery — suspense behavior', () => {
|
|
160
160
|
let client: QueryClient
|
|
161
161
|
beforeEach(() => {
|
|
162
162
|
client = makeClient()
|
|
163
163
|
})
|
|
164
164
|
|
|
165
|
-
it(
|
|
165
|
+
it('isPending is true while query is loading', async () => {
|
|
166
166
|
const { promise, resolve } = deferred<string>()
|
|
167
167
|
let query: ReturnType<typeof useSuspenseQuery<string>> | undefined
|
|
168
168
|
|
|
169
|
-
const el = document.createElement(
|
|
169
|
+
const el = document.createElement('div')
|
|
170
170
|
document.body.appendChild(el)
|
|
171
171
|
const unmount = mount(
|
|
172
172
|
<QueryClientProvider client={client}>
|
|
173
173
|
{() => {
|
|
174
174
|
query = useSuspenseQuery(() => ({
|
|
175
|
-
queryKey: [
|
|
175
|
+
queryKey: ['suspense-pending'],
|
|
176
176
|
queryFn: () => promise,
|
|
177
177
|
}))
|
|
178
178
|
return null
|
|
@@ -184,33 +184,33 @@ describe("useSuspenseQuery — suspense behavior", () => {
|
|
|
184
184
|
expect(query!.isPending()).toBe(true)
|
|
185
185
|
expect(query!.isSuccess()).toBe(false)
|
|
186
186
|
|
|
187
|
-
resolve(
|
|
187
|
+
resolve('loaded')
|
|
188
188
|
await promise
|
|
189
189
|
await new Promise((r) => setTimeout(r, 10))
|
|
190
190
|
|
|
191
191
|
expect(query!.isPending()).toBe(false)
|
|
192
192
|
expect(query!.isSuccess()).toBe(true)
|
|
193
|
-
expect(query!.data()).toBe(
|
|
193
|
+
expect(query!.data()).toBe('loaded')
|
|
194
194
|
unmount()
|
|
195
195
|
el.remove()
|
|
196
196
|
})
|
|
197
197
|
|
|
198
|
-
it(
|
|
198
|
+
it('QuerySuspense with multiple queries waits for all', async () => {
|
|
199
199
|
const d1 = deferred<string>()
|
|
200
200
|
const d2 = deferred<number>()
|
|
201
201
|
let childCalled = false
|
|
202
202
|
|
|
203
|
-
const el = document.createElement(
|
|
203
|
+
const el = document.createElement('div')
|
|
204
204
|
document.body.appendChild(el)
|
|
205
205
|
const unmount = mount(
|
|
206
206
|
<QueryClientProvider client={client}>
|
|
207
207
|
{() => {
|
|
208
208
|
const q1 = useSuspenseQuery(() => ({
|
|
209
|
-
queryKey: [
|
|
209
|
+
queryKey: ['multi-s1'],
|
|
210
210
|
queryFn: () => d1.promise,
|
|
211
211
|
}))
|
|
212
212
|
const q2 = useSuspenseQuery(() => ({
|
|
213
|
-
queryKey: [
|
|
213
|
+
queryKey: ['multi-s2'],
|
|
214
214
|
queryFn: () => d2.promise,
|
|
215
215
|
}))
|
|
216
216
|
return (
|
|
@@ -227,7 +227,7 @@ describe("useSuspenseQuery — suspense behavior", () => {
|
|
|
227
227
|
)
|
|
228
228
|
|
|
229
229
|
// Only first resolves — children should not render
|
|
230
|
-
d1.resolve(
|
|
230
|
+
d1.resolve('first')
|
|
231
231
|
await d1.promise
|
|
232
232
|
await new Promise((r) => setTimeout(r, 10))
|
|
233
233
|
expect(childCalled).toBe(false)
|
|
@@ -241,16 +241,16 @@ describe("useSuspenseQuery — suspense behavior", () => {
|
|
|
241
241
|
el.remove()
|
|
242
242
|
})
|
|
243
243
|
|
|
244
|
-
it(
|
|
244
|
+
it('QuerySuspense renders null fallback when not provided', async () => {
|
|
245
245
|
let query: ReturnType<typeof useSuspenseQuery<string>> | undefined
|
|
246
246
|
|
|
247
|
-
const el = document.createElement(
|
|
247
|
+
const el = document.createElement('div')
|
|
248
248
|
document.body.appendChild(el)
|
|
249
249
|
const unmount = mount(
|
|
250
250
|
<QueryClientProvider client={client}>
|
|
251
251
|
{() => {
|
|
252
252
|
query = useSuspenseQuery(() => ({
|
|
253
|
-
queryKey: [
|
|
253
|
+
queryKey: ['suspense-no-fallback'],
|
|
254
254
|
queryFn: () =>
|
|
255
255
|
new Promise(() => {
|
|
256
256
|
/* never resolves */
|
|
@@ -270,12 +270,12 @@ describe("useSuspenseQuery — suspense behavior", () => {
|
|
|
270
270
|
|
|
271
271
|
// ─── QueryClientProvider context ─────────────────────────────────────────────
|
|
272
272
|
|
|
273
|
-
describe(
|
|
274
|
-
it(
|
|
273
|
+
describe('QueryClientProvider — context behavior', () => {
|
|
274
|
+
it('useQueryClient returns the provided client', () => {
|
|
275
275
|
const client = makeClient()
|
|
276
276
|
let received: QueryClient | null = null
|
|
277
277
|
|
|
278
|
-
const el = document.createElement(
|
|
278
|
+
const el = document.createElement('div')
|
|
279
279
|
document.body.appendChild(el)
|
|
280
280
|
const unmount = mount(
|
|
281
281
|
<QueryClientProvider client={client}>
|
|
@@ -292,13 +292,13 @@ describe("QueryClientProvider — context behavior", () => {
|
|
|
292
292
|
el.remove()
|
|
293
293
|
})
|
|
294
294
|
|
|
295
|
-
it(
|
|
295
|
+
it('nested providers override outer client', () => {
|
|
296
296
|
const outerClient = makeClient()
|
|
297
297
|
const innerClient = makeClient()
|
|
298
298
|
let outerReceived: QueryClient | null = null
|
|
299
299
|
let innerReceived: QueryClient | null = null
|
|
300
300
|
|
|
301
|
-
const el = document.createElement(
|
|
301
|
+
const el = document.createElement('div')
|
|
302
302
|
document.body.appendChild(el)
|
|
303
303
|
const unmount = mount(
|
|
304
304
|
<QueryClientProvider client={outerClient}>
|
|
@@ -324,32 +324,32 @@ describe("QueryClientProvider — context behavior", () => {
|
|
|
324
324
|
el.remove()
|
|
325
325
|
})
|
|
326
326
|
|
|
327
|
-
it(
|
|
328
|
-
expect(() => useQueryClient()).toThrow(
|
|
327
|
+
it('useQueryClient throws descriptive error without provider', () => {
|
|
328
|
+
expect(() => useQueryClient()).toThrow('No QueryClient found')
|
|
329
329
|
})
|
|
330
330
|
})
|
|
331
331
|
|
|
332
332
|
// ─── useIsFetching / useIsMutating — additional ──────────────────────────────
|
|
333
333
|
|
|
334
|
-
describe(
|
|
335
|
-
it(
|
|
334
|
+
describe('useIsFetching — additional', () => {
|
|
335
|
+
it('counts multiple concurrent queries', async () => {
|
|
336
336
|
const client = makeClient()
|
|
337
337
|
const d1 = deferred<string>()
|
|
338
338
|
const d2 = deferred<string>()
|
|
339
339
|
let isFetching: (() => number) | undefined
|
|
340
340
|
|
|
341
|
-
const el = document.createElement(
|
|
341
|
+
const el = document.createElement('div')
|
|
342
342
|
document.body.appendChild(el)
|
|
343
343
|
const unmount = mount(
|
|
344
344
|
<QueryClientProvider client={client}>
|
|
345
345
|
{() => {
|
|
346
346
|
isFetching = useIsFetching()
|
|
347
347
|
useQuery(() => ({
|
|
348
|
-
queryKey: [
|
|
348
|
+
queryKey: ['concurrent-1'],
|
|
349
349
|
queryFn: () => d1.promise,
|
|
350
350
|
}))
|
|
351
351
|
useQuery(() => ({
|
|
352
|
-
queryKey: [
|
|
352
|
+
queryKey: ['concurrent-2'],
|
|
353
353
|
queryFn: () => d2.promise,
|
|
354
354
|
}))
|
|
355
355
|
return null
|
|
@@ -362,13 +362,13 @@ describe("useIsFetching — additional", () => {
|
|
|
362
362
|
// Both queries should be fetching
|
|
363
363
|
expect(isFetching!()).toBeGreaterThanOrEqual(2)
|
|
364
364
|
|
|
365
|
-
d1.resolve(
|
|
365
|
+
d1.resolve('done1')
|
|
366
366
|
await d1.promise
|
|
367
367
|
await new Promise((r) => setTimeout(r, 10))
|
|
368
368
|
// One still fetching
|
|
369
369
|
expect(isFetching!()).toBeGreaterThanOrEqual(1)
|
|
370
370
|
|
|
371
|
-
d2.resolve(
|
|
371
|
+
d2.resolve('done2')
|
|
372
372
|
await d2.promise
|
|
373
373
|
await new Promise((r) => setTimeout(r, 10))
|
|
374
374
|
expect(isFetching!()).toBe(0)
|
|
@@ -377,26 +377,26 @@ describe("useIsFetching — additional", () => {
|
|
|
377
377
|
el.remove()
|
|
378
378
|
})
|
|
379
379
|
|
|
380
|
-
it(
|
|
380
|
+
it('useIsFetching with query key filter', async () => {
|
|
381
381
|
const client = makeClient()
|
|
382
382
|
const d1 = deferred<string>()
|
|
383
383
|
const d2 = deferred<string>()
|
|
384
384
|
let allFetching: (() => number) | undefined
|
|
385
385
|
let userFetching: (() => number) | undefined
|
|
386
386
|
|
|
387
|
-
const el = document.createElement(
|
|
387
|
+
const el = document.createElement('div')
|
|
388
388
|
document.body.appendChild(el)
|
|
389
389
|
const unmount = mount(
|
|
390
390
|
<QueryClientProvider client={client}>
|
|
391
391
|
{() => {
|
|
392
392
|
allFetching = useIsFetching()
|
|
393
|
-
userFetching = useIsFetching({ queryKey: [
|
|
393
|
+
userFetching = useIsFetching({ queryKey: ['user'] })
|
|
394
394
|
useQuery(() => ({
|
|
395
|
-
queryKey: [
|
|
395
|
+
queryKey: ['user', '1'],
|
|
396
396
|
queryFn: () => d1.promise,
|
|
397
397
|
}))
|
|
398
398
|
useQuery(() => ({
|
|
399
|
-
queryKey: [
|
|
399
|
+
queryKey: ['posts'],
|
|
400
400
|
queryFn: () => d2.promise,
|
|
401
401
|
}))
|
|
402
402
|
return null
|
|
@@ -409,13 +409,13 @@ describe("useIsFetching — additional", () => {
|
|
|
409
409
|
expect(allFetching!()).toBeGreaterThanOrEqual(2)
|
|
410
410
|
expect(userFetching!()).toBe(1) // Only the user query
|
|
411
411
|
|
|
412
|
-
d1.resolve(
|
|
412
|
+
d1.resolve('user-data')
|
|
413
413
|
await d1.promise
|
|
414
414
|
await new Promise((r) => setTimeout(r, 10))
|
|
415
415
|
expect(userFetching!()).toBe(0)
|
|
416
416
|
expect(allFetching!()).toBeGreaterThanOrEqual(1) // posts still fetching
|
|
417
417
|
|
|
418
|
-
d2.resolve(
|
|
418
|
+
d2.resolve('posts-data')
|
|
419
419
|
await d2.promise
|
|
420
420
|
await new Promise((r) => setTimeout(r, 10))
|
|
421
421
|
expect(allFetching!()).toBe(0)
|
|
@@ -425,8 +425,8 @@ describe("useIsFetching — additional", () => {
|
|
|
425
425
|
})
|
|
426
426
|
})
|
|
427
427
|
|
|
428
|
-
describe(
|
|
429
|
-
it(
|
|
428
|
+
describe('useIsMutating — additional', () => {
|
|
429
|
+
it('counts multiple concurrent mutations', async () => {
|
|
430
430
|
const client = makeClient()
|
|
431
431
|
const d1 = deferred<void>()
|
|
432
432
|
const d2 = deferred<void>()
|
|
@@ -434,7 +434,7 @@ describe("useIsMutating — additional", () => {
|
|
|
434
434
|
let mut1: ReturnType<typeof useMutation<void, Error, void>> | undefined
|
|
435
435
|
let mut2: ReturnType<typeof useMutation<void, Error, void>> | undefined
|
|
436
436
|
|
|
437
|
-
const el = document.createElement(
|
|
437
|
+
const el = document.createElement('div')
|
|
438
438
|
document.body.appendChild(el)
|
|
439
439
|
const unmount = mount(
|
|
440
440
|
<QueryClientProvider client={client}>
|