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

useQueryClient.test.ts

45 lines1.6 KB

1import { getCurrentInstance, inject } from 'vue-demi'
2import { useQueryClient } from '../useQueryClient'
3import { VUE_QUERY_CLIENT } from '../utils'
4​
5describe('useQueryClient', () => {
6 const injectSpy = inject as jest.Mock
7 const getCurrentInstanceSpy = getCurrentInstance as jest.Mock
8​
9 test('should return queryClient when it is provided in the context', () => {
10 const queryClientMock = { name: 'Mocked client' }
11 injectSpy.mockReturnValueOnce(queryClientMock)
12​
13 const queryClient = useQueryClient()
14​
15 expect(queryClient).toStrictEqual(queryClientMock)
16 expect(injectSpy).toHaveBeenCalledTimes(1)
17 expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
18 })
19​
20 test('should throw an error when queryClient does not exist in the context', () => {
21 injectSpy.mockReturnValueOnce(undefined)
22​
23 expect(useQueryClient).toThrowError()
24 expect(injectSpy).toHaveBeenCalledTimes(1)
25 expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
26 })
27​
28 test('should throw an error when used outside of setup function', () => {
29 getCurrentInstanceSpy.mockReturnValueOnce(undefined)
30​
31 expect(useQueryClient).toThrowError()
32 expect(getCurrentInstanceSpy).toHaveBeenCalledTimes(1)
33 })
34​
35 test('should call inject with a custom key as a suffix', () => {
36 const queryClientKey = 'foo'
37 const expectedKeyParameter = `${VUE_QUERY_CLIENT}:${queryClientKey}`
38 const queryClientMock = { name: 'Mocked client' }
39 injectSpy.mockReturnValueOnce(queryClientMock)
40​
41 useQueryClient(queryClientKey)
42​
43 expect(injectSpy).toHaveBeenCalledWith(expectedKeyParameter)
44 })
45})