File size: 1,746 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 |
import { beforeEach, describe, expect, test, vi } from 'vitest'
import { hasInjectionContext, inject } from 'vue-demi'
import { useQueryClient } from '../useQueryClient'
import { VUE_QUERY_CLIENT } from '../utils'
import type { Mock } from 'vitest'
describe('useQueryClient', () => {
const injectSpy = inject as Mock
const hasInjectionContextSpy = hasInjectionContext as Mock
beforeEach(() => {
vi.restoreAllMocks()
})
test('should return queryClient when it is provided in the context', () => {
const queryClientMock = { name: 'Mocked client' }
injectSpy.mockReturnValueOnce(queryClientMock)
const queryClient = useQueryClient()
expect(queryClient).toStrictEqual(queryClientMock)
expect(injectSpy).toHaveBeenCalledTimes(1)
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
})
test('should throw an error when queryClient does not exist in the context', () => {
injectSpy.mockReturnValueOnce(undefined)
expect(useQueryClient).toThrowError()
expect(injectSpy).toHaveBeenCalledTimes(1)
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
})
test('should throw an error when used outside of setup function', () => {
hasInjectionContextSpy.mockReturnValueOnce(false)
expect(useQueryClient).toThrowError()
expect(hasInjectionContextSpy).toHaveBeenCalledTimes(1)
})
test('should call inject with a custom key as a suffix', () => {
const queryClientKey = 'foo'
const expectedKeyParameter = `${VUE_QUERY_CLIENT}:${queryClientKey}`
const queryClientMock = { name: 'Mocked client' }
injectSpy.mockReturnValueOnce(queryClientMock)
useQueryClient(queryClientKey)
expect(injectSpy).toHaveBeenCalledWith(expectedKeyParameter)
})
})
|