-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFolder.test.tsx
More file actions
195 lines (162 loc) · 7.03 KB
/
Folder.test.tsx
File metadata and controls
195 lines (162 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { fireEvent, render, waitFor } from '@testing-library/react'
import { strict as assert } from 'assert'
import React, { act } from 'react'
import { describe, expect, it, test, vi } from 'vitest'
import { DirSource, FileMetadata, Folder, HyperparamFileMetadata, RoutesConfig, getHyperparamSource } from '../../src/index.js'
const endpoint = 'http://localhost:3000'
const mockFiles: HyperparamFileMetadata[] = [
{ key: 'folder1/', lastModified: '2022-01-01T12:00:00Z' },
{ key: 'file1.txt', fileSize: 8196, lastModified: '2023-01-01T12:00:00Z' },
]
const config: RoutesConfig = {
routes: {
getSourceRouteUrl: ({ sourceId }) => `/files?key=${sourceId}`,
},
}
globalThis.fetch = vi.fn()
describe('Folder Component', () => {
test.for([
'',
'subfolder/',
])('fetches file data and displays files on mount', async (path) => {
vi.mocked(fetch).mockResolvedValueOnce({
json: () => Promise.resolve(mockFiles),
ok: true,
} as Response)
const source = getHyperparamSource(path, { endpoint })
assert(source?.kind === 'directory')
const { findByText, getByText } = render(<Folder source={source} config={config} />)
const folderLink = await findByText('folder1/')
expect(folderLink.closest('a')?.getAttribute('href')).toBe(`/files?key=${path}folder1/`)
expect(getByText('/')).toBeDefined()
const fileLink = getByText('file1.txt')
expect(fileLink.closest('a')?.getAttribute('href')).toBe(`/files?key=${path}file1.txt`)
expect(getByText('8.0 kb')).toBeDefined()
expect(getByText('1/1/2023')).toBeDefined()
})
it('displays the spinner while loading', async () => {
vi.mocked(fetch).mockResolvedValueOnce({
json: () => Promise.resolve([]),
ok: true,
} as Response)
const source = getHyperparamSource('', { endpoint })
assert(source?.kind === 'directory')
const { container } = await act(() => render(<Folder source={source} />))
expect(container.querySelector('.spinner')).toBeDefined()
})
it('handles file listing errors', async () => {
const errorMessage = 'Failed to fetch'
vi.mocked(fetch).mockResolvedValueOnce({
text: () => Promise.resolve(errorMessage),
ok: false,
} as Response)
const source = getHyperparamSource('test-prefix/', { endpoint })
assert(source?.kind === 'directory')
const { findByText, queryByText } = render(<Folder source={source} />)
await waitFor(() => { expect(fetch).toHaveBeenCalled() })
await findByText('Error: ' + errorMessage)
expect(queryByText('file1.txt')).toBeNull()
expect(queryByText('folder1/')).toBeNull()
})
it('renders breadcrumbs correctly', async () => {
vi.mocked(fetch).mockResolvedValueOnce({
json: () => Promise.resolve(mockFiles),
ok: true,
} as Response)
const source = getHyperparamSource('subdir1/subdir2/', { endpoint })
assert(source?.kind === 'directory')
const { findByText, getByText } = render(<Folder source={source} config={config} />)
await waitFor(() => { expect(fetch).toHaveBeenCalled() })
const subdir1Link = await findByText('subdir1/')
expect(subdir1Link.closest('a')?.getAttribute('href')).toBe('/files?key=subdir1/')
const subdir2Link = getByText('subdir2/')
expect(subdir2Link.closest('a')?.getAttribute('href')).toBe('/files?key=subdir1/subdir2/')
})
it('filters files based on search query', async () => {
const mockFiles: FileMetadata[] = [
{ sourceId: 'folder1', name: 'folder1/', kind: 'directory', lastModified: '2023-01-01T00:00:00Z' },
{ sourceId: 'file1.txt', name: 'file1.txt', kind: 'file', size: 8196, lastModified: '2023-01-01T00:00:00Z' },
{ sourceId: 'report.pdf', name: 'report.pdf', kind: 'file', size: 10240, lastModified: '2023-01-02T00:00:00Z' },
]
const dirSource: DirSource = {
sourceId: 'test-source',
sourceParts: [{ text: 'test-source', sourceId: 'test-source' }],
kind: 'directory',
prefix: '',
listFiles: () => Promise.resolve(mockFiles),
}
const { getByPlaceholderText, getByText, queryByText } = render(<Folder source={dirSource} />)
// Type a search query
const searchInput = getByPlaceholderText('Search...')
fireEvent.keyUp(searchInput, { target: { value: 'file1' } })
// Only matching files are displayed
await waitFor(() => {
expect(getByText('file1.txt')).toBeDefined()
expect(queryByText('folder1/')).toBeNull()
expect(queryByText('report.pdf')).toBeNull()
})
// Clear search with escape key
fireEvent.keyUp(searchInput, { key: 'Escape' })
await waitFor(() => {
expect(getByText('file1.txt')).toBeDefined()
expect(getByText('folder1/')).toBeDefined()
expect(getByText('report.pdf')).toBeDefined()
})
})
it('hitting enter on single search result navigates to file', async () => {
// Mock location.href
const location = { href: '' }
Object.defineProperty(window, 'location', {
writable: true,
value: location,
})
const mockFiles: FileMetadata[] = [
{ sourceId: 'file1.txt', name: 'file1.txt', kind: 'file', size: 8196, lastModified: '2023-01-01T00:00:00Z' },
{ sourceId: 'file2.txt', name: 'file2.txt', kind: 'file', size: 4096, lastModified: '2023-02-02T00:00:00Z' },
]
const dirSource: DirSource = {
sourceId: 'test-source',
sourceParts: [{ text: 'test-source', sourceId: 'test-source' }],
kind: 'directory',
prefix: '',
listFiles: () => Promise.resolve(mockFiles),
}
const { getByPlaceholderText, getByText } = render(<Folder source={dirSource} />)
// Type a search query and hit enter
const searchInput = getByPlaceholderText('Search...')
act(() => {
fireEvent.keyUp(searchInput, { target: { value: 'file1' } })
})
await waitFor(() => {
expect(getByText('file1.txt')).toBeDefined()
})
fireEvent.keyUp(searchInput, { key: 'Enter' })
expect(location.href).toBe('/files?key=file1.txt')
})
it('jumps to search box when user types /', () => {
const dirSource: DirSource = {
sourceId: 'test-source',
sourceParts: [{ text: 'test-source', sourceId: 'test-source' }],
kind: 'directory',
prefix: '',
listFiles: () => Promise.resolve([]),
}
const { getByPlaceholderText } = render(<Folder source={dirSource} />)
const searchInput = getByPlaceholderText('Search...') as HTMLInputElement
// Typing / should focus the search box
fireEvent.keyDown(document.body, { key: '/' })
expect(document.activeElement).toBe(searchInput)
// Typing inside the search box should work including /
act(() => {
fireEvent.keyUp(searchInput, { target: { value: 'file1/' } })
expect(searchInput.value).toBe('file1/')
})
// Unfocus and re-focus should select all text in search box
searchInput.blur()
expect(document.activeElement).not.toBe(searchInput)
fireEvent.keyDown(document.body, { key: '/' })
expect(document.activeElement).toBe(searchInput)
expect(searchInput.selectionStart).toBe(0)
expect(searchInput.selectionEnd).toBe(searchInput.value.length)
})
})