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

useIsFetching.test.ts

77 lines2.1 KB

1import { onScopeDispose, reactive } from 'vue-demi'
2​
3import { flushPromises, simpleFetcher } from './test-utils'
4import { useQuery } from '../useQuery'
5import { useIsFetching } from '../useIsFetching'
6import { vi } from 'vitest'
7import type { MockedFunction } from 'vitest'
8​
9vi.mock('../useQueryClient')
10​
11describe('useIsFetching', () => {
12 test('should properly return isFetching state', async () => {
13 const { isFetching: isFetchingQuery } = useQuery({
14 queryKey: ['isFetching1'],
15 queryFn: simpleFetcher,
16 })
17 useQuery({ queryKey: ['isFetching2'], queryFn: simpleFetcher })
18 const isFetching = useIsFetching()
19​
20 expect(isFetchingQuery.value).toStrictEqual(true)
21 expect(isFetching.value).toStrictEqual(2)
22​
23 await flushPromises()
24​
25 expect(isFetchingQuery.value).toStrictEqual(false)
26 expect(isFetching.value).toStrictEqual(0)
27 })
28​
29 test('should stop listening to changes on onScopeDispose', async () => {
30 const onScopeDisposeMock = onScopeDispose as MockedFunction<
31 typeof onScopeDispose
32 >
33 onScopeDisposeMock.mockImplementation((fn) => fn())
34​
35 const { status } = useQuery({
36 queryKey: ['onScopeDispose'],
37 queryFn: simpleFetcher,
38 })
39 const isFetching = useIsFetching()
40​
41 expect(status.value).toStrictEqual('pending')
42 expect(isFetching.value).toStrictEqual(1)
43​
44 await flushPromises()
45​
46 expect(status.value).toStrictEqual('pending')
47 expect(isFetching.value).toStrictEqual(1)
48​
49 await flushPromises()
50​
51 expect(status.value).toStrictEqual('pending')
52 expect(isFetching.value).toStrictEqual(1)
53​
54 onScopeDisposeMock.mockReset()
55 })
56​
57 test('should properly update filters', async () => {
58 const filter = reactive({ stale: false })
59 useQuery({
60 queryKey: ['isFetching'],
61 queryFn: () =>
62 new Promise((resolve) => {
63 setTimeout(() => {
64 return resolve('Some data')
65 }, 100)
66 }),
67 })
68 const isFetching = useIsFetching(filter)
69​
70 expect(isFetching.value).toStrictEqual(0)
71​
72 filter.stale = true
73 await flushPromises()
74​
75 expect(isFetching.value).toStrictEqual(1)
76 })
77})