-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathheader-trim.test.ts
77 lines (68 loc) · 1.97 KB
/
header-trim.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
import parse from '@commitlint/parse';
import {Commit} from '@commitlint/types';
import {headerTrim} from './header-trim';
const messages = {
correct: 'test: subject',
whitespaceStart: ' test: subject',
whitespaceEnd: 'test: subject ',
whitespaceSurround: ' test: subject ',
tabStart: '\t\ttest: subject',
tabEnd: 'test: subject\t\t',
tabSurround: '\t\ttest: subject\t',
mixStart: '\t\ttest: subject',
mixEnd: 'test: subject\t\t',
mixSurround: '\t \ttest: subject \t \t',
};
const parsed = Object.entries(messages).reduce((_parsed, [key, message]) => {
_parsed[key] = parse(message);
return _parsed;
}, {}) as Record<keyof typeof messages, Promise<Commit>>;
test('should succeed when header is not surrounded by whitespace', async () => {
const result = headerTrim(await parsed.correct);
expect(result).toEqual(expect.arrayContaining([true]));
});
(
[
['mixed whitespace', parsed.mixStart],
['whitespace', parsed.whitespaceStart],
['tab', parsed.tabStart],
] as const
).forEach(([desc, commit]) => {
test(`should fail with ${desc}`, async () => {
const result = headerTrim(await commit);
expect(result).toEqual(
expect.arrayContaining([false, 'header must not start with whitespace'])
);
});
});
(
[
['mixed whitespace', parsed.mixEnd],
['whitespace', parsed.whitespaceEnd],
['tab', parsed.tabEnd],
] as const
).forEach(([desc, commit]) => {
test(`should fail when ends with ${desc}`, async () => {
const result = headerTrim(await commit);
expect(result).toEqual(
expect.arrayContaining([false, 'header must not end with whitespace'])
);
});
});
(
[
['mixed whitespace', parsed.mixSurround],
['whitespace', parsed.whitespaceSurround],
['tab', parsed.tabSurround],
] as const
).forEach(([desc, commit]) => {
test(`should fail when surrounded by ${desc}`, async () => {
const result = headerTrim(await commit);
expect(result).toEqual(
expect.arrayContaining([
false,
'header must not be surrounded by whitespace',
])
);
});
});