|
| 1 | +import parse from '@commitlint/parse'; |
| 2 | +import {Commit} from '@commitlint/types'; |
| 3 | +import {headerTrim} from './header-trim'; |
| 4 | + |
| 5 | +const messages = { |
| 6 | + correct: 'test: subject', |
| 7 | + |
| 8 | + whitespaceStart: ' test: subject', |
| 9 | + whitespaceEnd: 'test: subject ', |
| 10 | + whitespaceSurround: ' test: subject ', |
| 11 | + |
| 12 | + tabStart: '\t\ttest: subject', |
| 13 | + tabEnd: 'test: subject\t\t', |
| 14 | + tabSurround: '\t\ttest: subject\t', |
| 15 | + |
| 16 | + mixStart: '\t\ttest: subject', |
| 17 | + mixEnd: 'test: subject\t\t', |
| 18 | + mixSurround: '\t \ttest: subject \t \t', |
| 19 | +}; |
| 20 | + |
| 21 | +const parsed = Object.entries(messages).reduce((_parsed, [key, message]) => { |
| 22 | + _parsed[key] = parse(message); |
| 23 | + return _parsed; |
| 24 | +}, {}) as Record<keyof typeof messages, Promise<Commit>>; |
| 25 | + |
| 26 | +test('should succeed when header is not surrounded by whitespace', async () => { |
| 27 | + const result = headerTrim(await parsed.correct); |
| 28 | + expect(result).toEqual(expect.arrayContaining([true])); |
| 29 | +}); |
| 30 | + |
| 31 | +( |
| 32 | + [ |
| 33 | + ['mixed whitespace', parsed.mixStart], |
| 34 | + ['whitespace', parsed.whitespaceStart], |
| 35 | + ['tab', parsed.tabStart], |
| 36 | + ] as const |
| 37 | +).forEach(([desc, commit]) => { |
| 38 | + test(`should fail with ${desc}`, async () => { |
| 39 | + const result = headerTrim(await commit); |
| 40 | + expect(result).toEqual( |
| 41 | + expect.arrayContaining([false, 'header must not start with whitespace']) |
| 42 | + ); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +( |
| 47 | + [ |
| 48 | + ['mixed whitespace', parsed.mixEnd], |
| 49 | + ['whitespace', parsed.whitespaceEnd], |
| 50 | + ['tab', parsed.tabEnd], |
| 51 | + ] as const |
| 52 | +).forEach(([desc, commit]) => { |
| 53 | + test(`should fail when ends with ${desc}`, async () => { |
| 54 | + const result = headerTrim(await commit); |
| 55 | + expect(result).toEqual( |
| 56 | + expect.arrayContaining([false, 'header must not end with whitespace']) |
| 57 | + ); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +( |
| 62 | + [ |
| 63 | + ['mixed whitespace', parsed.mixSurround], |
| 64 | + ['whitespace', parsed.whitespaceSurround], |
| 65 | + ['tab', parsed.tabSurround], |
| 66 | + ] as const |
| 67 | +).forEach(([desc, commit]) => { |
| 68 | + test(`should fail when surrounded by ${desc}`, async () => { |
| 69 | + const result = headerTrim(await commit); |
| 70 | + expect(result).toEqual( |
| 71 | + expect.arrayContaining([ |
| 72 | + false, |
| 73 | + 'header must not be surrounded by whitespace', |
| 74 | + ]) |
| 75 | + ); |
| 76 | + }); |
| 77 | +}); |
0 commit comments