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

useIsFetching.test.ts

108 lines3.1 KB

1import { createKey, sleep } from '@tanstack/query-test-utils'
2import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3import { onScopeDispose, reactive, ref } from 'vue-demi'
4import { useIsFetching } from '../useIsFetching'
5import { useQuery } from '../useQuery'
6​
7vi.mock(import('../useQueryClient'))
8​
9describe('useIsFetching', () => {
10 beforeEach(() => {
11 vi.useFakeTimers()
12 })
13​
14 afterEach(() => {
15 vi.useRealTimers()
16 })
17​
18 it('should properly return isFetching state', async () => {
19 const key1 = createKey()
20 const key2 = createKey()
21 const { isFetching: isFetchingQuery } = useQuery({
22 queryKey: key1,
23 queryFn: () => sleep(0).then(() => 'Some data'),
24 })
25 useQuery({
26 queryKey: key2,
27 queryFn: () => sleep(0).then(() => 'Some data'),
28 })
29 const isFetching = useIsFetching()
30​
31 expect(isFetchingQuery.value).toBe(true)
32 expect(isFetching.value).toBe(2)
33 await vi.advanceTimersByTimeAsync(0)
34 expect(isFetchingQuery.value).toBe(false)
35 expect(isFetching.value).toBe(0)
36 })
37​
38 it('should stop listening to changes on onScopeDispose', async () => {
39 const key = createKey()
40 const onScopeDisposeMock = vi.mocked(onScopeDispose)
41 onScopeDisposeMock.mockImplementation((fn) => fn())
42​
43 const { status } = useQuery({
44 queryKey: key,
45 queryFn: () => sleep(0).then(() => 'Some data'),
46 })
47 const isFetching = useIsFetching()
48​
49 expect(status.value).toBe('pending')
50 expect(isFetching.value).toBe(1)
51 await vi.advanceTimersByTimeAsync(0)
52 expect(status.value).toBe('pending')
53 expect(isFetching.value).toBe(1)
54 await vi.advanceTimersByTimeAsync(0)
55 expect(status.value).toBe('pending')
56 expect(isFetching.value).toBe(1)
57​
58 onScopeDisposeMock.mockReset()
59 })
60​
61 it('should properly update filters', async () => {
62 const key = createKey()
63 const filter = reactive({ stale: false, queryKey: key })
64 useQuery({
65 queryKey: key,
66 queryFn: () => sleep(10).then(() => 'Some data'),
67 })
68 const isFetching = useIsFetching(filter)
69​
70 expect(isFetching.value).toBe(0)
71​
72 filter.stale = true
73 await vi.advanceTimersByTimeAsync(0)
74 expect(isFetching.value).toBe(1)
75 })
76​
77 it('should work with options getter and be reactive', async () => {
78 const key = createKey()
79 const staleRef = ref(false)
80 useQuery({
81 queryKey: key,
82 queryFn: () => sleep(10).then(() => 'Some data'),
83 })
84 const isFetching = useIsFetching(() => ({
85 stale: staleRef.value,
86 queryKey: key,
87 }))
88​
89 expect(isFetching.value).toBe(0)
90​
91 staleRef.value = true
92 await vi.advanceTimersByTimeAsync(0)
93 expect(isFetching.value).toBe(1)
94 })
95​
96 it('should warn when used outside of setup function in development mode', () => {
97 vi.stubEnv('NODE_ENV', 'development')
98 const consoleWarnMock = vi
99 .spyOn(console, 'warn')
100 .mockImplementation(() => {})
101​
102 useIsFetching()
103​
104 expect(consoleWarnMock).toHaveBeenCalledWith(
105 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
106 )
107 })
108})