@@ -46,15 +46,16 @@ describe('Folder Component', () => {
4646
4747 it ( 'displays the spinner while loading' , async ( ) => {
4848 vi . mocked ( fetch ) . mockResolvedValueOnce ( {
49- json : ( ) => Promise . resolve ( [ ] ) ,
49+ // resolve in 50ms
50+ json : ( ) => new Promise ( resolve => setTimeout ( ( ) => { resolve ( [ ] ) } , 50 ) ) ,
5051 ok : true ,
5152 } as Response )
5253
5354 const source = getHyperparamSource ( '' , { endpoint } )
5455 assert ( source ?. kind === 'directory' )
5556
5657 const { container } = await act ( ( ) => render ( < Folder source = { source } /> ) )
57- expect ( container . querySelector ( '.spinner' ) ) . toBeDefined ( )
58+ expect ( container . querySelector ( '.spinner' ) ) . toBeTruthy ( )
5859 } )
5960
6061 it ( 'handles file listing errors' , async ( ) => {
@@ -111,8 +112,10 @@ describe('Folder Component', () => {
111112 const { getByPlaceholderText, getByText, queryByText } = render ( < Folder source = { dirSource } /> )
112113
113114 // Type a search query
114- const searchInput = getByPlaceholderText ( 'Search...' )
115- fireEvent . keyUp ( searchInput , { target : { value : 'file1' } } )
115+ const searchInput = getByPlaceholderText ( 'Search...' ) as HTMLInputElement
116+ act ( ( ) => {
117+ fireEvent . keyUp ( searchInput , { target : { value : 'file1' } } )
118+ } )
116119
117120 // Only matching files are displayed
118121 await waitFor ( ( ) => {
@@ -122,7 +125,10 @@ describe('Folder Component', () => {
122125 } )
123126
124127 // Clear search with escape key
125- fireEvent . keyUp ( searchInput , { key : 'Escape' } )
128+ act ( ( ) => {
129+ fireEvent . keyUp ( searchInput , { key : 'Escape' } )
130+ } )
131+
126132 await waitFor ( ( ) => {
127133 expect ( getByText ( 'file1.txt' ) ) . toBeDefined ( )
128134 expect ( getByText ( 'folder1/' ) ) . toBeDefined ( )
@@ -151,19 +157,23 @@ describe('Folder Component', () => {
151157 const { getByPlaceholderText, getByText } = render ( < Folder source = { dirSource } /> )
152158
153159 // Type a search query and hit enter
154- const searchInput = getByPlaceholderText ( 'Search...' )
160+ const searchInput = getByPlaceholderText ( 'Search...' ) as HTMLInputElement
155161 act ( ( ) => {
156162 fireEvent . keyUp ( searchInput , { target : { value : 'file1' } } )
157163 } )
158164
159165 await waitFor ( ( ) => {
160166 expect ( getByText ( 'file1.txt' ) ) . toBeDefined ( )
161167 } )
162- fireEvent . keyUp ( searchInput , { key : 'Enter' } )
168+
169+ act ( ( ) => {
170+ fireEvent . keyUp ( searchInput , { key : 'Enter' } )
171+ } )
172+
163173 expect ( location . href ) . toBe ( '/files?key=file1.txt' )
164174 } )
165175
166- it ( 'jumps to search box when user types /' , ( ) => {
176+ it ( 'jumps to search box when user types /' , async ( ) => {
167177 const dirSource : DirSource = {
168178 sourceId : 'test-source' ,
169179 sourceParts : [ { text : 'test-source' , sourceId : 'test-source' } ] ,
@@ -173,21 +183,34 @@ describe('Folder Component', () => {
173183 }
174184 const { getByPlaceholderText } = render ( < Folder source = { dirSource } /> )
175185
186+ // Wait for component to settle
187+ await waitFor ( ( ) => {
188+ expect ( fetch ) . toHaveBeenCalled ( )
189+ } )
190+
176191 const searchInput = getByPlaceholderText ( 'Search...' ) as HTMLInputElement
192+
177193 // Typing / should focus the search box
178- fireEvent . keyDown ( document . body , { key : '/' } )
194+ act ( ( ) => {
195+ fireEvent . keyDown ( document . body , { key : '/' } )
196+ } )
179197 expect ( document . activeElement ) . toBe ( searchInput )
180198
181199 // Typing inside the search box should work including /
182200 act ( ( ) => {
183201 fireEvent . keyUp ( searchInput , { target : { value : 'file1/' } } )
184- expect ( searchInput . value ) . toBe ( 'file1/' )
185202 } )
203+ expect ( searchInput . value ) . toBe ( 'file1/' )
186204
187205 // Unfocus and re-focus should select all text in search box
188- searchInput . blur ( )
206+ act ( ( ) => {
207+ searchInput . blur ( )
208+ } )
189209 expect ( document . activeElement ) . not . toBe ( searchInput )
190- fireEvent . keyDown ( document . body , { key : '/' } )
210+
211+ act ( ( ) => {
212+ fireEvent . keyDown ( document . body , { key : '/' } )
213+ } )
191214 expect ( document . activeElement ) . toBe ( searchInput )
192215 expect ( searchInput . selectionStart ) . toBe ( 0 )
193216 expect ( searchInput . selectionEnd ) . toBe ( searchInput . value . length )
0 commit comments