Open History to see the last commit
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})