@tanstack/solid-query 4.24.10 → 5.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/index.js +300 -0
- package/build/cjs/index.js.map +1 -0
- package/build/esm/index.js +285 -0
- package/build/esm/index.js.map +1 -0
- package/build/source/QueryClientProvider.jsx +21 -0
- package/build/source/__tests__/QueryClientProvider.test.jsx +120 -0
- package/build/{solid → source}/__tests__/createInfiniteQuery.test.jsx +228 -372
- package/build/{solid → source}/__tests__/createMutation.test.jsx +174 -165
- package/build/{solid → source}/__tests__/createQueries.test.jsx +86 -367
- package/build/{solid → source}/__tests__/createQuery.test.jsx +991 -943
- package/build/{solid → source}/__tests__/createQuery.types.test.jsx +35 -24
- package/build/{solid → source}/__tests__/suspense.test.jsx +177 -148
- package/build/{solid → source}/__tests__/transition.test.jsx +7 -4
- package/build/{solid → source}/__tests__/useIsFetching.test.jsx +68 -85
- package/build/{solid → source}/__tests__/useIsMutating.test.jsx +78 -93
- package/build/{solid → source}/__tests__/utils.jsx +3 -9
- package/build/source/createBaseQuery.js +147 -0
- package/build/source/createInfiniteQuery.js +8 -0
- package/build/{solid → source}/createMutation.js +7 -9
- package/build/source/createQueries.js +32 -0
- package/build/source/createQuery.js +6 -0
- package/build/{solid → source}/index.js +5 -3
- package/build/source/setBatchUpdatesFn.js +3 -0
- package/build/source/useIsFetching.js +12 -0
- package/build/source/useIsMutating.js +12 -0
- package/build/source/utils.js +7 -0
- package/build/types/QueryClientProvider.d.ts +9 -0
- package/build/{lib → types}/__tests__/utils.d.ts +3 -8
- package/build/types/createBaseQuery.d.ts +4 -0
- package/build/types/createInfiniteQuery.d.ts +3 -0
- package/build/types/createMutation.d.ts +3 -0
- package/build/{lib → types}/createQueries.d.ts +10 -8
- package/build/types/createQuery.d.ts +11 -0
- package/build/{lib → types}/index.d.ts +4 -3
- package/build/types/setBatchUpdatesFn.d.ts +1 -0
- package/build/types/types.d.ts +33 -0
- package/build/types/useIsFetching.d.ts +8 -0
- package/build/types/useIsMutating.d.ts +8 -0
- package/build/types/utils.d.ts +1 -0
- package/build/umd/index.js +2 -0
- package/build/umd/index.js.map +1 -0
- package/package.json +25 -17
- package/src/QueryClientProvider.tsx +17 -86
- package/src/__tests__/QueryClientProvider.test.tsx +37 -140
- package/src/__tests__/createInfiniteQuery.test.tsx +277 -508
- package/src/__tests__/createMutation.test.tsx +177 -225
- package/src/__tests__/createQueries.test.tsx +180 -528
- package/src/__tests__/createQuery.test.tsx +970 -1200
- package/src/__tests__/createQuery.types.test.tsx +30 -25
- package/src/__tests__/suspense.test.tsx +141 -178
- package/src/__tests__/transition.test.tsx +7 -4
- package/src/__tests__/useIsFetching.test.tsx +77 -122
- package/src/__tests__/useIsMutating.test.tsx +83 -128
- package/src/__tests__/utils.tsx +4 -11
- package/src/createBaseQuery.ts +148 -60
- package/src/createInfiniteQuery.ts +16 -94
- package/src/createMutation.ts +9 -63
- package/src/createQueries.ts +44 -55
- package/src/createQuery.ts +42 -127
- package/src/index.ts +6 -3
- package/src/setBatchUpdatesFn.ts +4 -0
- package/src/types.ts +81 -75
- package/src/useIsFetching.ts +12 -44
- package/src/useIsMutating.ts +13 -29
- package/src/utils.ts +5 -79
- package/build/lib/QueryClientProvider.d.ts +0 -24
- package/build/lib/QueryClientProvider.esm.js +0 -74
- package/build/lib/QueryClientProvider.esm.js.map +0 -1
- package/build/lib/QueryClientProvider.js +0 -80
- package/build/lib/QueryClientProvider.js.map +0 -1
- package/build/lib/QueryClientProvider.mjs +0 -74
- package/build/lib/QueryClientProvider.mjs.map +0 -1
- package/build/lib/createBaseQuery.d.ts +0 -4
- package/build/lib/createBaseQuery.esm.js +0 -93
- package/build/lib/createBaseQuery.esm.js.map +0 -1
- package/build/lib/createBaseQuery.js +0 -97
- package/build/lib/createBaseQuery.js.map +0 -1
- package/build/lib/createBaseQuery.mjs +0 -93
- package/build/lib/createBaseQuery.mjs.map +0 -1
- package/build/lib/createInfiniteQuery.d.ts +0 -5
- package/build/lib/createInfiniteQuery.esm.js +0 -20
- package/build/lib/createInfiniteQuery.esm.js.map +0 -1
- package/build/lib/createInfiniteQuery.js +0 -24
- package/build/lib/createInfiniteQuery.js.map +0 -1
- package/build/lib/createInfiniteQuery.mjs +0 -20
- package/build/lib/createInfiniteQuery.mjs.map +0 -1
- package/build/lib/createMutation.d.ts +0 -6
- package/build/lib/createMutation.esm.js +0 -45
- package/build/lib/createMutation.esm.js.map +0 -1
- package/build/lib/createMutation.js +0 -49
- package/build/lib/createMutation.js.map +0 -1
- package/build/lib/createMutation.mjs +0 -45
- package/build/lib/createMutation.mjs.map +0 -1
- package/build/lib/createQueries.esm.js +0 -54
- package/build/lib/createQueries.esm.js.map +0 -1
- package/build/lib/createQueries.js +0 -58
- package/build/lib/createQueries.js.map +0 -1
- package/build/lib/createQueries.mjs +0 -54
- package/build/lib/createQueries.mjs.map +0 -1
- package/build/lib/createQuery.d.ts +0 -23
- package/build/lib/createQuery.esm.js +0 -25
- package/build/lib/createQuery.esm.js.map +0 -1
- package/build/lib/createQuery.js +0 -29
- package/build/lib/createQuery.js.map +0 -1
- package/build/lib/createQuery.mjs +0 -25
- package/build/lib/createQuery.mjs.map +0 -1
- package/build/lib/index.esm.js +0 -9
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.js +0 -31
- package/build/lib/index.js.map +0 -1
- package/build/lib/index.mjs +0 -9
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/types.d.ts +0 -47
- package/build/lib/useIsFetching.d.ts +0 -7
- package/build/lib/useIsFetching.esm.js +0 -29
- package/build/lib/useIsFetching.esm.js.map +0 -1
- package/build/lib/useIsFetching.js +0 -33
- package/build/lib/useIsFetching.js.map +0 -1
- package/build/lib/useIsFetching.mjs +0 -29
- package/build/lib/useIsFetching.mjs.map +0 -1
- package/build/lib/useIsMutating.d.ts +0 -8
- package/build/lib/useIsMutating.esm.js +0 -22
- package/build/lib/useIsMutating.esm.js.map +0 -1
- package/build/lib/useIsMutating.js +0 -26
- package/build/lib/useIsMutating.js.map +0 -1
- package/build/lib/useIsMutating.mjs +0 -22
- package/build/lib/useIsMutating.mjs.map +0 -1
- package/build/lib/utils.d.ts +0 -14
- package/build/lib/utils.esm.js +0 -63
- package/build/lib/utils.esm.js.map +0 -1
- package/build/lib/utils.js +0 -72
- package/build/lib/utils.js.map +0 -1
- package/build/lib/utils.mjs +0 -63
- package/build/lib/utils.mjs.map +0 -1
- package/build/solid/QueryClientProvider.jsx +0 -49
- package/build/solid/__tests__/QueryClientProvider.test.jsx +0 -185
- package/build/solid/createBaseQuery.js +0 -81
- package/build/solid/createInfiniteQuery.js +0 -16
- package/build/solid/createQueries.js +0 -39
- package/build/solid/createQuery.js +0 -16
- package/build/solid/useIsFetching.js +0 -23
- package/build/solid/useIsMutating.js +0 -16
- package/build/solid/utils.js +0 -45
- package/build/umd/index.development.js +0 -3577
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -2
- package/build/umd/index.production.js.map +0 -1
- /package/build/{solid → source}/types.js +0 -0
- /package/build/{lib → types}/__tests__/QueryClientProvider.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createInfiniteQuery.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createMutation.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQueries.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQuery.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/createQuery.types.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/suspense.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/transition.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/useIsFetching.test.d.ts +0 -0
- /package/build/{lib → types}/__tests__/useIsMutating.test.d.ts +0 -0
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import '@testing-library/jest-dom'
|
|
2
2
|
import {
|
|
3
|
-
createContext,
|
|
4
3
|
createEffect,
|
|
5
4
|
createRenderEffect,
|
|
6
5
|
createSignal,
|
|
7
6
|
ErrorBoundary,
|
|
8
7
|
} from 'solid-js'
|
|
9
8
|
import { fireEvent, render, screen, waitFor } from 'solid-testing-library'
|
|
10
|
-
import type { QueryClient } from '..'
|
|
11
9
|
import {
|
|
12
10
|
createMutation,
|
|
13
11
|
MutationCache,
|
|
@@ -23,14 +21,16 @@ import {
|
|
|
23
21
|
sleep,
|
|
24
22
|
} from './utils'
|
|
25
23
|
|
|
26
|
-
describe('
|
|
24
|
+
describe('createMutation', () => {
|
|
27
25
|
const queryCache = new QueryCache()
|
|
28
26
|
const mutationCache = new MutationCache()
|
|
29
27
|
const queryClient = createQueryClient({ queryCache, mutationCache })
|
|
30
28
|
|
|
31
29
|
it('should be able to reset `data`', async () => {
|
|
32
30
|
function Page() {
|
|
33
|
-
const mutation = createMutation(() =>
|
|
31
|
+
const mutation = createMutation(() => ({
|
|
32
|
+
mutationFn: () => Promise.resolve('mutation'),
|
|
33
|
+
}))
|
|
34
34
|
|
|
35
35
|
return (
|
|
36
36
|
<div>
|
|
@@ -64,11 +64,13 @@ describe('useMutation', () => {
|
|
|
64
64
|
|
|
65
65
|
it('should be able to reset `error`', async () => {
|
|
66
66
|
function Page() {
|
|
67
|
-
const mutation = createMutation<string, Error>(() => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
const mutation = createMutation<string, Error>(() => ({
|
|
68
|
+
mutationFn: () => {
|
|
69
|
+
const err = new Error('Expected mock error. All is well!')
|
|
70
|
+
err.stack = ''
|
|
71
|
+
return Promise.reject(err)
|
|
72
|
+
},
|
|
73
|
+
}))
|
|
72
74
|
|
|
73
75
|
return (
|
|
74
76
|
<div>
|
|
@@ -110,17 +112,15 @@ describe('useMutation', () => {
|
|
|
110
112
|
const onSettledMock = jest.fn()
|
|
111
113
|
|
|
112
114
|
function Page() {
|
|
113
|
-
const mutation = createMutation(
|
|
114
|
-
(vars: { count: number }) => Promise.resolve(vars.count),
|
|
115
|
-
{
|
|
116
|
-
|
|
117
|
-
onSuccessMock(data)
|
|
118
|
-
},
|
|
119
|
-
onSettled: (data) => {
|
|
120
|
-
onSettledMock(data)
|
|
121
|
-
},
|
|
115
|
+
const mutation = createMutation(() => ({
|
|
116
|
+
mutationFn: (vars: { count: number }) => Promise.resolve(vars.count),
|
|
117
|
+
onSuccess: (data) => {
|
|
118
|
+
onSuccessMock(data)
|
|
122
119
|
},
|
|
123
|
-
|
|
120
|
+
onSettled: (data) => {
|
|
121
|
+
onSettledMock(data)
|
|
122
|
+
},
|
|
123
|
+
}))
|
|
124
124
|
|
|
125
125
|
return (
|
|
126
126
|
<div>
|
|
@@ -177,7 +177,7 @@ describe('useMutation', () => {
|
|
|
177
177
|
const mutateFn = jest.fn<Promise<Value>, [value: Value]>()
|
|
178
178
|
|
|
179
179
|
mutateFn.mockImplementationOnce(() => {
|
|
180
|
-
return Promise.reject('Error test Jonas')
|
|
180
|
+
return Promise.reject(new Error('Error test Jonas'))
|
|
181
181
|
})
|
|
182
182
|
|
|
183
183
|
mutateFn.mockImplementation(async (value) => {
|
|
@@ -186,14 +186,16 @@ describe('useMutation', () => {
|
|
|
186
186
|
})
|
|
187
187
|
|
|
188
188
|
function Page() {
|
|
189
|
-
const mutation = createMutation
|
|
189
|
+
const mutation = createMutation(() => ({
|
|
190
|
+
mutationFn: mutateFn,
|
|
191
|
+
}))
|
|
190
192
|
|
|
191
193
|
return (
|
|
192
194
|
<div>
|
|
193
195
|
<h1>Data {mutation.data?.count}</h1>
|
|
194
196
|
<h2>Status {mutation.status}</h2>
|
|
195
197
|
<h2>Failed {mutation.failureCount} times</h2>
|
|
196
|
-
<h2>Failed because {mutation.failureReason ?? 'null'}</h2>
|
|
198
|
+
<h2>Failed because {mutation.failureReason?.message ?? 'null'}</h2>
|
|
197
199
|
<button
|
|
198
200
|
onClick={() => {
|
|
199
201
|
setCount((c) => c + 1)
|
|
@@ -221,7 +223,7 @@ describe('useMutation', () => {
|
|
|
221
223
|
await waitFor(() => screen.getByText('Failed because Error test Jonas'))
|
|
222
224
|
|
|
223
225
|
fireEvent.click(screen.getByRole('button', { name: /mutate/i }))
|
|
224
|
-
await waitFor(() => screen.getByText('Status
|
|
226
|
+
await waitFor(() => screen.getByText('Status pending'))
|
|
225
227
|
await waitFor(() => screen.getByText('Status success'))
|
|
226
228
|
await waitFor(() => screen.getByText('Data 2'))
|
|
227
229
|
await waitFor(() => screen.getByText('Failed 0 times'))
|
|
@@ -234,23 +236,21 @@ describe('useMutation', () => {
|
|
|
234
236
|
const [count, setCount] = createSignal(0)
|
|
235
237
|
|
|
236
238
|
function Page() {
|
|
237
|
-
const mutation = createMutation(
|
|
238
|
-
(vars: { count: number }) => {
|
|
239
|
+
const mutation = createMutation(() => ({
|
|
240
|
+
mutationFn: (vars: { count: number }) => {
|
|
239
241
|
const error = new Error(
|
|
240
242
|
`Expected mock error. All is well! ${vars.count}`,
|
|
241
243
|
)
|
|
242
244
|
error.stack = ''
|
|
243
245
|
return Promise.reject(error)
|
|
244
246
|
},
|
|
245
|
-
{
|
|
246
|
-
|
|
247
|
-
onErrorMock(error.message)
|
|
248
|
-
},
|
|
249
|
-
onSettled: (_data, error) => {
|
|
250
|
-
onSettledMock(error?.message)
|
|
251
|
-
},
|
|
247
|
+
onError: (error: Error) => {
|
|
248
|
+
onErrorMock(error.message)
|
|
252
249
|
},
|
|
253
|
-
|
|
250
|
+
onSettled: (_data, error) => {
|
|
251
|
+
onSettledMock(error?.message)
|
|
252
|
+
},
|
|
253
|
+
}))
|
|
254
254
|
|
|
255
255
|
return (
|
|
256
256
|
<div>
|
|
@@ -314,14 +314,15 @@ describe('useMutation', () => {
|
|
|
314
314
|
const callbacks: string[] = []
|
|
315
315
|
|
|
316
316
|
function Page() {
|
|
317
|
-
const mutation = createMutation(
|
|
317
|
+
const mutation = createMutation(() => ({
|
|
318
|
+
mutationFn: async (text: string) => text,
|
|
318
319
|
onSuccess: async () => {
|
|
319
320
|
callbacks.push('useMutation.onSuccess')
|
|
320
321
|
},
|
|
321
322
|
onSettled: async () => {
|
|
322
323
|
callbacks.push('useMutation.onSettled')
|
|
323
324
|
},
|
|
324
|
-
})
|
|
325
|
+
}))
|
|
325
326
|
|
|
326
327
|
createEffect(() => {
|
|
327
328
|
const { mutateAsync } = mutation
|
|
@@ -364,17 +365,16 @@ describe('useMutation', () => {
|
|
|
364
365
|
const callbacks: string[] = []
|
|
365
366
|
|
|
366
367
|
function Page() {
|
|
367
|
-
const mutation = createMutation(
|
|
368
|
-
async (_text: string) => Promise.reject('oops'),
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
},
|
|
373
|
-
onSettled: async () => {
|
|
374
|
-
callbacks.push('useMutation.onSettled')
|
|
375
|
-
},
|
|
368
|
+
const mutation = createMutation(() => ({
|
|
369
|
+
mutationFn: async (_text: string) => Promise.reject(new Error('oops')),
|
|
370
|
+
|
|
371
|
+
onError: async () => {
|
|
372
|
+
callbacks.push('useMutation.onError')
|
|
376
373
|
},
|
|
377
|
-
|
|
374
|
+
onSettled: async () => {
|
|
375
|
+
callbacks.push('useMutation.onSettled')
|
|
376
|
+
},
|
|
377
|
+
}))
|
|
378
378
|
|
|
379
379
|
createEffect(() => {
|
|
380
380
|
const { mutateAsync } = mutation
|
|
@@ -389,7 +389,7 @@ describe('useMutation', () => {
|
|
|
389
389
|
},
|
|
390
390
|
})
|
|
391
391
|
} catch (error) {
|
|
392
|
-
callbacks.push(`mutateAsync.error:${error}`)
|
|
392
|
+
callbacks.push(`mutateAsync.error:${(error as Error).message}`)
|
|
393
393
|
}
|
|
394
394
|
}, 10)
|
|
395
395
|
})
|
|
@@ -417,7 +417,7 @@ describe('useMutation', () => {
|
|
|
417
417
|
it('should be able to use mutation defaults', async () => {
|
|
418
418
|
const key = queryKey()
|
|
419
419
|
|
|
420
|
-
queryClient.setMutationDefaults(key
|
|
420
|
+
queryClient.setMutationDefaults(key, {
|
|
421
421
|
mutationFn: async (text: string) => {
|
|
422
422
|
await sleep(10)
|
|
423
423
|
return text
|
|
@@ -427,7 +427,9 @@ describe('useMutation', () => {
|
|
|
427
427
|
const states: CreateMutationResult<any, any, any, any>[] = []
|
|
428
428
|
|
|
429
429
|
function Page() {
|
|
430
|
-
const mutation = createMutation<string, unknown, string>(
|
|
430
|
+
const mutation = createMutation<string, unknown, string>(() => ({
|
|
431
|
+
mutationKey: key,
|
|
432
|
+
}))
|
|
431
433
|
|
|
432
434
|
createRenderEffect(() => {
|
|
433
435
|
states.push({ ...mutation })
|
|
@@ -452,25 +454,23 @@ describe('useMutation', () => {
|
|
|
452
454
|
await sleep(100)
|
|
453
455
|
|
|
454
456
|
expect(states.length).toBe(3)
|
|
455
|
-
expect(states[0]).toMatchObject({ data: undefined,
|
|
456
|
-
expect(states[1]).toMatchObject({ data: undefined,
|
|
457
|
-
expect(states[2]).toMatchObject({ data: 'todo',
|
|
457
|
+
expect(states[0]).toMatchObject({ data: undefined, isPending: false })
|
|
458
|
+
expect(states[1]).toMatchObject({ data: undefined, isPending: true })
|
|
459
|
+
expect(states[2]).toMatchObject({ data: 'todo', isPending: false })
|
|
458
460
|
})
|
|
459
461
|
|
|
460
462
|
it('should be able to retry a failed mutation', async () => {
|
|
461
463
|
let count = 0
|
|
462
464
|
|
|
463
465
|
function Page() {
|
|
464
|
-
const mutation = createMutation(
|
|
465
|
-
(_text: string) => {
|
|
466
|
+
const mutation = createMutation(() => ({
|
|
467
|
+
mutationFn: (_text: string) => {
|
|
466
468
|
count++
|
|
467
|
-
return Promise.reject('oops')
|
|
468
|
-
},
|
|
469
|
-
{
|
|
470
|
-
retry: 1,
|
|
471
|
-
retryDelay: 5,
|
|
469
|
+
return Promise.reject(new Error('oops'))
|
|
472
470
|
},
|
|
473
|
-
|
|
471
|
+
retry: 1,
|
|
472
|
+
retryDelay: 5,
|
|
473
|
+
}))
|
|
474
474
|
|
|
475
475
|
createEffect(() => {
|
|
476
476
|
const { mutate } = mutation
|
|
@@ -499,16 +499,14 @@ describe('useMutation', () => {
|
|
|
499
499
|
let count = 0
|
|
500
500
|
|
|
501
501
|
function Page() {
|
|
502
|
-
const mutation = createMutation(
|
|
503
|
-
(_text: string) => {
|
|
502
|
+
const mutation = createMutation(() => ({
|
|
503
|
+
mutationFn: (_text: string) => {
|
|
504
504
|
count++
|
|
505
505
|
return Promise.reject(new Error('oops'))
|
|
506
506
|
},
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
},
|
|
511
|
-
)
|
|
507
|
+
retry: 1,
|
|
508
|
+
retryDelay: 5,
|
|
509
|
+
}))
|
|
512
510
|
|
|
513
511
|
return (
|
|
514
512
|
<div>
|
|
@@ -540,7 +538,7 @@ describe('useMutation', () => {
|
|
|
540
538
|
|
|
541
539
|
await waitFor(() => {
|
|
542
540
|
expect(
|
|
543
|
-
screen.getByText('error: null, status:
|
|
541
|
+
screen.getByText('error: null, status: pending, isPaused: true'),
|
|
544
542
|
).toBeInTheDocument()
|
|
545
543
|
})
|
|
546
544
|
|
|
@@ -568,16 +566,14 @@ describe('useMutation', () => {
|
|
|
568
566
|
let count = 0
|
|
569
567
|
|
|
570
568
|
function Page() {
|
|
571
|
-
const mutation = createMutation(
|
|
572
|
-
async (_text: string) => {
|
|
569
|
+
const mutation = createMutation(() => ({
|
|
570
|
+
mutationFn: async (_text: string) => {
|
|
573
571
|
count++
|
|
574
572
|
await sleep(10)
|
|
575
573
|
return count
|
|
576
574
|
},
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
},
|
|
580
|
-
)
|
|
575
|
+
onMutate,
|
|
576
|
+
}))
|
|
581
577
|
|
|
582
578
|
return (
|
|
583
579
|
<div>
|
|
@@ -600,7 +596,7 @@ describe('useMutation', () => {
|
|
|
600
596
|
|
|
601
597
|
fireEvent.click(screen.getByRole('button', { name: /mutate/i }))
|
|
602
598
|
|
|
603
|
-
await screen.findByText('data: null, status:
|
|
599
|
+
await screen.findByText('data: null, status: pending, isPaused: true')
|
|
604
600
|
|
|
605
601
|
expect(onMutate).toHaveBeenCalledTimes(1)
|
|
606
602
|
expect(onMutate).toHaveBeenCalledWith('todo')
|
|
@@ -622,11 +618,13 @@ describe('useMutation', () => {
|
|
|
622
618
|
const states: Array<string> = []
|
|
623
619
|
|
|
624
620
|
function Page() {
|
|
625
|
-
const mutation = createMutation(
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
621
|
+
const mutation = createMutation(() => ({
|
|
622
|
+
mutationFn: async (_text: string) => {
|
|
623
|
+
count++
|
|
624
|
+
await sleep(10)
|
|
625
|
+
return count
|
|
626
|
+
},
|
|
627
|
+
}))
|
|
630
628
|
|
|
631
629
|
createRenderEffect(() => {
|
|
632
630
|
states.push(`${mutation.status}, ${mutation.isPaused}`)
|
|
@@ -653,11 +651,11 @@ describe('useMutation', () => {
|
|
|
653
651
|
|
|
654
652
|
fireEvent.click(screen.getByRole('button', { name: /mutate/i }))
|
|
655
653
|
|
|
656
|
-
await screen.findByText('data: null, status:
|
|
654
|
+
await screen.findByText('data: null, status: pending, isPaused: true')
|
|
657
655
|
|
|
658
|
-
// no intermediate '
|
|
656
|
+
// no intermediate 'pending, false' state is expected because we don't start mutating!
|
|
659
657
|
expect(states[0]).toBe('idle, false')
|
|
660
|
-
expect(states[1]).toBe('
|
|
658
|
+
expect(states[1]).toBe('pending, true')
|
|
661
659
|
|
|
662
660
|
onlineMock.mockReturnValue(true)
|
|
663
661
|
window.dispatchEvent(new Event('online'))
|
|
@@ -674,18 +672,18 @@ describe('useMutation', () => {
|
|
|
674
672
|
const states: CreateMutationResult<any, any, any, any>[] = []
|
|
675
673
|
|
|
676
674
|
function Page() {
|
|
677
|
-
const mutation = createMutation(
|
|
678
|
-
async (_text: string) => {
|
|
675
|
+
const mutation = createMutation(() => ({
|
|
676
|
+
mutationFn: async (_text: string) => {
|
|
679
677
|
await sleep(1)
|
|
680
678
|
count++
|
|
681
|
-
return count > 1
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
retry: 1,
|
|
685
|
-
retryDelay: 5,
|
|
686
|
-
networkMode: 'offlineFirst',
|
|
679
|
+
return count > 1
|
|
680
|
+
? Promise.resolve('data')
|
|
681
|
+
: Promise.reject(new Error('oops'))
|
|
687
682
|
},
|
|
688
|
-
|
|
683
|
+
retry: 1,
|
|
684
|
+
retryDelay: 5,
|
|
685
|
+
networkMode: 'offlineFirst',
|
|
686
|
+
}))
|
|
689
687
|
|
|
690
688
|
createRenderEffect(() => {
|
|
691
689
|
states.push({ ...mutation })
|
|
@@ -711,28 +709,28 @@ describe('useMutation', () => {
|
|
|
711
709
|
|
|
712
710
|
expect(states.length).toBe(4)
|
|
713
711
|
expect(states[0]).toMatchObject({
|
|
714
|
-
|
|
712
|
+
isPending: false,
|
|
715
713
|
isPaused: false,
|
|
716
714
|
failureCount: 0,
|
|
717
715
|
failureReason: null,
|
|
718
716
|
})
|
|
719
717
|
expect(states[1]).toMatchObject({
|
|
720
|
-
|
|
718
|
+
isPending: true,
|
|
721
719
|
isPaused: false,
|
|
722
720
|
failureCount: 0,
|
|
723
721
|
failureReason: null,
|
|
724
722
|
})
|
|
725
723
|
expect(states[2]).toMatchObject({
|
|
726
|
-
|
|
724
|
+
isPending: true,
|
|
727
725
|
isPaused: false,
|
|
728
726
|
failureCount: 1,
|
|
729
|
-
failureReason: 'oops',
|
|
727
|
+
failureReason: new Error('oops'),
|
|
730
728
|
})
|
|
731
729
|
expect(states[3]).toMatchObject({
|
|
732
|
-
|
|
730
|
+
isPending: true,
|
|
733
731
|
isPaused: true,
|
|
734
732
|
failureCount: 1,
|
|
735
|
-
failureReason: 'oops',
|
|
733
|
+
failureReason: new Error('oops'),
|
|
736
734
|
})
|
|
737
735
|
|
|
738
736
|
onlineMock.mockReturnValue(true)
|
|
@@ -742,13 +740,13 @@ describe('useMutation', () => {
|
|
|
742
740
|
|
|
743
741
|
expect(states.length).toBe(6)
|
|
744
742
|
expect(states[4]).toMatchObject({
|
|
745
|
-
|
|
743
|
+
isPending: true,
|
|
746
744
|
isPaused: false,
|
|
747
745
|
failureCount: 1,
|
|
748
|
-
failureReason: 'oops',
|
|
746
|
+
failureReason: new Error('oops'),
|
|
749
747
|
})
|
|
750
748
|
expect(states[5]).toMatchObject({
|
|
751
|
-
|
|
749
|
+
isPending: false,
|
|
752
750
|
isPaused: false,
|
|
753
751
|
failureCount: 0,
|
|
754
752
|
failureReason: null,
|
|
@@ -760,7 +758,7 @@ describe('useMutation', () => {
|
|
|
760
758
|
|
|
761
759
|
it('should not change state if unmounted', async () => {
|
|
762
760
|
function Mutates() {
|
|
763
|
-
const mutation = createMutation(() => sleep(10))
|
|
761
|
+
const mutation = createMutation(() => ({ mutationFn: () => sleep(10) }))
|
|
764
762
|
return <button onClick={() => mutation.mutate()}>mutate</button>
|
|
765
763
|
}
|
|
766
764
|
function Page() {
|
|
@@ -782,16 +780,16 @@ describe('useMutation', () => {
|
|
|
782
780
|
fireEvent.click(screen.getByText('unmount'))
|
|
783
781
|
})
|
|
784
782
|
|
|
785
|
-
it('should be able to throw an error when
|
|
783
|
+
it('should be able to throw an error when throwErrors is set to true', async () => {
|
|
786
784
|
function Page() {
|
|
787
|
-
const mutation = createMutation<string, Error>(
|
|
788
|
-
() => {
|
|
785
|
+
const mutation = createMutation<string, Error>(() => ({
|
|
786
|
+
mutationFn: () => {
|
|
789
787
|
const err = new Error('Expected mock error. All is well!')
|
|
790
788
|
err.stack = ''
|
|
791
789
|
return Promise.reject(err)
|
|
792
790
|
},
|
|
793
|
-
|
|
794
|
-
)
|
|
791
|
+
throwErrors: true,
|
|
792
|
+
}))
|
|
795
793
|
|
|
796
794
|
return (
|
|
797
795
|
<div>
|
|
@@ -821,22 +819,20 @@ describe('useMutation', () => {
|
|
|
821
819
|
})
|
|
822
820
|
})
|
|
823
821
|
|
|
824
|
-
it('should be able to throw an error when
|
|
822
|
+
it('should be able to throw an error when throwErrors is a function that returns true', async () => {
|
|
825
823
|
let boundary = false
|
|
826
824
|
function Page() {
|
|
827
|
-
const mutation = createMutation<string, Error>(
|
|
828
|
-
() => {
|
|
825
|
+
const mutation = createMutation<string, Error>(() => ({
|
|
826
|
+
mutationFn: () => {
|
|
829
827
|
const err = new Error('mock error')
|
|
830
828
|
err.stack = ''
|
|
831
829
|
return Promise.reject(err)
|
|
832
830
|
},
|
|
833
|
-
{
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
return !boundary
|
|
837
|
-
},
|
|
831
|
+
throwErrors: () => {
|
|
832
|
+
boundary = !boundary
|
|
833
|
+
return !boundary
|
|
838
834
|
},
|
|
839
|
-
)
|
|
835
|
+
}))
|
|
840
836
|
|
|
841
837
|
return (
|
|
842
838
|
<div>
|
|
@@ -892,17 +888,16 @@ describe('useMutation', () => {
|
|
|
892
888
|
const metaErrorMessage = 'mutation failed'
|
|
893
889
|
|
|
894
890
|
function Page() {
|
|
895
|
-
const mutationSucceed = createMutation(
|
|
891
|
+
const mutationSucceed = createMutation(() => ({
|
|
892
|
+
mutationFn: async () => '',
|
|
896
893
|
meta: { metaSuccessMessage },
|
|
897
|
-
})
|
|
898
|
-
const mutationError = createMutation(
|
|
899
|
-
async () => {
|
|
894
|
+
}))
|
|
895
|
+
const mutationError = createMutation(() => ({
|
|
896
|
+
mutationFn: async () => {
|
|
900
897
|
throw new Error('')
|
|
901
898
|
},
|
|
902
|
-
{
|
|
903
|
-
|
|
904
|
-
},
|
|
905
|
-
)
|
|
899
|
+
meta: { metaErrorMessage },
|
|
900
|
+
}))
|
|
906
901
|
|
|
907
902
|
return (
|
|
908
903
|
<div>
|
|
@@ -953,19 +948,17 @@ describe('useMutation', () => {
|
|
|
953
948
|
}
|
|
954
949
|
|
|
955
950
|
function Component() {
|
|
956
|
-
const mutation = createMutation(
|
|
957
|
-
async (_text: string) => {
|
|
951
|
+
const mutation = createMutation(() => ({
|
|
952
|
+
mutationFn: async (_text: string) => {
|
|
958
953
|
count++
|
|
959
954
|
await sleep(10)
|
|
960
955
|
return count
|
|
961
956
|
},
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
},
|
|
968
|
-
)
|
|
957
|
+
mutationKey: mutationKey,
|
|
958
|
+
gcTime: 0,
|
|
959
|
+
onSuccess,
|
|
960
|
+
onSettled,
|
|
961
|
+
}))
|
|
969
962
|
|
|
970
963
|
return (
|
|
971
964
|
<div>
|
|
@@ -1000,7 +993,7 @@ describe('useMutation', () => {
|
|
|
1000
993
|
|
|
1001
994
|
await waitFor(() => {
|
|
1002
995
|
expect(
|
|
1003
|
-
queryClient.getMutationCache().findAll({ mutationKey: mutationKey
|
|
996
|
+
queryClient.getMutationCache().findAll({ mutationKey: mutationKey }),
|
|
1004
997
|
).toHaveLength(0)
|
|
1005
998
|
})
|
|
1006
999
|
|
|
@@ -1012,71 +1005,6 @@ describe('useMutation', () => {
|
|
|
1012
1005
|
expect(onSettledMutate).toHaveBeenCalledTimes(0)
|
|
1013
1006
|
})
|
|
1014
1007
|
|
|
1015
|
-
describe('with custom context', () => {
|
|
1016
|
-
it('should be able to reset `data`', async () => {
|
|
1017
|
-
const context = createContext<QueryClient | undefined>(undefined)
|
|
1018
|
-
|
|
1019
|
-
function Page() {
|
|
1020
|
-
const mutation = createMutation(() => Promise.resolve('mutation'), {
|
|
1021
|
-
context,
|
|
1022
|
-
})
|
|
1023
|
-
|
|
1024
|
-
return (
|
|
1025
|
-
<div>
|
|
1026
|
-
<h1>{mutation.data ?? 'empty'}</h1>
|
|
1027
|
-
<button onClick={() => mutation.reset()}>reset</button>
|
|
1028
|
-
<button onClick={() => mutation.mutate()}>mutate</button>
|
|
1029
|
-
</div>
|
|
1030
|
-
)
|
|
1031
|
-
}
|
|
1032
|
-
|
|
1033
|
-
render(() => (
|
|
1034
|
-
<QueryClientProvider client={queryClient} context={context}>
|
|
1035
|
-
<Page />
|
|
1036
|
-
</QueryClientProvider>
|
|
1037
|
-
))
|
|
1038
|
-
|
|
1039
|
-
expect(screen.getByRole('heading').textContent).toBe('empty')
|
|
1040
|
-
|
|
1041
|
-
fireEvent.click(screen.getByRole('button', { name: /mutate/i }))
|
|
1042
|
-
|
|
1043
|
-
await waitFor(() => {
|
|
1044
|
-
expect(screen.getByRole('heading').textContent).toBe('mutation')
|
|
1045
|
-
})
|
|
1046
|
-
|
|
1047
|
-
fireEvent.click(screen.getByRole('button', { name: /reset/i }))
|
|
1048
|
-
|
|
1049
|
-
await waitFor(() => {
|
|
1050
|
-
expect(screen.getByRole('heading').textContent).toBe('empty')
|
|
1051
|
-
})
|
|
1052
|
-
})
|
|
1053
|
-
|
|
1054
|
-
it('should throw if the context is not passed to useMutation', async () => {
|
|
1055
|
-
const context = createContext<QueryClient | undefined>(undefined)
|
|
1056
|
-
|
|
1057
|
-
function Page() {
|
|
1058
|
-
const { data = '' } = createMutation(() => Promise.resolve('mutation'))
|
|
1059
|
-
|
|
1060
|
-
return (
|
|
1061
|
-
<div>
|
|
1062
|
-
<h1 data-testid="title">{data}</h1>
|
|
1063
|
-
</div>
|
|
1064
|
-
)
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
render(() => (
|
|
1068
|
-
<QueryClientProvider client={queryClient} context={context}>
|
|
1069
|
-
<ErrorBoundary fallback={() => <div>error boundary</div>}>
|
|
1070
|
-
<Page />
|
|
1071
|
-
</ErrorBoundary>
|
|
1072
|
-
,
|
|
1073
|
-
</QueryClientProvider>
|
|
1074
|
-
))
|
|
1075
|
-
|
|
1076
|
-
await waitFor(() => screen.getByText('error boundary'))
|
|
1077
|
-
})
|
|
1078
|
-
})
|
|
1079
|
-
|
|
1080
1008
|
it('should call mutate callbacks only for the last observer', async () => {
|
|
1081
1009
|
const onSuccess = jest.fn()
|
|
1082
1010
|
const onSuccessMutate = jest.fn()
|
|
@@ -1085,17 +1013,15 @@ describe('useMutation', () => {
|
|
|
1085
1013
|
let count = 0
|
|
1086
1014
|
|
|
1087
1015
|
function Page() {
|
|
1088
|
-
const mutation = createMutation(
|
|
1089
|
-
async (_text: string) => {
|
|
1016
|
+
const mutation = createMutation(() => ({
|
|
1017
|
+
mutationFn: async (_text: string) => {
|
|
1090
1018
|
count++
|
|
1091
1019
|
await sleep(10)
|
|
1092
1020
|
return `result${count}`
|
|
1093
1021
|
},
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
},
|
|
1098
|
-
)
|
|
1022
|
+
onSuccess,
|
|
1023
|
+
onSettled,
|
|
1024
|
+
}))
|
|
1099
1025
|
|
|
1100
1026
|
return (
|
|
1101
1027
|
<div>
|
|
@@ -1149,16 +1075,14 @@ describe('useMutation', () => {
|
|
|
1149
1075
|
const onError = jest.fn()
|
|
1150
1076
|
|
|
1151
1077
|
function Page() {
|
|
1152
|
-
const mutation = createMutation(
|
|
1153
|
-
async (_text: string) => {
|
|
1078
|
+
const mutation = createMutation(() => ({
|
|
1079
|
+
mutationFn: async (_text: string) => {
|
|
1154
1080
|
await sleep(10)
|
|
1155
1081
|
return 'result'
|
|
1156
1082
|
},
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
},
|
|
1161
|
-
)
|
|
1083
|
+
onSuccess: () => Promise.reject(error),
|
|
1084
|
+
onError,
|
|
1085
|
+
}))
|
|
1162
1086
|
|
|
1163
1087
|
return (
|
|
1164
1088
|
<div>
|
|
@@ -1188,15 +1112,13 @@ describe('useMutation', () => {
|
|
|
1188
1112
|
const mutateFnError = new Error('mutateFnError')
|
|
1189
1113
|
|
|
1190
1114
|
function Page() {
|
|
1191
|
-
const mutation = createMutation(
|
|
1192
|
-
async (_text: string) => {
|
|
1115
|
+
const mutation = createMutation(() => ({
|
|
1116
|
+
mutationFn: async (_text: string) => {
|
|
1193
1117
|
await sleep(10)
|
|
1194
1118
|
throw mutateFnError
|
|
1195
1119
|
},
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
},
|
|
1199
|
-
)
|
|
1120
|
+
onError: () => Promise.reject(error),
|
|
1121
|
+
}))
|
|
1200
1122
|
|
|
1201
1123
|
return (
|
|
1202
1124
|
<div>
|
|
@@ -1229,16 +1151,14 @@ describe('useMutation', () => {
|
|
|
1229
1151
|
const onError = jest.fn()
|
|
1230
1152
|
|
|
1231
1153
|
function Page() {
|
|
1232
|
-
const mutation = createMutation(
|
|
1233
|
-
async (_text: string) => {
|
|
1154
|
+
const mutation = createMutation(() => ({
|
|
1155
|
+
mutationFn: async (_text: string) => {
|
|
1234
1156
|
await sleep(10)
|
|
1235
1157
|
throw mutateFnError
|
|
1236
1158
|
},
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
},
|
|
1241
|
-
)
|
|
1159
|
+
onSettled: () => Promise.reject(error),
|
|
1160
|
+
onError,
|
|
1161
|
+
}))
|
|
1242
1162
|
|
|
1243
1163
|
return (
|
|
1244
1164
|
<div>
|
|
@@ -1266,4 +1186,36 @@ describe('useMutation', () => {
|
|
|
1266
1186
|
|
|
1267
1187
|
expect(onError).toHaveBeenCalledWith(mutateFnError, 'todo', undefined)
|
|
1268
1188
|
})
|
|
1189
|
+
|
|
1190
|
+
it('should use provided custom queryClient', async () => {
|
|
1191
|
+
function Page() {
|
|
1192
|
+
const mutation = createMutation(
|
|
1193
|
+
() => ({
|
|
1194
|
+
mutationFn: async (text: string) => {
|
|
1195
|
+
return Promise.resolve(text)
|
|
1196
|
+
},
|
|
1197
|
+
}),
|
|
1198
|
+
() => queryClient,
|
|
1199
|
+
)
|
|
1200
|
+
|
|
1201
|
+
return (
|
|
1202
|
+
<div>
|
|
1203
|
+
<button onClick={() => mutation.mutate('custom client')}>
|
|
1204
|
+
mutate
|
|
1205
|
+
</button>
|
|
1206
|
+
<div>
|
|
1207
|
+
data: {mutation.data ?? 'null'}, status: {mutation.status}
|
|
1208
|
+
</div>
|
|
1209
|
+
</div>
|
|
1210
|
+
)
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
render(() => <Page></Page>)
|
|
1214
|
+
|
|
1215
|
+
await screen.findByText('data: null, status: idle')
|
|
1216
|
+
|
|
1217
|
+
fireEvent.click(screen.getByRole('button', { name: /mutate/i }))
|
|
1218
|
+
|
|
1219
|
+
await screen.findByText('data: custom client, status: success')
|
|
1220
|
+
})
|
|
1269
1221
|
})
|