Skip to content

Commit 1c5a820

Browse files
committed
test(unit): update to handle context in mutation callbacks
1 parent b835d3a commit 1c5a820

File tree

5 files changed

+92
-11
lines changed

5 files changed

+92
-11
lines changed

packages/query-core/src/__tests__/hydration.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ describe('dehydration and rehydration', () => {
545545
{ id: 2, text: 'text' },
546546
{ text: 'text' },
547547
{ optimisticTodo: { id: 1, text: 'text' } },
548+
{ client: client, meta: undefined, mutationKey: ['addTodo'] },
548549
)
549550

550551
client.clear()

packages/query-core/src/__tests__/mutationObserver.test.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,22 @@ describe('mutationObserver', () => {
320320
await vi.advanceTimersByTimeAsync(0)
321321

322322
expect(onSuccess).toHaveBeenCalledTimes(1)
323-
expect(onSuccess).toHaveBeenCalledWith('SUCCESS', 'success', undefined)
323+
expect(onSuccess).toHaveBeenCalledWith('SUCCESS', 'success', undefined, {
324+
client: queryClient,
325+
meta: undefined,
326+
mutationKey: undefined,
327+
})
324328
expect(onSettled).toHaveBeenCalledTimes(1)
325329
expect(onSettled).toHaveBeenCalledWith(
326330
'SUCCESS',
327331
null,
328332
'success',
329333
undefined,
334+
{
335+
client: queryClient,
336+
meta: undefined,
337+
mutationKey: undefined,
338+
},
330339
)
331340

332341
unsubscribe()
@@ -354,9 +363,23 @@ describe('mutationObserver', () => {
354363
await vi.advanceTimersByTimeAsync(0)
355364

356365
expect(onError).toHaveBeenCalledTimes(1)
357-
expect(onError).toHaveBeenCalledWith(error, 'error', undefined)
366+
expect(onError).toHaveBeenCalledWith(error, 'error', undefined, {
367+
client: queryClient,
368+
meta: undefined,
369+
mutationKey: undefined,
370+
})
358371
expect(onSettled).toHaveBeenCalledTimes(1)
359-
expect(onSettled).toHaveBeenCalledWith(undefined, error, 'error', undefined)
372+
expect(onSettled).toHaveBeenCalledWith(
373+
undefined,
374+
error,
375+
'error',
376+
undefined,
377+
{
378+
client: queryClient,
379+
meta: undefined,
380+
mutationKey: undefined,
381+
},
382+
)
360383

361384
unsubscribe()
362385
})

packages/query-core/src/__tests__/mutations.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ describe('mutations', () => {
5353

5454
await vi.advanceTimersByTimeAsync(0)
5555
expect(fn).toHaveBeenCalledTimes(1)
56-
expect(fn).toHaveBeenCalledWith('vars')
56+
expect(fn).toHaveBeenCalledWith('vars', {
57+
client: queryClient,
58+
meta: undefined,
59+
mutationKey: key,
60+
})
5761
})
5862

5963
test('mutation should set correct success states', async () => {

packages/react-query/src/__tests__/useMutation.test.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,11 @@ describe('useMutation', () => {
525525
).toBeInTheDocument()
526526

527527
expect(onMutate).toHaveBeenCalledTimes(1)
528-
expect(onMutate).toHaveBeenCalledWith('todo')
528+
expect(onMutate).toHaveBeenCalledWith('todo', {
529+
client: queryClient,
530+
meta: undefined,
531+
mutationKey: undefined,
532+
})
529533

530534
onlineMock.mockReturnValue(true)
531535
queryClient.getMutationCache().resumePausedMutations()
@@ -979,26 +983,46 @@ describe('useMutation', () => {
979983
'result-todo1',
980984
'todo1',
981985
undefined,
986+
{
987+
client: queryClient,
988+
meta: undefined,
989+
mutationKey: undefined,
990+
},
982991
)
983992
expect(onSuccess).toHaveBeenNthCalledWith(
984993
2,
985994
'result-todo2',
986995
'todo2',
987996
undefined,
997+
{
998+
client: queryClient,
999+
meta: undefined,
1000+
mutationKey: undefined,
1001+
},
9881002
)
9891003
expect(onSettled).toHaveBeenCalledTimes(2)
9901004
expect(onSuccessMutate).toHaveBeenCalledTimes(1)
9911005
expect(onSuccessMutate).toHaveBeenCalledWith(
9921006
'result-todo2',
9931007
'todo2',
9941008
undefined,
1009+
{
1010+
client: queryClient,
1011+
meta: undefined,
1012+
mutationKey: undefined,
1013+
},
9951014
)
9961015
expect(onSettledMutate).toHaveBeenCalledTimes(1)
9971016
expect(onSettledMutate).toHaveBeenCalledWith(
9981017
'result-todo2',
9991018
null,
10001019
'todo2',
10011020
undefined,
1021+
{
1022+
client: queryClient,
1023+
meta: undefined,
1024+
mutationKey: undefined,
1025+
},
10021026
)
10031027
})
10041028

@@ -1033,7 +1057,11 @@ describe('useMutation', () => {
10331057
await vi.advanceTimersByTimeAsync(11)
10341058
expect(rendered.getByText('status: error')).toBeInTheDocument()
10351059

1036-
expect(onError).toHaveBeenCalledWith(error, 'todo', undefined)
1060+
expect(onError).toHaveBeenCalledWith(error, 'todo', undefined, {
1061+
client: queryClient,
1062+
meta: undefined,
1063+
mutationKey: undefined,
1064+
})
10371065
})
10381066

10391067
it('should go to error state if onError callback errors', async () => {
@@ -1110,7 +1138,11 @@ describe('useMutation', () => {
11101138
expect(
11111139
rendered.getByText('error: mutateFnError, status: error'),
11121140
).toBeInTheDocument()
1113-
expect(onError).toHaveBeenCalledWith(mutateFnError, 'todo', undefined)
1141+
expect(onError).toHaveBeenCalledWith(mutateFnError, 'todo', undefined, {
1142+
client: queryClient,
1143+
meta: undefined,
1144+
mutationKey: undefined,
1145+
})
11141146
})
11151147

11161148
it('should use provided custom queryClient', async () => {

packages/solid-query/src/__tests__/useMutation.test.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,11 @@ describe('useMutation', () => {
583583
).toBeInTheDocument()
584584

585585
expect(onMutate).toHaveBeenCalledTimes(1)
586-
expect(onMutate).toHaveBeenCalledWith('todo')
586+
expect(onMutate).toHaveBeenCalledWith('todo', {
587+
client: queryClient,
588+
meta: undefined,
589+
mutationKey: undefined,
590+
})
587591

588592
onlineMock.mockRestore()
589593
window.dispatchEvent(new Event('online'))
@@ -1053,13 +1057,22 @@ describe('useMutation', () => {
10531057
expect(onSuccess).toHaveBeenCalledTimes(2)
10541058
expect(onSettled).toHaveBeenCalledTimes(2)
10551059
expect(onSuccessMutate).toHaveBeenCalledTimes(1)
1056-
expect(onSuccessMutate).toHaveBeenCalledWith('result2', 'todo', undefined)
1060+
expect(onSuccessMutate).toHaveBeenCalledWith('result2', 'todo', undefined, {
1061+
client: queryClient,
1062+
meta: undefined,
1063+
mutationKey: undefined,
1064+
})
10571065
expect(onSettledMutate).toHaveBeenCalledTimes(1)
10581066
expect(onSettledMutate).toHaveBeenCalledWith(
10591067
'result2',
10601068
null,
10611069
'todo',
10621070
undefined,
1071+
{
1072+
client: queryClient,
1073+
meta: undefined,
1074+
mutationKey: undefined,
1075+
},
10631076
)
10641077
})
10651078

@@ -1098,7 +1111,11 @@ describe('useMutation', () => {
10981111
await vi.advanceTimersByTimeAsync(10)
10991112
await rendered.findByText('status: error')
11001113

1101-
expect(onError).toHaveBeenCalledWith(error, 'todo', undefined)
1114+
expect(onError).toHaveBeenCalledWith(error, 'todo', undefined, {
1115+
client: queryClient,
1116+
meta: undefined,
1117+
mutationKey: undefined,
1118+
})
11021119
})
11031120

11041121
it('should go to error state if onError callback errors', async () => {
@@ -1183,7 +1200,11 @@ describe('useMutation', () => {
11831200
rendered.getByText('error: mutateFnError, status: error'),
11841201
).toBeInTheDocument()
11851202

1186-
expect(onError).toHaveBeenCalledWith(mutateFnError, 'todo', undefined)
1203+
expect(onError).toHaveBeenCalledWith(mutateFnError, 'todo', undefined, {
1204+
client: queryClient,
1205+
meta: undefined,
1206+
mutationKey: undefined,
1207+
})
11871208
})
11881209

11891210
it('should use provided custom queryClient', async () => {

0 commit comments

Comments
 (0)