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

useIsMutating.test.ts

130 lines3.5 KB

1import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'
2import { onScopeDispose, reactive } from 'vue-demi'
3import { sleep } from '@tanstack/query-test-utils'
4import { useMutation } from '../useMutation'
5import { useIsMutating, useMutationState } from '../useMutationState'
6import { useQueryClient } from '../useQueryClient'
7import type { MockedFunction } from 'vitest'
8​
9vi.mock('../useQueryClient')
10​
11describe('useIsMutating', () => {
12 beforeEach(() => {
13 vi.useFakeTimers()
14 })
15​
16 afterEach(() => {
17 vi.useRealTimers()
18 })
19​
20 test('should properly return isMutating state', async () => {
21 const mutation = useMutation({
22 mutationFn: (params: string) => sleep(10).then(() => params),
23 })
24 const mutation2 = useMutation({
25 mutationFn: (params: string) => sleep(10).then(() => params),
26 })
27 const isMutating = useIsMutating()
28​
29 expect(isMutating.value).toStrictEqual(0)
30​
31 mutation.mutateAsync('a')
32 mutation2.mutateAsync('b')
33​
34 await vi.advanceTimersByTimeAsync(0)
35​
36 expect(isMutating.value).toStrictEqual(2)
37​
38 await vi.advanceTimersByTimeAsync(10)
39​
40 expect(isMutating.value).toStrictEqual(0)
41 })
42​
43 test('should stop listening to changes on onScopeDispose', async () => {
44 const onScopeDisposeMock = onScopeDispose as MockedFunction<
45 typeof onScopeDispose
46 >
47 onScopeDisposeMock.mockImplementation((fn) => fn())
48​
49 const mutation = useMutation({
50 mutationFn: (params: string) => sleep(0).then(() => params),
51 })
52 const mutation2 = useMutation({
53 mutationFn: (params: string) => sleep(0).then(() => params),
54 })
55 const isMutating = useIsMutating()
56​
57 expect(isMutating.value).toStrictEqual(0)
58​
59 mutation.mutateAsync('a')
60 mutation2.mutateAsync('b')
61​
62 await vi.advanceTimersByTimeAsync(0)
63​
64 expect(isMutating.value).toStrictEqual(0)
65​
66 await vi.advanceTimersByTimeAsync(0)
67​
68 expect(isMutating.value).toStrictEqual(0)
69​
70 onScopeDisposeMock.mockReset()
71 })
72​
73 test('should properly update filters', async () => {
74 const filter = reactive({ mutationKey: ['foo'] })
75 const { mutate } = useMutation({
76 mutationKey: ['isMutating'],
77 mutationFn: (params: string) => sleep(10).then(() => params),
78 })
79 mutate('foo')
80​
81 const isMutating = useIsMutating(filter)
82​
83 expect(isMutating.value).toStrictEqual(0)
84​
85 filter.mutationKey = ['isMutating']
86​
87 await vi.advanceTimersByTimeAsync(0)
88​
89 expect(isMutating.value).toStrictEqual(1)
90 })
91})
92​
93describe('useMutationState', () => {
94 it('should return variables after calling mutate 1', () => {
95 const mutationKey = ['mutation']
96 const variables = 'foo123'
97​
98 const { mutate } = useMutation({
99 mutationKey: mutationKey,
100 mutationFn: (params: string) => sleep(0).then(() => params),
101 })
102​
103 mutate(variables)
104​
105 const mutationState = useMutationState({
106 filters: { mutationKey, status: 'pending' },
107 select: (mutation) => mutation.state.variables,
108 })
109​
110 expect(mutationState.value).toEqual([variables])
111 })
112​
113 it('should return variables after calling mutate 2', () => {
114 const queryClient = useQueryClient()
115 queryClient.clear()
116 const mutationKey = ['mutation']
117 const variables = 'bar234'
118​
119 const { mutate } = useMutation({
120 mutationKey: mutationKey,
121 mutationFn: (params: string) => sleep(0).then(() => params),
122 })
123​
124 mutate(variables)
125​
126 const mutationState = useMutationState()
127​
128 expect(mutationState.value[0]?.variables).toEqual(variables)
129 })
130})