File size: 3,535 Bytes
1e92f2d |
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 |
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'
import { onScopeDispose, reactive } from 'vue-demi'
import { sleep } from '@tanstack/query-test-utils'
import { useMutation } from '../useMutation'
import { useIsMutating, useMutationState } from '../useMutationState'
import { useQueryClient } from '../useQueryClient'
import type { MockedFunction } from 'vitest'
vi.mock('../useQueryClient')
describe('useIsMutating', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
test('should properly return isMutating state', async () => {
const mutation = useMutation({
mutationFn: (params: string) => sleep(10).then(() => params),
})
const mutation2 = useMutation({
mutationFn: (params: string) => sleep(10).then(() => params),
})
const isMutating = useIsMutating()
expect(isMutating.value).toStrictEqual(0)
mutation.mutateAsync('a')
mutation2.mutateAsync('b')
await vi.advanceTimersByTimeAsync(0)
expect(isMutating.value).toStrictEqual(2)
await vi.advanceTimersByTimeAsync(10)
expect(isMutating.value).toStrictEqual(0)
})
test('should stop listening to changes on onScopeDispose', async () => {
const onScopeDisposeMock = onScopeDispose as MockedFunction<
typeof onScopeDispose
>
onScopeDisposeMock.mockImplementation((fn) => fn())
const mutation = useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
})
const mutation2 = useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
})
const isMutating = useIsMutating()
expect(isMutating.value).toStrictEqual(0)
mutation.mutateAsync('a')
mutation2.mutateAsync('b')
await vi.advanceTimersByTimeAsync(0)
expect(isMutating.value).toStrictEqual(0)
await vi.advanceTimersByTimeAsync(0)
expect(isMutating.value).toStrictEqual(0)
onScopeDisposeMock.mockReset()
})
test('should properly update filters', async () => {
const filter = reactive({ mutationKey: ['foo'] })
const { mutate } = useMutation({
mutationKey: ['isMutating'],
mutationFn: (params: string) => sleep(10).then(() => params),
})
mutate('foo')
const isMutating = useIsMutating(filter)
expect(isMutating.value).toStrictEqual(0)
filter.mutationKey = ['isMutating']
await vi.advanceTimersByTimeAsync(0)
expect(isMutating.value).toStrictEqual(1)
})
})
describe('useMutationState', () => {
it('should return variables after calling mutate 1', () => {
const mutationKey = ['mutation']
const variables = 'foo123'
const { mutate } = useMutation({
mutationKey: mutationKey,
mutationFn: (params: string) => sleep(0).then(() => params),
})
mutate(variables)
const mutationState = useMutationState({
filters: { mutationKey, status: 'pending' },
select: (mutation) => mutation.state.variables,
})
expect(mutationState.value).toEqual([variables])
})
it('should return variables after calling mutate 2', () => {
const queryClient = useQueryClient()
queryClient.clear()
const mutationKey = ['mutation']
const variables = 'bar234'
const { mutate } = useMutation({
mutationKey: mutationKey,
mutationFn: (params: string) => sleep(0).then(() => params),
})
mutate(variables)
const mutationState = useMutationState()
expect(mutationState.value[0]?.variables).toEqual(variables)
})
})
|