Open History to see the last commit
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})