Repository navigation
Expand file tree
/
Copy pathfile_upload_component.test.js
More file actions
50 lines (42 loc) · 2.16 KB
/
Copy pathfile_upload_component.test.js
File metadata and controls
50 lines (42 loc) · 2.16 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
const assert = require('node:assert/strict');
const { readFileSync } = require('node:fs');
const test = require('node:test');
const source = readFileSync('components/FileUpload.tsx', 'utf8');
test('FileUpload has one coherent validation loop for selected files', () => {
assert.match(source, /const validFiles: File\[\] = \[\]/);
assert.match(source, /const invalidFileNames: string\[\] = \[\]/);
assert.equal((source.match(/for\s*\(const file of files\)/g) || []).length, 1);
assert.doesNotMatch(source, /const errors: string\[\] = \[\]/);
});
test('FileUpload skips oversized files without passing them to onFilesSelected', () => {
assert.match(source, /if\s*\(file\.size > maxBytes\)/);
assert.match(source, /invalidFileNames\.push\(file\.name\)/);
assert.match(source, /validFiles\.push\(file\)/);
assert.match(source, /onFilesSelected\?\.\(validFiles\)/);
});
test('FileUpload validates file types against accept filter pattern', () => {
assert.match(source, /isFileTypeAccepted/);
assert.match(source, /!isFileTypeAccepted\(file,\s*accept\)/);
assert.match(source, /invalidFileNames\.push\(/);
});
test('FileUpload resets invalid-only selections so users can select the same file again', () => {
assert.match(source, /validFiles\.length === 0/);
assert.match(source, /clearSelection\(\)/);
assert.match(source, /inputRef\.current\.value = ''/);
});
test('FileUpload keeps image preview URL cleanup', () => {
assert.match(source, /URL\.createObjectURL\(file\)/);
assert.match(source, /URL\.revokeObjectURL\(url\)/);
});
test('FileUpload keeps one validation loop for selected files', () => {
const loopMatches = source.match(/for\s*\(const file of files\)/g) || [];
assert.equal(loopMatches.length, 1);
assert.match(source, /const validFiles: File\[\] = \[\]/);
assert.match(source, /const invalidFileNames: string\[\] = \[\]/);
});
test('FileUpload resets invalid selections and only reports valid files', () => {
assert.match(source, /inputRef\.current\.value = ''/);
assert.match(source, /setSelectedFiles\(validFiles\)/);
assert.match(source, /onFilesSelected\?\.\(validFiles\)/);
assert.doesNotMatch(source, /onFilesSelected\(files\)/);
});