Skip to content

Commit d87ea5e

Browse files
committed
Fix folder test warnings
1 parent 9863204 commit d87ea5e

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

src/components/Folder.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default function Folder({ source, config }: FolderProps) {
6666
searchElement?.addEventListener('keyup', handleKeyup)
6767
// Clean up event listener
6868
return () => searchElement?.removeEventListener('keyup', handleKeyup)
69-
})
69+
}, [filtered, source.prefix])
7070

7171
// Jump to search box if user types '/'
7272
useEffect(() => {
@@ -80,7 +80,7 @@ export default function Folder({ source, config }: FolderProps) {
8080
}
8181
document.addEventListener('keydown', handleKeydown)
8282
return () => { document.removeEventListener('keydown', handleKeydown) }
83-
})
83+
}, [])
8484

8585
return <Layout error={error} title={source.prefix}>
8686
<Breadcrumb source={source} config={config}>

test/components/Folder.test.tsx

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

test/components/Markdown.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ describe('Markdown with nested elements', () => {
283283
const text = '![alt'
284284
const { container } = render(<Markdown text={text} />)
285285

286-
console.log(container.innerHTML)
287286
expect(container.textContent).toBe('![alt')
288287
expect(container.querySelector('img')).toBeNull()
289288
})

0 commit comments

Comments
 (0)