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

devtools.test.tsx

170 lines5.1 KB

1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@solidjs/testing-library'
3import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
4import { TanstackQueryDevtools } from '@tanstack/query-devtools'
5import SolidQueryDevtools from '../devtools'
6​
7describe('SolidQueryDevtools', () => {
8 afterEach(() => {
9 vi.restoreAllMocks()
10 })
11​
12 it('should throw an error if no query client has been set', () => {
13 expect(() => render(() => <SolidQueryDevtools />)).toThrow(
14 'No QueryClient set, use QueryClientProvider to set one',
15 )
16 })
17​
18 it('should not throw an error if query client is provided via context', () => {
19 const queryClient = new QueryClient()
20​
21 expect(() =>
22 render(() => (
23 <QueryClientProvider client={queryClient}>
24 <SolidQueryDevtools />
25 </QueryClientProvider>
26 )),
27 ).not.toThrow()
28 })
29​
30 it('should not throw an error if query client is provided via props', () => {
31 const queryClient = new QueryClient()
32​
33 expect(() =>
34 render(() => <SolidQueryDevtools client={queryClient} />),
35 ).not.toThrow()
36 })
37​
38 it('should forward "buttonPosition" to the devtools instance', () => {
39 const setButtonPosition = vi.spyOn(
40 TanstackQueryDevtools.prototype,
41 'setButtonPosition',
42 )
43 const queryClient = new QueryClient()
44​
45 render(() => (
46 <SolidQueryDevtools client={queryClient} buttonPosition="top-left" />
47 ))
48​
49 expect(setButtonPosition).toHaveBeenCalledWith('top-left')
50 })
51​
52 it('should forward "position" to the devtools instance', () => {
53 const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition')
54 const queryClient = new QueryClient()
55​
56 render(() => <SolidQueryDevtools client={queryClient} position="left" />)
57​
58 expect(setPosition).toHaveBeenCalledWith('left')
59 })
60​
61 it('should forward "initialIsOpen" to the devtools instance', () => {
62 const setInitialIsOpen = vi.spyOn(
63 TanstackQueryDevtools.prototype,
64 'setInitialIsOpen',
65 )
66 const queryClient = new QueryClient()
67​
68 render(() => (
69 <SolidQueryDevtools client={queryClient} initialIsOpen={true} />
70 ))
71​
72 expect(setInitialIsOpen).toHaveBeenCalledWith(true)
73 })
74​
75 it('should default "initialIsOpen" to "false" when the prop is omitted', () => {
76 const setInitialIsOpen = vi.spyOn(
77 TanstackQueryDevtools.prototype,
78 'setInitialIsOpen',
79 )
80 const queryClient = new QueryClient()
81​
82 render(() => <SolidQueryDevtools client={queryClient} />)
83​
84 expect(setInitialIsOpen).toHaveBeenCalledWith(false)
85 })
86​
87 it('should forward "errorTypes" to the devtools instance', () => {
88 const setErrorTypes = vi.spyOn(
89 TanstackQueryDevtools.prototype,
90 'setErrorTypes',
91 )
92 const queryClient = new QueryClient()
93 const errorTypes = [
94 { name: 'Network', initializer: () => new Error('Network') },
95 ]
96​
97 render(() => (
98 <SolidQueryDevtools client={queryClient} errorTypes={errorTypes} />
99 ))
100​
101 expect(setErrorTypes).toHaveBeenCalledWith(errorTypes)
102 })
103​
104 it('should default "errorTypes" to an empty array when the prop is omitted', () => {
105 const setErrorTypes = vi.spyOn(
106 TanstackQueryDevtools.prototype,
107 'setErrorTypes',
108 )
109 const queryClient = new QueryClient()
110​
111 render(() => <SolidQueryDevtools client={queryClient} />)
112​
113 expect(setErrorTypes).toHaveBeenCalledWith([])
114 })
115​
116 it('should forward "theme" to the devtools instance', () => {
117 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
118 const queryClient = new QueryClient()
119​
120 render(() => <SolidQueryDevtools client={queryClient} theme="dark" />)
121​
122 expect(setTheme).toHaveBeenCalledWith('dark')
123 })
124​
125 it('should default "theme" to "system" when the prop is omitted', () => {
126 const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme')
127 const queryClient = new QueryClient()
128​
129 render(() => <SolidQueryDevtools client={queryClient} />)
130​
131 expect(setTheme).toHaveBeenCalledWith('system')
132 })
133​
134 it('should forward the resolved "QueryClient" via "setClient"', () => {
135 const setClient = vi.spyOn(TanstackQueryDevtools.prototype, 'setClient')
136 const queryClient = new QueryClient()
137​
138 render(() => <SolidQueryDevtools client={queryClient} />)
139​
140 expect(setClient).toHaveBeenCalledWith(queryClient)
141 })
142​
143 it('should call "unmount" on the devtools instance when the component unmounts', () => {
144 const unmount = vi.spyOn(TanstackQueryDevtools.prototype, 'unmount')
145 const queryClient = new QueryClient()
146​
147 const { unmount: unmountComponent } = render(() => (
148 <SolidQueryDevtools client={queryClient} />
149 ))
150 unmountComponent()
151​
152 expect(unmount).toHaveBeenCalled()
153 })
154​
155 it('should return null in non-development environments', async () => {
156 vi.doMock('solid-js/web', async (importOriginal) => {
157 const actual = await importOriginal()
158 return Object.assign({}, actual, { isDev: false })
159 })
160 vi.resetModules()
161​
162 try {
163 const { SolidQueryDevtools: ProductionDevtools } = await import('..')
164 expect(ProductionDevtools({})).toBeNull()
165 } finally {
166 vi.doUnmock('solid-js/web')
167 vi.resetModules()
168 }
169 })
170})