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

useIsMutating.test.ts

111 lines2.9 KB

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