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