1- import { fireEvent , render , waitFor } from '@testing-library/react'
1+ import { render , waitFor } from '@testing-library/react'
22import { strict as assert } from 'assert'
33import { act } from 'react'
4- import { describe , expect , it , test , vi } from 'vitest'
4+ import { beforeEach , describe , expect , it , test , vi } from 'vitest'
55import { Config , ConfigProvider } from '../../hooks/useConfig.js'
66import { DirSource , FileMetadata , HyperparamFileMetadata , getHyperparamSource } from '../../lib/sources/index.js'
77import Folder from './Folder.js'
@@ -22,6 +22,10 @@ const config: Config = {
2222globalThis . fetch = vi . fn ( )
2323
2424describe ( 'Folder Component' , ( ) => {
25+ beforeEach ( ( ) => {
26+ vi . clearAllMocks ( )
27+ } )
28+
2529 test . for ( [
2630 '' ,
2731 'subfolder/' ,
@@ -100,19 +104,16 @@ describe('Folder Component', () => {
100104
101105 // Type a search query
102106 const searchInput = getByPlaceholderText ( 'Search...' ) as HTMLInputElement
103- act ( ( ) => {
104- fireEvent . keyUp ( searchInput , { target : { value : 'file1' } } )
105- } )
107+ const user = userEvent . setup ( )
108+ await user . type ( searchInput , 'file1' )
106109
107110 // Only matching files are displayed
108111 await findByText ( 'file1.txt' )
109112 expect ( queryByText ( 'folder1/' ) ) . toBeNull ( )
110113 expect ( queryByText ( 'report.pdf' ) ) . toBeNull ( )
111114
112115 // Clear search with escape key
113- act ( ( ) => {
114- fireEvent . keyUp ( searchInput , { key : 'Escape' } )
115- } )
116+ await user . type ( searchInput , '{Escape}' )
116117
117118 await findByText ( 'report.pdf' )
118119 getByText ( 'folder1/' )
@@ -149,13 +150,16 @@ describe('Folder Component', () => {
149150 expect ( location . href ) . toBe ( '/files?key=file1.txt' )
150151 } )
151152
152- it ( 'jumps to search box when user types /' , async ( ) => {
153+ it ( 'jumps to search box when user types / while the body is focused ' , async ( ) => {
153154 const dirSource : DirSource = {
154155 sourceId : 'test-source' ,
155156 sourceParts : [ { text : 'test-source' , sourceId : 'test-source' } ] ,
156157 kind : 'directory' ,
157158 prefix : '' ,
158- listFiles : ( ) => Promise . resolve ( [ ] ) ,
159+ listFiles : async ( ) => {
160+ await fetch ( 'something' ) // to ensure we wait for loading
161+ return [ ]
162+ } ,
159163 }
160164 const { getByPlaceholderText } = render ( < Folder source = { dirSource } /> )
161165
@@ -165,30 +169,33 @@ describe('Folder Component', () => {
165169 } )
166170
167171 const searchInput = getByPlaceholderText ( 'Search...' ) as HTMLInputElement
172+ const user = userEvent . setup ( )
168173
169- // Typing / should focus the search box
170- act ( ( ) => {
171- fireEvent . keyDown ( document . body , { key : '/' } )
172- } )
174+ // By default, the search box is already focused in this test
173175 expect ( document . activeElement ) . toBe ( searchInput )
174176
175177 // Typing inside the search box should work including /
176- act ( ( ) => {
177- fireEvent . keyUp ( searchInput , { target : { value : 'file1/' } } )
178- } )
178+ await user . type ( searchInput , 'file1/' )
179179 expect ( searchInput . value ) . toBe ( 'file1/' )
180180
181181 // Unfocus and re-focus should select all text in search box
182182 act ( ( ) => {
183183 searchInput . blur ( )
184184 } )
185185 expect ( document . activeElement ) . not . toBe ( searchInput )
186+ expect ( document . activeElement ) . toBe ( document . body )
186187
187- act ( ( ) => {
188- fireEvent . keyDown ( document . body , { key : '/' } )
189- } )
188+ await user . keyboard ( '/' )
190189 expect ( document . activeElement ) . toBe ( searchInput )
191190 expect ( searchInput . selectionStart ) . toBe ( 0 )
192191 expect ( searchInput . selectionEnd ) . toBe ( searchInput . value . length )
192+
193+ // Focus another element and try again: it does not focus the search box
194+ await user . tab ( )
195+ expect ( document . activeElement ) . not . toBe ( searchInput )
196+ expect ( document . activeElement ) . not . toBe ( document . body )
197+
198+ await user . keyboard ( '/' )
199+ expect ( document . activeElement ) . not . toBe ( searchInput )
193200 } )
194201} )
0 commit comments