File size: 2,431 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 |
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { onScopeDispose, reactive } from 'vue-demi'
import { sleep } from '@tanstack/query-test-utils'
import { useQuery } from '../useQuery'
import { useIsFetching } from '../useIsFetching'
import type { MockedFunction } from 'vitest'
vi.mock('../useQueryClient')
describe('useIsFetching', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
test('should properly return isFetching state', async () => {
const { isFetching: isFetchingQuery } = useQuery({
queryKey: ['isFetching1'],
queryFn: () => sleep(0).then(() => 'Some data'),
})
useQuery({
queryKey: ['isFetching2'],
queryFn: () => sleep(0).then(() => 'Some data'),
})
const isFetching = useIsFetching()
expect(isFetchingQuery.value).toStrictEqual(true)
expect(isFetching.value).toStrictEqual(2)
await vi.advanceTimersByTimeAsync(0)
expect(isFetchingQuery.value).toStrictEqual(false)
expect(isFetching.value).toStrictEqual(0)
})
test('should stop listening to changes on onScopeDispose', async () => {
const onScopeDisposeMock = onScopeDispose as MockedFunction<
typeof onScopeDispose
>
onScopeDisposeMock.mockImplementation((fn) => fn())
const { status } = useQuery({
queryKey: ['onScopeDispose'],
queryFn: () => sleep(0).then(() => 'Some data'),
})
const isFetching = useIsFetching()
expect(status.value).toStrictEqual('pending')
expect(isFetching.value).toStrictEqual(1)
await vi.advanceTimersByTimeAsync(0)
expect(status.value).toStrictEqual('pending')
expect(isFetching.value).toStrictEqual(1)
await vi.advanceTimersByTimeAsync(0)
expect(status.value).toStrictEqual('pending')
expect(isFetching.value).toStrictEqual(1)
onScopeDisposeMock.mockReset()
})
test('should properly update filters', async () => {
const filter = reactive({ stale: false })
useQuery({
queryKey: ['isFetching'],
queryFn: () =>
new Promise((resolve) => {
setTimeout(() => {
return resolve('Some data')
}, 100)
}),
})
const isFetching = useIsFetching(filter)
expect(isFetching.value).toStrictEqual(0)
filter.stale = true
await vi.advanceTimersByTimeAsync(0)
expect(isFetching.value).toStrictEqual(1)
})
})
|