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

ReactQueryDevtoolsPanel.test.tsx

78 lines2.3 KB

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