@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
22import { render } from '@testing-library/preact'
33import { QueryClient , QueryClientProvider } from '@tanstack/preact-query'
44import { TanstackQueryDevtools } from '@tanstack/query-devtools'
5+ import type { PreactQueryDevtools as PreactQueryDevtoolsComponent } from '../PreactQueryDevtools'
56
67const mountMock = vi . fn ( )
78const unmountMock = vi . fn ( )
@@ -26,22 +27,22 @@ vi.mock('@tanstack/query-devtools', () => ({
2627} ) )
2728
2829describe ( 'PreactQueryDevtools' , ( ) => {
29- beforeEach ( ( ) => {
30+ let PreactQueryDevtools : typeof PreactQueryDevtoolsComponent
31+ let queryClient : QueryClient
32+
33+ beforeEach ( async ( ) => {
3034 vi . clearAllMocks ( )
35+ ; ( { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' ) )
36+ queryClient = new QueryClient ( )
3137 } )
3238
33- it ( 'should throw an error if no query client has been set' , async ( ) => {
34- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
35-
39+ it ( 'should throw an error if no query client has been set' , ( ) => {
3640 expect ( ( ) => render ( < PreactQueryDevtools /> ) ) . 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 { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
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('PreactQueryDevtools', () => {
5253 expect ( mountMock ) . toHaveBeenCalled ( )
5354 } )
5455
55- it ( 'should not throw an error if query client is provided via props' , async ( ) => {
56- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
57- const queryClient = new QueryClient ( )
58-
56+ it ( 'should not throw an error if query client is provided via props' , ( ) => {
5957 expect ( ( ) =>
6058 render ( < PreactQueryDevtools client = { queryClient } /> ) ,
6159 ) . not . toThrow ( )
6260 expect ( mountMock ) . toHaveBeenCalled ( )
6361 } )
6462
65- it ( 'should forward "buttonPosition" to the devtools instance' , async ( ) => {
66- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
67- const queryClient = new QueryClient ( )
68-
63+ it ( 'should forward "buttonPosition" to the devtools instance' , ( ) => {
6964 render (
7065 < PreactQueryDevtools 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 { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
78- const queryClient = new QueryClient ( )
79-
71+ it ( 'should forward "position" to the devtools instance' , ( ) => {
8072 render ( < PreactQueryDevtools client = { queryClient } position = "left" /> )
8173
8274 expect ( setPositionMock ) . toHaveBeenCalledWith ( 'left' )
8375 } )
8476
85- it ( 'should forward "initialIsOpen" to the devtools instance' , async ( ) => {
86- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
87- const queryClient = new QueryClient ( )
88-
77+ it ( 'should forward "initialIsOpen" to the devtools instance' , ( ) => {
8978 render ( < PreactQueryDevtools 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 { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
96- const queryClient = new QueryClient ( )
97-
83+ it ( 'should default "initialIsOpen" to "false" when the prop is omitted' , ( ) => {
9884 render ( < PreactQueryDevtools client = { queryClient } /> )
9985
10086 expect ( setInitialIsOpenMock ) . toHaveBeenCalledWith ( false )
10187 } )
10288
103- it ( 'should forward "errorTypes" to the devtools instance' , async ( ) => {
104- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
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('PreactQueryDevtools', () => {
11296 expect ( setErrorTypesMock ) . toHaveBeenCalledWith ( errorTypes )
11397 } )
11498
115- it ( 'should default "errorTypes" to an empty array when the prop is omitted' , async ( ) => {
116- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
117- const queryClient = new QueryClient ( )
118-
99+ it ( 'should default "errorTypes" to an empty array when the prop is omitted' , ( ) => {
119100 render ( < PreactQueryDevtools client = { queryClient } /> )
120101
121102 expect ( setErrorTypesMock ) . toHaveBeenCalledWith ( [ ] )
122103 } )
123104
124- it ( 'should forward "theme" to the devtools instance' , async ( ) => {
125- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
126- const queryClient = new QueryClient ( )
127-
105+ it ( 'should forward "theme" to the devtools instance' , ( ) => {
128106 render ( < PreactQueryDevtools client = { queryClient } theme = "dark" /> )
129107
130108 expect ( setThemeMock ) . toHaveBeenCalledWith ( 'dark' )
131109 } )
132110
133- it ( 'should forward the resolved "QueryClient" via "setClient"' , async ( ) => {
134- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
135- const queryClient = new QueryClient ( )
136-
111+ it ( 'should forward the resolved "QueryClient" via "setClient"' , ( ) => {
137112 render ( < PreactQueryDevtools client = { queryClient } /> )
138113
139114 expect ( setClientMock ) . toHaveBeenCalledWith ( queryClient )
140115 } )
141116
142- it ( 'should forward "styleNonce" to the devtools constructor' , async ( ) => {
143- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
144- const queryClient = new QueryClient ( )
145-
117+ it ( 'should forward "styleNonce" to the devtools constructor' , ( ) => {
146118 render ( < PreactQueryDevtools 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 { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
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('PreactQueryDevtools', () => {
169139 )
170140 } )
171141
172- it ( 'should forward "hideDisabledQueries" to the devtools constructor' , async ( ) => {
173- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
174- const queryClient = new QueryClient ( )
175-
142+ it ( 'should forward "hideDisabledQueries" to the devtools constructor' , ( ) => {
176143 render (
177144 < PreactQueryDevtools client = { queryClient } hideDisabledQueries = { true } /> ,
178145 )
@@ -182,10 +149,7 @@ describe('PreactQueryDevtools', () => {
182149 )
183150 } )
184151
185- it ( 'should forward a "buttonPosition" change to the devtools instance after mount' , async ( ) => {
186- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
187- const queryClient = new QueryClient ( )
188-
152+ it ( 'should forward a "buttonPosition" change to the devtools instance after mount' , ( ) => {
189153 const { rerender } = render (
190154 < PreactQueryDevtools
191155 client = { queryClient }
@@ -201,10 +165,7 @@ describe('PreactQueryDevtools', () => {
201165 expect ( setButtonPositionMock ) . toHaveBeenCalledWith ( 'top-left' )
202166 } )
203167
204- it ( 'should forward a "position" change to the devtools instance after mount' , async ( ) => {
205- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
206- const queryClient = new QueryClient ( )
207-
168+ it ( 'should forward a "position" change to the devtools instance after mount' , ( ) => {
208169 const { rerender } = render (
209170 < PreactQueryDevtools client = { queryClient } position = "bottom" /> ,
210171 )
@@ -215,10 +176,7 @@ describe('PreactQueryDevtools', () => {
215176 expect ( setPositionMock ) . toHaveBeenCalledWith ( 'top' )
216177 } )
217178
218- it ( 'should forward an "initialIsOpen" change to the devtools instance after mount' , async ( ) => {
219- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
220- const queryClient = new QueryClient ( )
221-
179+ it ( 'should forward an "initialIsOpen" change to the devtools instance after mount' , ( ) => {
222180 const { rerender } = render (
223181 < PreactQueryDevtools client = { queryClient } initialIsOpen = { false } /> ,
224182 )
@@ -229,10 +187,7 @@ describe('PreactQueryDevtools', () => {
229187 expect ( setInitialIsOpenMock ) . toHaveBeenCalledWith ( true )
230188 } )
231189
232- it ( 'should forward an "errorTypes" change to the devtools instance after mount' , async ( ) => {
233- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
234- const queryClient = new QueryClient ( )
235-
190+ it ( 'should forward an "errorTypes" change to the devtools instance after mount' , ( ) => {
236191 const { rerender } = render (
237192 < PreactQueryDevtools client = { queryClient } errorTypes = { [ ] } /> ,
238193 )
@@ -248,10 +203,7 @@ describe('PreactQueryDevtools', () => {
248203 expect ( setErrorTypesMock ) . toHaveBeenCalledWith ( errorTypes )
249204 } )
250205
251- it ( 'should forward a "theme" change to the devtools instance after mount' , async ( ) => {
252- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
253- const queryClient = new QueryClient ( )
254-
206+ it ( 'should forward a "theme" change to the devtools instance after mount' , ( ) => {
255207 const { rerender } = render (
256208 < PreactQueryDevtools client = { queryClient } theme = "light" /> ,
257209 )
@@ -262,10 +214,7 @@ describe('PreactQueryDevtools', () => {
262214 expect ( setThemeMock ) . toHaveBeenCalledWith ( 'dark' )
263215 } )
264216
265- it ( 'should call "unmount" on the devtools instance when the component unmounts' , async ( ) => {
266- const { PreactQueryDevtools } = await import ( '../PreactQueryDevtools' )
267- const queryClient = new QueryClient ( )
268-
217+ it ( 'should call "unmount" on the devtools instance when the component unmounts' , ( ) => {
269218 const { unmount } = render ( < PreactQueryDevtools client = { queryClient } /> )
270219 unmount ( )
271220
@@ -277,8 +226,8 @@ describe('PreactQueryDevtools', () => {
277226 vi . resetModules ( )
278227
279228 try {
280- const { PreactQueryDevtools } = await import ( '..' )
281- expect ( PreactQueryDevtools ( { } ) ) . toBeNull ( )
229+ const { PreactQueryDevtools : ProductionDevtools } = await import ( '..' )
230+ expect ( ProductionDevtools ( { } ) ) . toBeNull ( )
282231 } finally {
283232 vi . unstubAllEnvs ( )
284233 vi . resetModules ( )
0 commit comments