-
Notifications
You must be signed in to change notification settings - Fork 6
/
validate-filenames.test.ts
88 lines (65 loc) · 3 KB
/
validate-filenames.test.ts
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
import * as nodePath from 'path';
import { validateFilenames } from '../src/validate-filenames';
const TEST_CONFIGS = {
anyCharacter: { path: '05-contains-a-files', pattern: '^.+$' },
nameDotExtension: {
path: '02-name-dot-extension-files',
pattern: '^.+\\..+$',
},
dotFile: { path: '03-dot-files', pattern: '^\\..+$' },
noDot: { path: '04-no-dot-files', pattern: '^[^\\.].+$' },
containsA: { path: '05-contains-a-files', pattern: 'a+' },
json: { path: '06-json-files', pattern: '\\.json$' },
};
let spy: jest.SpyInstance;
beforeEach(() => {
// We silence the console for better readability in tests
spy = jest.spyOn(console, 'log').mockImplementation();
});
afterEach(() => {
spy.mockRestore();
});
describe('Name of the group', () => {
it('01 - Passes for any character files', async () => {
const testConfig = TEST_CONFIGS.anyCharacter;
const path = nodePath.join(__dirname, 'test-files', testConfig.path);
const pattern = new RegExp(testConfig.pattern);
const expected = { totalFilesAnalyzed: 5, failedFiles: [] };
await expect(validateFilenames(path, pattern)).resolves.toEqual(expected);
});
it('02 - Passes for `name.ext` files', async () => {
const testConfig = TEST_CONFIGS.nameDotExtension;
const path = nodePath.join(__dirname, 'test-files', testConfig.path);
const pattern = new RegExp(testConfig.pattern);
const expected = { totalFilesAnalyzed: 1, failedFiles: [] };
await expect(validateFilenames(path, pattern)).resolves.toEqual(expected);
});
it('03 - Passes for `.dotfiles`', async () => {
const testConfig = TEST_CONFIGS.dotFile;
const path = nodePath.join(__dirname, 'test-files', testConfig.path);
const pattern = new RegExp(testConfig.pattern);
const expected = { totalFilesAnalyzed: 1, failedFiles: [] };
await expect(validateFilenames(path, pattern)).resolves.toEqual(expected);
});
it('04 - Passes for no `.dotfiles`', async () => {
const testConfig = TEST_CONFIGS.noDot;
const path = nodePath.join(__dirname, 'test-files', testConfig.path);
const pattern = new RegExp(testConfig.pattern);
const expected = { totalFilesAnalyzed: 5, failedFiles: [] };
await expect(validateFilenames(path, pattern)).resolves.toEqual(expected);
});
it('05 - Passes for files with `a` in the name', async () => {
const testConfig = TEST_CONFIGS.containsA;
const path = nodePath.join(__dirname, 'test-files', testConfig.path);
const pattern = new RegExp(testConfig.pattern);
const expected = { totalFilesAnalyzed: 5, failedFiles: [] };
await expect(validateFilenames(path, pattern)).resolves.toEqual(expected);
});
it('06 - Passes for json files `*.json`', async () => {
const testConfig = TEST_CONFIGS.json;
const path = nodePath.join(__dirname, 'test-files', testConfig.path);
const pattern = new RegExp(testConfig.pattern);
const expected = { totalFilesAnalyzed: 5, failedFiles: [] };
await expect(validateFilenames(path, pattern)).resolves.toEqual(expected);
});
});