Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@testing-library/react'
3import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4import type { TanstackQueryDevtools } from '@tanstack/query-devtools'
5β
6const mountMock = vi.fn()
7const unmountMock = vi.fn()
8const setClientMock = vi.fn()
9const setButtonPositionMock = vi.fn()
10const setPositionMock = vi.fn()
11const setInitialIsOpenMock = vi.fn()
12const setErrorTypesMock = vi.fn()
13const setThemeMock = vi.fn()
14β
15vi.mock('@tanstack/query-devtools', () => ({
16 TanstackQueryDevtools: vi.fn(function (this: TanstackQueryDevtools) {
17 this.mount = mountMock
18 this.unmount = unmountMock
19 this.setClient = setClientMock
20 this.setButtonPosition = setButtonPositionMock
21 this.setPosition = setPositionMock
22 this.setInitialIsOpen = setInitialIsOpenMock
23 this.setErrorTypes = setErrorTypesMock
24 this.setTheme = setThemeMock
25 }),
26}))
27β
28describe('ReactQueryDevtools', () => {
29 beforeEach(() => {
30 vi.clearAllMocks()
31 })
32β
33 it('should throw an error if no query client has been set', async () => {
34 const { ReactQueryDevtools } = await import('../ReactQueryDevtools')
35β
36 expect(() => render(<ReactQueryDevtools />)).toThrow(
37 'No QueryClient set, use QueryClientProvider to set one',
38 )
39 })
40β
41 it('should not throw an error if query client is provided via context', async () => {
42 const { ReactQueryDevtools } = await import('../ReactQueryDevtools')
43 const queryClient = new QueryClient()
44β
45 expect(() =>
46 render(
47 <QueryClientProvider client={queryClient}>
48 <ReactQueryDevtools />
49 </QueryClientProvider>,
50 ),
51 ).not.toThrow()
52 expect(mountMock).toHaveBeenCalled()
53 })
54β
55 it('should not throw an error if query client is provided via props', async () => {
56 const { ReactQueryDevtools } = await import('../ReactQueryDevtools')
57 const queryClient = new QueryClient()
58β
59 expect(() =>
60 render(<ReactQueryDevtools client={queryClient} />),
61 ).not.toThrow()
62 expect(mountMock).toHaveBeenCalled()
63 })
64β
65 it('should return null in non-development environments', async () => {
66 vi.stubEnv('NODE_ENV', 'production')
67 vi.resetModules()
68β
69 try {
70 const { ReactQueryDevtools } = await import('..')
71 expect(ReactQueryDevtools({})).toBeNull()
72 } finally {
73 vi.unstubAllEnvs()
74 vi.resetModules()
75 }
76 })
77})