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