@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
22import { render } from '@testing-library/react'
33import { QueryClient , QueryClientProvider } from '@tanstack/react-query'
44import { TanstackQueryDevtools } from '@tanstack/query-devtools'
5+ import type { ReactQueryDevtools as ReactQueryDevtoolsComponent } from '../ReactQueryDevtools'
56
67const mountMock = vi . fn ( )
78const unmountMock = vi . fn ( )
@@ -26,22 +27,22 @@ vi.mock('@tanstack/query-devtools', () => ({
2627} ) )
2728
2829describe ( 'ReactQueryDevtools' , ( ) => {
29- beforeEach ( ( ) => {
30+ let ReactQueryDevtools : typeof ReactQueryDevtoolsComponent
31+ let queryClient : QueryClient
32+
33+ beforeEach ( async ( ) => {
3034 vi . clearAllMocks ( )
35+ ; ( { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' ) )
36+ queryClient = new QueryClient ( )
3137 } )
3238
33- it ( 'should throw an error if no query client has been set' , async ( ) => {
34- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
35-
39+ it ( 'should throw an error if no query client has been set' , ( ) => {
3640 expect ( ( ) => render ( < ReactQueryDevtools /> ) ) . toThrow (
3741 'No QueryClient set, use QueryClientProvider to set one' ,
3842 )
3943 } )
4044
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+ it ( 'should not throw an error if query client is provided via context' , ( ) => {
4546 expect ( ( ) =>
4647 render (
4748 < QueryClientProvider client = { queryClient } >
@@ -52,57 +53,40 @@ describe('ReactQueryDevtools', () => {
5253 expect ( mountMock ) . toHaveBeenCalled ( )
5354 } )
5455
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-
56+ it ( 'should not throw an error if query client is provided via props' , ( ) => {
5957 expect ( ( ) =>
6058 render ( < ReactQueryDevtools client = { queryClient } /> ) ,
6159 ) . not . toThrow ( )
6260 expect ( mountMock ) . toHaveBeenCalled ( )
6361 } )
6462
65- it ( 'should forward "buttonPosition" to the devtools instance' , async ( ) => {
66- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
67- const queryClient = new QueryClient ( )
68-
63+ it ( 'should forward "buttonPosition" to the devtools instance' , ( ) => {
6964 render (
7065 < ReactQueryDevtools client = { queryClient } buttonPosition = "top-left" /> ,
7166 )
7267
7368 expect ( setButtonPositionMock ) . toHaveBeenCalledWith ( 'top-left' )
7469 } )
7570
76- it ( 'should forward "position" to the devtools instance' , async ( ) => {
77- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
78- const queryClient = new QueryClient ( )
79-
71+ it ( 'should forward "position" to the devtools instance' , ( ) => {
8072 render ( < ReactQueryDevtools client = { queryClient } position = "left" /> )
8173
8274 expect ( setPositionMock ) . toHaveBeenCalledWith ( 'left' )
8375 } )
8476
85- it ( 'should forward "initialIsOpen" to the devtools instance' , async ( ) => {
86- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
87- const queryClient = new QueryClient ( )
88-
77+ it ( 'should forward "initialIsOpen" to the devtools instance' , ( ) => {
8978 render ( < ReactQueryDevtools client = { queryClient } initialIsOpen = { true } /> )
9079
9180 expect ( setInitialIsOpenMock ) . toHaveBeenCalledWith ( true )
9281 } )
9382
94- it ( 'should default "initialIsOpen" to "false" when the prop is omitted' , async ( ) => {
95- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
96- const queryClient = new QueryClient ( )
97-
83+ it ( 'should default "initialIsOpen" to "false" when the prop is omitted' , ( ) => {
9884 render ( < ReactQueryDevtools client = { queryClient } /> )
9985
10086 expect ( setInitialIsOpenMock ) . toHaveBeenCalledWith ( false )
10187 } )
10288
103- it ( 'should forward "errorTypes" to the devtools instance' , async ( ) => {
104- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
105- const queryClient = new QueryClient ( )
89+ it ( 'should forward "errorTypes" to the devtools instance' , ( ) => {
10690 const errorTypes = [
10791 { name : 'Network' , initializer : ( ) => new Error ( 'Network' ) } ,
10892 ]
@@ -112,47 +96,33 @@ describe('ReactQueryDevtools', () => {
11296 expect ( setErrorTypesMock ) . toHaveBeenCalledWith ( errorTypes )
11397 } )
11498
115- it ( 'should default "errorTypes" to an empty array when the prop is omitted' , async ( ) => {
116- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
117- const queryClient = new QueryClient ( )
118-
99+ it ( 'should default "errorTypes" to an empty array when the prop is omitted' , ( ) => {
119100 render ( < ReactQueryDevtools client = { queryClient } /> )
120101
121102 expect ( setErrorTypesMock ) . toHaveBeenCalledWith ( [ ] )
122103 } )
123104
124- it ( 'should forward "theme" to the devtools instance' , async ( ) => {
125- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
126- const queryClient = new QueryClient ( )
127-
105+ it ( 'should forward "theme" to the devtools instance' , ( ) => {
128106 render ( < ReactQueryDevtools client = { queryClient } theme = "dark" /> )
129107
130108 expect ( setThemeMock ) . toHaveBeenCalledWith ( 'dark' )
131109 } )
132110
133- it ( 'should forward the resolved "QueryClient" via "setClient"' , async ( ) => {
134- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
135- const queryClient = new QueryClient ( )
136-
111+ it ( 'should forward the resolved "QueryClient" via "setClient"' , ( ) => {
137112 render ( < ReactQueryDevtools client = { queryClient } /> )
138113
139114 expect ( setClientMock ) . toHaveBeenCalledWith ( queryClient )
140115 } )
141116
142- it ( 'should forward "styleNonce" to the devtools constructor' , async ( ) => {
143- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
144- const queryClient = new QueryClient ( )
145-
117+ it ( 'should forward "styleNonce" to the devtools constructor' , ( ) => {
146118 render ( < ReactQueryDevtools client = { queryClient } styleNonce = "abc" /> )
147119
148120 expect ( TanstackQueryDevtools ) . toHaveBeenCalledWith (
149121 expect . objectContaining ( { styleNonce : 'abc' } ) ,
150122 )
151123 } )
152124
153- it ( 'should forward "shadowDOMTarget" to the devtools constructor' , async ( ) => {
154- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
155- const queryClient = new QueryClient ( )
125+ it ( 'should forward "shadowDOMTarget" to the devtools constructor' , ( ) => {
156126 const shadowDOMTarget = document
157127 . createElement ( 'div' )
158128 . attachShadow ( { mode : 'open' } )
@@ -169,10 +139,7 @@ describe('ReactQueryDevtools', () => {
169139 )
170140 } )
171141
172- it ( 'should forward "hideDisabledQueries" to the devtools constructor' , async ( ) => {
173- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
174- const queryClient = new QueryClient ( )
175-
142+ it ( 'should forward "hideDisabledQueries" to the devtools constructor' , ( ) => {
176143 render (
177144 < ReactQueryDevtools client = { queryClient } hideDisabledQueries = { true } /> ,
178145 )
@@ -182,10 +149,7 @@ describe('ReactQueryDevtools', () => {
182149 )
183150 } )
184151
185- it ( 'should forward a "buttonPosition" change to the devtools instance after mount' , async ( ) => {
186- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
187- const queryClient = new QueryClient ( )
188-
152+ it ( 'should forward a "buttonPosition" change to the devtools instance after mount' , ( ) => {
189153 const { rerender } = render (
190154 < ReactQueryDevtools client = { queryClient } buttonPosition = "bottom-right" /> ,
191155 )
@@ -198,10 +162,7 @@ describe('ReactQueryDevtools', () => {
198162 expect ( setButtonPositionMock ) . toHaveBeenCalledWith ( 'top-left' )
199163 } )
200164
201- it ( 'should forward a "position" change to the devtools instance after mount' , async ( ) => {
202- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
203- const queryClient = new QueryClient ( )
204-
165+ it ( 'should forward a "position" change to the devtools instance after mount' , ( ) => {
205166 const { rerender } = render (
206167 < ReactQueryDevtools client = { queryClient } position = "bottom" /> ,
207168 )
@@ -212,10 +173,7 @@ describe('ReactQueryDevtools', () => {
212173 expect ( setPositionMock ) . toHaveBeenCalledWith ( 'top' )
213174 } )
214175
215- it ( 'should forward an "initialIsOpen" change to the devtools instance after mount' , async ( ) => {
216- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
217- const queryClient = new QueryClient ( )
218-
176+ it ( 'should forward an "initialIsOpen" change to the devtools instance after mount' , ( ) => {
219177 const { rerender } = render (
220178 < ReactQueryDevtools client = { queryClient } initialIsOpen = { false } /> ,
221179 )
@@ -226,10 +184,7 @@ describe('ReactQueryDevtools', () => {
226184 expect ( setInitialIsOpenMock ) . toHaveBeenCalledWith ( true )
227185 } )
228186
229- it ( 'should forward an "errorTypes" change to the devtools instance after mount' , async ( ) => {
230- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
231- const queryClient = new QueryClient ( )
232-
187+ it ( 'should forward an "errorTypes" change to the devtools instance after mount' , ( ) => {
233188 const { rerender } = render (
234189 < ReactQueryDevtools client = { queryClient } errorTypes = { [ ] } /> ,
235190 )
@@ -245,10 +200,7 @@ describe('ReactQueryDevtools', () => {
245200 expect ( setErrorTypesMock ) . toHaveBeenCalledWith ( errorTypes )
246201 } )
247202
248- it ( 'should forward a "theme" change to the devtools instance after mount' , async ( ) => {
249- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
250- const queryClient = new QueryClient ( )
251-
203+ it ( 'should forward a "theme" change to the devtools instance after mount' , ( ) => {
252204 const { rerender } = render (
253205 < ReactQueryDevtools client = { queryClient } theme = "light" /> ,
254206 )
@@ -259,10 +211,7 @@ describe('ReactQueryDevtools', () => {
259211 expect ( setThemeMock ) . toHaveBeenCalledWith ( 'dark' )
260212 } )
261213
262- it ( 'should call "unmount" on the devtools instance when the component unmounts' , async ( ) => {
263- const { ReactQueryDevtools } = await import ( '../ReactQueryDevtools' )
264- const queryClient = new QueryClient ( )
265-
214+ it ( 'should call "unmount" on the devtools instance when the component unmounts' , ( ) => {
266215 const { unmount } = render ( < ReactQueryDevtools client = { queryClient } /> )
267216 unmount ( )
268217
@@ -274,8 +223,8 @@ describe('ReactQueryDevtools', () => {
274223 vi . resetModules ( )
275224
276225 try {
277- const { ReactQueryDevtools } = await import ( '..' )
278- expect ( ReactQueryDevtools ( { } ) ) . toBeNull ( )
226+ const { ReactQueryDevtools : ProductionDevtools } = await import ( '..' )
227+ expect ( ProductionDevtools ( { } ) ) . toBeNull ( )
279228 } finally {
280229 vi . unstubAllEnvs ( )
281230 vi . resetModules ( )
0 commit comments