Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createMutation.svelte.test.ts

131 lines4.5 KB

1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { flushSync } from 'svelte'
3import { fireEvent, render } from '@testing-library/svelte'
4import { QueryClient } from '@tanstack/query-core'
5import { sleep } from '@tanstack/query-test-utils'
6import { createMutation } from '../../src/index.js'
7import { withEffectRoot } from '../utils.svelte.js'
8import ResetExample from './ResetExample.svelte'
9import OnSuccessExample from './OnSuccessExample.svelte'
10import FailureExample from './FailureExample.svelte'
11​
12describe('createMutation', () => {
13 beforeEach(() => {
14 vi.useFakeTimers()
15 })
16​
17 afterEach(() => {
18 vi.useRealTimers()
19 })
20​
21 test('Able to reset `error`', async () => {
22 const rendered = render(ResetExample)
23​
24 expect(rendered.queryByText('Error: undefined')).toBeInTheDocument()
25​
26 fireEvent.click(rendered.getByRole('button', { name: /Mutate/i }))
27 await vi.advanceTimersByTimeAsync(11)
28 expect(rendered.getByText('Error: Expected mock error')).toBeInTheDocument()
29​
30 fireEvent.click(rendered.getByRole('button', { name: /Reset/i }))
31 await vi.advanceTimersByTimeAsync(11)
32 expect(rendered.getByText('Error: undefined')).toBeInTheDocument()
33 })
34​
35 test('Able to call `onSuccess` and `onSettled` after each successful mutate', async () => {
36 const onSuccessMock = vi.fn()
37 const onSettledMock = vi.fn()
38​
39 const rendered = render(OnSuccessExample, {
40 props: {
41 onSuccessMock,
42 onSettledMock,
43 },
44 })
45​
46 expect(rendered.queryByText('Count: 0')).toBeInTheDocument()
47​
48 fireEvent.click(rendered.getByRole('button', { name: /Mutate/i }))
49 fireEvent.click(rendered.getByRole('button', { name: /Mutate/i }))
50 fireEvent.click(rendered.getByRole('button', { name: /Mutate/i }))
51 await vi.advanceTimersByTimeAsync(11)
52 expect(rendered.queryByText('Count: 3')).toBeInTheDocument()
53​
54 expect(onSuccessMock).toHaveBeenCalledTimes(3)
55 expect(onSuccessMock).toHaveBeenNthCalledWith(1, 1)
56 expect(onSuccessMock).toHaveBeenNthCalledWith(2, 2)
57 expect(onSuccessMock).toHaveBeenNthCalledWith(3, 3)
58​
59 expect(onSettledMock).toHaveBeenCalledTimes(3)
60 expect(onSettledMock).toHaveBeenNthCalledWith(1, 1)
61 expect(onSettledMock).toHaveBeenNthCalledWith(2, 2)
62 expect(onSettledMock).toHaveBeenNthCalledWith(3, 3)
63 })
64​
65 test('Set correct values for `failureReason` and `failureCount` on multiple mutate calls', async () => {
66 type Value = { count: number }
67​
68 const mutationFn = vi.fn<(value: Value) => Promise<Value>>()
69​
70 mutationFn.mockImplementationOnce(() =>
71 sleep(20).then(() => Promise.reject(`Expected mock error`)),
72 )
73​
74 mutationFn.mockImplementation((value) => sleep(10).then(() => value))
75​
76 const rendered = render(FailureExample, {
77 props: {
78 mutationFn,
79 },
80 })
81​
82 expect(rendered.queryByText('Data: undefined')).toBeInTheDocument()
83​
84 fireEvent.click(rendered.getByRole('button', { name: /Mutate/i }))
85 expect(rendered.getByText('Data: undefined')).toBeInTheDocument()
86 await vi.advanceTimersByTimeAsync(21)
87 expect(rendered.getByText('Status: error')).toBeInTheDocument()
88 expect(rendered.getByText('Failure Count: 1')).toBeInTheDocument()
89 expect(
90 rendered.getByText('Failure Reason: Expected mock error'),
91 ).toBeInTheDocument()
92​
93 fireEvent.click(rendered.getByRole('button', { name: /Mutate/i }))
94 await vi.advanceTimersByTimeAsync(0)
95 expect(rendered.getByText('Status: pending')).toBeInTheDocument()
96 await vi.advanceTimersByTimeAsync(11)
97 expect(rendered.getByText('Status: success')).toBeInTheDocument()
98 expect(rendered.getByText('Data: 2')).toBeInTheDocument()
99 expect(rendered.getByText('Failure Count: 0')).toBeInTheDocument()
100 expect(rendered.getByText('Failure Reason: undefined')).toBeInTheDocument()
101 })
102​
103 test(
104 'should recreate observer when queryClient changes',
105 withEffectRoot(async () => {
106 const queryClient1 = new QueryClient()
107 const queryClient2 = new QueryClient()
108​
109 let queryClient = $state(queryClient1)
110​
111 const mutation = createMutation(
112 () => ({
113 mutationFn: (params: string) => sleep(10).then(() => params),
114 }),
115 () => queryClient,
116 )
117​
118 mutation.mutate('first')
119 await vi.advanceTimersByTimeAsync(11)
120​
121 expect(mutation.status).toBe('success')
122 expect(mutation.data).toBe('first')
123​
124 queryClient = queryClient2
125 flushSync()
126​
127 expect(mutation.status).toBe('idle')
128 expect(mutation.data).toBeUndefined()
129 }),
130 )
131})