Skip to content

Commit 15d4a1a

Browse files
authored
test(vue-query/useMutationState): split 'useMutationState' tests into separate file from 'useIsMutating.test.ts' (#10236)
1 parent b6fd093 commit 15d4a1a

2 files changed

Lines changed: 82 additions & 75 deletions

File tree

packages/vue-query/src/__tests__/useIsMutating.test.ts

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
22
import { onScopeDispose, reactive, ref } from 'vue-demi'
33
import { sleep } from '@tanstack/query-test-utils'
44
import { useMutation } from '../useMutation'
5-
import { useIsMutating, useMutationState } from '../useMutationState'
6-
import { useQueryClient } from '../useQueryClient'
5+
import { useIsMutating } from '../useMutationState'
76
import type { MockedFunction } from 'vitest'
87

98
vi.mock('../useQueryClient')
@@ -110,75 +109,3 @@ describe('useIsMutating', () => {
110109
expect(isMutating.value).toStrictEqual(1)
111110
})
112111
})
113-
114-
describe('useMutationState', () => {
115-
beforeEach(() => {
116-
vi.useFakeTimers()
117-
})
118-
119-
afterEach(() => {
120-
vi.useRealTimers()
121-
})
122-
123-
it('should return variables after calling mutate 1', () => {
124-
const mutationKey = ['mutation']
125-
const variables = 'foo123'
126-
127-
const { mutate } = useMutation({
128-
mutationKey: mutationKey,
129-
mutationFn: (params: string) => sleep(0).then(() => params),
130-
})
131-
132-
mutate(variables)
133-
134-
const mutationState = useMutationState({
135-
filters: { mutationKey, status: 'pending' },
136-
select: (mutation) => mutation.state.variables,
137-
})
138-
139-
expect(mutationState.value).toEqual([variables])
140-
})
141-
142-
it('should return variables after calling mutate 2', () => {
143-
const queryClient = useQueryClient()
144-
queryClient.clear()
145-
const mutationKey = ['mutation']
146-
const variables = 'bar234'
147-
148-
const { mutate } = useMutation({
149-
mutationKey: mutationKey,
150-
mutationFn: (params: string) => sleep(0).then(() => params),
151-
})
152-
153-
mutate(variables)
154-
155-
const mutationState = useMutationState()
156-
157-
expect(mutationState.value[0]?.variables).toEqual(variables)
158-
})
159-
160-
it('should work with options getter and be reactive', async () => {
161-
const keyRef = ref('useMutationStateGetter2')
162-
const variables = 'foo123'
163-
164-
const { mutate } = useMutation({
165-
mutationKey: ['useMutationStateGetter'],
166-
mutationFn: (params: string) => sleep(10).then(() => params),
167-
})
168-
169-
mutate(variables)
170-
171-
const mutationState = useMutationState(() => ({
172-
filters: { mutationKey: [keyRef.value], status: 'pending' },
173-
select: (mutation) => mutation.state.variables,
174-
}))
175-
176-
expect(mutationState.value).toEqual([])
177-
178-
keyRef.value = 'useMutationStateGetter'
179-
180-
await vi.advanceTimersByTimeAsync(0)
181-
182-
expect(mutationState.value).toEqual([variables])
183-
})
184-
})
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { ref } from 'vue-demi'
3+
import { sleep } from '@tanstack/query-test-utils'
4+
import { useMutation } from '../useMutation'
5+
import { useMutationState } from '../useMutationState'
6+
import { useQueryClient } from '../useQueryClient'
7+
8+
vi.mock('../useQueryClient')
9+
10+
describe('useMutationState', () => {
11+
beforeEach(() => {
12+
vi.useFakeTimers()
13+
})
14+
15+
afterEach(() => {
16+
vi.useRealTimers()
17+
})
18+
19+
it('should return variables after calling mutate 1', () => {
20+
const mutationKey = ['mutation']
21+
const variables = 'foo123'
22+
23+
const { mutate } = useMutation({
24+
mutationKey: mutationKey,
25+
mutationFn: (params: string) => sleep(0).then(() => params),
26+
})
27+
28+
mutate(variables)
29+
30+
const mutationState = useMutationState({
31+
filters: { mutationKey, status: 'pending' },
32+
select: (mutation) => mutation.state.variables,
33+
})
34+
35+
expect(mutationState.value).toEqual([variables])
36+
})
37+
38+
it('should return variables after calling mutate 2', () => {
39+
const queryClient = useQueryClient()
40+
queryClient.clear()
41+
const mutationKey = ['mutation']
42+
const variables = 'bar234'
43+
44+
const { mutate } = useMutation({
45+
mutationKey: mutationKey,
46+
mutationFn: (params: string) => sleep(0).then(() => params),
47+
})
48+
49+
mutate(variables)
50+
51+
const mutationState = useMutationState()
52+
53+
expect(mutationState.value[0]?.variables).toEqual(variables)
54+
})
55+
56+
it('should work with options getter and be reactive', async () => {
57+
const keyRef = ref('useMutationStateGetter2')
58+
const variables = 'foo123'
59+
60+
const { mutate } = useMutation({
61+
mutationKey: ['useMutationStateGetter'],
62+
mutationFn: (params: string) => sleep(10).then(() => params),
63+
})
64+
65+
mutate(variables)
66+
67+
const mutationState = useMutationState(() => ({
68+
filters: { mutationKey: [keyRef.value], status: 'pending' },
69+
select: (mutation) => mutation.state.variables,
70+
}))
71+
72+
expect(mutationState.value).toEqual([])
73+
74+
keyRef.value = 'useMutationStateGetter'
75+
76+
await vi.advanceTimersByTimeAsync(0)
77+
78+
expect(mutationState.value).toEqual([variables])
79+
})
80+
})

0 commit comments

Comments
 (0)