-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSimpleFilesTable.test.tsx
More file actions
507 lines (432 loc) · 15.7 KB
/
Copy pathSimpleFilesTable.test.tsx
File metadata and controls
507 lines (432 loc) · 15.7 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
import {
type UploadModalContextType,
UploadModalContext,
} from '@nemo/common/src/components/UploadModal/Context/useUploadModalContext';
import {
UploadModalState,
initialState,
} from '@nemo/common/src/components/UploadModal/Context/useUploadModalReducer';
import { SimpleFilesTable } from '@nemo/common/src/components/UploadModal/SimpleFilesTable';
import { UploadFile } from '@nemo/common/src/components/UploadModal/types';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ReactNode } from 'react';
const createWrapper = (
stateOverrides?: Partial<UploadModalState>,
dispatch?: ReturnType<typeof vi.fn>
) => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
});
const mockDispatch = dispatch || vi.fn();
const state: UploadModalState = {
...initialState,
...stateOverrides,
};
return ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>
<UploadModalContext.Provider value={[state, mockDispatch] as UploadModalContextType}>
{children}
</UploadModalContext.Provider>
</QueryClientProvider>
);
};
describe('SimpleFilesTable', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const mockNewFiles: UploadFile[] = [
{ id: 'file-1', type: 'new', file: new File(['content1'], 'file1.jsonl') },
{ id: 'file-2', type: 'new', file: new File(['content2'], 'file2.jsonl') },
];
const mockExistingFiles: UploadFile[] = [
{
id: 'existing-1',
type: 'existing',
file: {
path: 'dataset/file1.jsonl',
file_ref: 'ref1',
size: 1024,
file_url: 'https://example.com/file1.jsonl',
},
},
{
id: 'existing-2',
type: 'existing',
file: {
path: 'dataset/file2.jsonl',
file_ref: 'ref2',
size: 2048,
file_url: 'https://example.com/file2.jsonl',
},
},
];
describe('rendering', () => {
it('renders table with column headers', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }),
});
expect(screen.getByText('Name')).toBeInTheDocument();
expect(screen.getByText('Size')).toBeInTheDocument();
});
it('renders table with new files from context', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }),
});
expect(screen.getByText('file1.jsonl')).toBeInTheDocument();
expect(screen.getByText('file2.jsonl')).toBeInTheDocument();
});
it('renders table with existing files showing path', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockExistingFiles }),
});
expect(screen.getByText('dataset/file1.jsonl')).toBeInTheDocument();
expect(screen.getByText('dataset/file2.jsonl')).toBeInTheDocument();
});
it('displays file sizes in formatted form', () => {
// Use ``.json`` extensions so the default ``acceptableFileTypes``
// filter (``['.json', '.jsonl']`` from the reducer's initial state)
// doesn't hide these rows from the rendered table.
const filesWithSize: UploadFile[] = [
{ id: 'file-1', type: 'new', file: new File(['x'.repeat(1024)], 'small.json') },
{
id: 'file-2',
type: 'existing',
file: {
path: 'large.json',
file_ref: 'ref1',
size: 1048576,
file_url: 'https://example.com/large.json',
},
},
];
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: filesWithSize }),
});
expect(screen.getByText('1 kB')).toBeInTheDocument();
expect(screen.getByText('1 MB')).toBeInTheDocument();
});
describe('multi-select (allowMultipleFileSelection=true)', () => {
it('renders checkboxes for each file', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles, allowMultipleFileSelection: true }),
});
const checkboxes = screen.getAllByRole('checkbox');
expect(checkboxes).toHaveLength(2);
});
it('renders empty table when no files', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: [], allowMultipleFileSelection: true }),
});
// Should still render headers
expect(screen.getByText('Name')).toBeInTheDocument();
expect(screen.getByText('Size')).toBeInTheDocument();
// But no checkboxes
expect(screen.queryByRole('checkbox')).not.toBeInTheDocument();
});
});
});
describe('file selection', () => {
describe('multi-select (allowMultipleFileSelection=true)', () => {
it('shows selected files as checked', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockNewFiles,
selectedFiles: [mockNewFiles[0]],
allowMultipleFileSelection: true,
}),
});
const checkboxes = screen.getAllByRole('checkbox');
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).not.toBeChecked();
});
it('shows all files as unchecked when none selected', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockNewFiles,
selectedFiles: [],
allowMultipleFileSelection: true,
}),
});
const checkboxes = screen.getAllByRole('checkbox');
checkboxes.forEach((checkbox) => {
expect(checkbox).not.toBeChecked();
});
});
it('can show multiple files as selected', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockNewFiles,
selectedFiles: [mockNewFiles[0], mockNewFiles[1]],
allowMultipleFileSelection: true,
}),
});
const checkboxes = screen.getAllByRole('checkbox');
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).toBeChecked();
});
it('dispatches TOGGLE_FILE_SELECTION when checkbox is clicked', async () => {
const user = userEvent.setup();
const mockDispatch = vi.fn();
render(<SimpleFilesTable />, {
wrapper: createWrapper(
{ files: mockNewFiles, allowMultipleFileSelection: true },
mockDispatch
),
});
const checkboxes = screen.getAllByRole('checkbox');
await user.click(checkboxes[0]);
expect(mockDispatch).toHaveBeenCalledWith({
type: 'TOGGLE_FILE_SELECTION',
payload: mockNewFiles[0],
});
});
it('dispatches correct file payload for existing files', async () => {
const user = userEvent.setup();
const mockDispatch = vi.fn();
render(<SimpleFilesTable />, {
wrapper: createWrapper(
{ files: mockExistingFiles, allowMultipleFileSelection: true },
mockDispatch
),
});
const checkboxes = screen.getAllByRole('checkbox');
await user.click(checkboxes[0]);
expect(mockDispatch).toHaveBeenCalledWith({
type: 'TOGGLE_FILE_SELECTION',
payload: mockExistingFiles[0],
});
});
it('exposes the file name as the accessible name of each checkbox', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockNewFiles,
allowMultipleFileSelection: true,
}),
});
expect(screen.getByRole('checkbox', { name: 'file1.jsonl' })).toBeInTheDocument();
expect(screen.getByRole('checkbox', { name: 'file2.jsonl' })).toBeInTheDocument();
});
});
});
describe('error handling', () => {
it('displays error message when file error is present', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockNewFiles,
errors: { file: 'File is required' },
}),
});
expect(screen.getByText('File is required')).toBeInTheDocument();
});
it('does not show error when errors object is empty', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockNewFiles,
errors: {},
}),
});
expect(screen.queryByText(/is required/i)).not.toBeInTheDocument();
});
});
describe('file type handling', () => {
it('displays file name for new files', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }),
});
expect(screen.getByText('file1.jsonl')).toBeInTheDocument();
});
it('displays file path for existing files', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockExistingFiles }),
});
expect(screen.getByText('dataset/file1.jsonl')).toBeInTheDocument();
});
it('handles mixed new and existing files', () => {
const mixedFiles = [...mockNewFiles, ...mockExistingFiles];
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mixedFiles }),
});
// New files show name
expect(screen.getByText('file1.jsonl')).toBeInTheDocument();
// Existing files show path
expect(screen.getByText('dataset/file1.jsonl')).toBeInTheDocument();
});
it('explains why unsupported files are disabled', () => {
const files: UploadFile[] = [
{
id: 'config',
type: 'existing',
file: {
path: 'eval/config.yaml',
file_ref: 'ref-config',
size: 100,
file_url: 'https://example.com/config.yaml',
},
},
{
id: 'notes',
type: 'existing',
file: {
path: 'eval/notes.txt',
file_ref: 'ref-notes',
size: 100,
file_url: 'https://example.com/notes.txt',
},
},
];
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files,
acceptableFileTypes: ['.yml', '.yaml'],
invalidFileMode: 'disable',
}),
});
expect(screen.getByRole('radio', { name: 'eval/config.yaml' })).not.toBeDisabled();
expect(screen.getByRole('radio', { name: 'eval/notes.txt' })).toBeDisabled();
expect(
screen.getByText(
'Only .yml, .yaml files can be selected. Upload a supported file or choose a different fileset.'
)
).toBeInTheDocument();
});
it('does not show disabled-file guidance when all visible files are selectable', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockExistingFiles,
acceptableFileTypes: ['.jsonl'],
invalidFileMode: 'disable',
}),
});
expect(
screen.queryByText(/Upload a supported file or choose a different fileset/)
).not.toBeInTheDocument();
expect(screen.getByRole('radio', { name: 'dataset/file1.jsonl' })).not.toBeDisabled();
expect(screen.getByRole('radio', { name: 'dataset/file2.jsonl' })).not.toBeDisabled();
});
});
describe('upload more files', () => {
it('renders upload more files anchor', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }),
});
expect(screen.getByText('Upload More Files')).toBeInTheDocument();
});
it('renders hidden file input with correct attributes', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockNewFiles,
acceptableFileTypes: ['.jsonl', '.json'],
}),
});
const fileInput = screen.getByLabelText('Upload more files', {
selector: 'input',
}) as HTMLInputElement;
expect(fileInput).toHaveClass('sr-only');
expect(fileInput).toHaveAttribute('type', 'file');
expect(fileInput).toHaveAttribute('accept', '.jsonl,.json');
});
it('dispatches SET_FILES when files are selected', async () => {
const user = userEvent.setup();
const mockDispatch = vi.fn();
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }, mockDispatch),
});
const fileInput = screen.getByLabelText('Upload more files', {
selector: 'input',
}) as HTMLInputElement;
const newFile = new File(['new content'], 'newfile.jsonl');
await user.upload(fileInput, newFile);
expect(mockDispatch).toHaveBeenCalledWith({
type: 'SET_FILES',
payload: [
{
id: 'newfile.jsonl',
type: 'new',
file: newFile,
},
],
});
});
it('label is associated with file input', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }),
});
const fileInput = screen.getByLabelText('Upload more files', {
selector: 'input',
});
expect(fileInput).toHaveAttribute('type', 'file');
});
});
describe('single-select (allowMultipleFileSelection=false, default)', () => {
it('renders a radio per file and no checkboxes', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }),
});
expect(screen.getAllByRole('radio')).toHaveLength(2);
expect(screen.queryAllByRole('checkbox')).toHaveLength(0);
});
it('shows the selected file as checked', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({
files: mockNewFiles,
selectedFiles: [mockNewFiles[0]],
}),
});
const radios = screen.getAllByRole('radio');
expect(radios[0]).toBeChecked();
expect(radios[1]).not.toBeChecked();
});
it('dispatches TOGGLE_FILE_SELECTION when an unselected radio is clicked', async () => {
const user = userEvent.setup();
const mockDispatch = vi.fn();
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }, mockDispatch),
});
const radios = screen.getAllByRole('radio');
await user.click(radios[1]);
expect(mockDispatch).toHaveBeenCalledWith({
type: 'TOGGLE_FILE_SELECTION',
payload: mockNewFiles[1],
});
});
it('does NOT dispatch when the already-selected radio is clicked', async () => {
const user = userEvent.setup();
const mockDispatch = vi.fn();
render(<SimpleFilesTable />, {
wrapper: createWrapper(
{ files: mockNewFiles, selectedFiles: [mockNewFiles[0]] },
mockDispatch
),
});
const radios = screen.getAllByRole('radio');
await user.click(radios[0]);
expect(mockDispatch).not.toHaveBeenCalled();
});
it('dispatches correct payload for existing files', async () => {
const user = userEvent.setup();
const mockDispatch = vi.fn();
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockExistingFiles }, mockDispatch),
});
const radios = screen.getAllByRole('radio');
await user.click(radios[0]);
expect(mockDispatch).toHaveBeenCalledWith({
type: 'TOGGLE_FILE_SELECTION',
payload: mockExistingFiles[0],
});
});
it('exposes the file name as the accessible name of each radio', () => {
render(<SimpleFilesTable />, {
wrapper: createWrapper({ files: mockNewFiles }),
});
expect(screen.getByRole('radio', { name: 'file1.jsonl' })).toBeInTheDocument();
expect(screen.getByRole('radio', { name: 'file2.jsonl' })).toBeInTheDocument();
});
});
});