Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/vue-query/src/__tests__/useIsMutating.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it, test, vi } from 'vitest'
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'
Expand All @@ -9,12 +9,20 @@ 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(0).then(() => params),
mutationFn: (params: string) => sleep(10).then(() => params),
})
const mutation2 = useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
mutationFn: (params: string) => sleep(10).then(() => params),
})
const isMutating = useIsMutating()

Expand All @@ -23,11 +31,11 @@ describe('useIsMutating', () => {
mutation.mutateAsync('a')
mutation2.mutateAsync('b')

await sleep(0)
await vi.advanceTimersByTimeAsync(0)

expect(isMutating.value).toStrictEqual(2)

await sleep(0)
await vi.advanceTimersByTimeAsync(10)

expect(isMutating.value).toStrictEqual(0)
})
Expand All @@ -51,11 +59,11 @@ describe('useIsMutating', () => {
mutation.mutateAsync('a')
mutation2.mutateAsync('b')

await sleep(0)
await vi.advanceTimersByTimeAsync(0)

expect(isMutating.value).toStrictEqual(0)

await sleep(0)
await vi.advanceTimersByTimeAsync(0)

expect(isMutating.value).toStrictEqual(0)

Expand All @@ -66,7 +74,7 @@ describe('useIsMutating', () => {
const filter = reactive({ mutationKey: ['foo'] })
const { mutate } = useMutation({
mutationKey: ['isMutating'],
mutationFn: (params: string) => sleep(0).then(() => params),
mutationFn: (params: string) => sleep(10).then(() => params),
})
mutate('foo')

Expand All @@ -76,7 +84,7 @@ describe('useIsMutating', () => {

filter.mutationKey = ['isMutating']

await sleep(0)
await vi.advanceTimersByTimeAsync(0)

expect(isMutating.value).toStrictEqual(1)
})
Expand Down
Loading