Skip to content

Commit c301adf

Browse files
test: add missing tests
1 parent 65e2b70 commit c301adf

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

src/commit-template.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { PromptAnswers, headerTemplate, commitTemplate } from './commit-template';
2+
3+
describe('commit-template', () => {
4+
describe('headerTemplate', () => {
5+
test.each<[PromptAnswers, string]>([
6+
[{ type: 'foo', scope: 'bar', subject: 'baz' }, 'foo(bar): baz'],
7+
[{ type: 'foo' }, 'foo: '],
8+
[{ type: 'foo', subject: 'bar' }, 'foo: bar']
9+
])(`should convert answers %o to template '%s'`, (answers, expected) => {
10+
const result = headerTemplate(answers.type, answers.scope, answers.subject);
11+
12+
expect(result).toBe(expected);
13+
});
14+
});
15+
16+
describe('headerTemplate', () => {
17+
test.each<[PromptAnswers, string]>([
18+
[{ type: 'foo', scope: 'bar', subject: 'baz', body: '\nbody', footer: '\nfooter' }, `foo(bar): baz\nbody\nfooter`]
19+
])(`should convert answers %o to template '%s'`, (answers, expected) => {
20+
const result = commitTemplate(answers);
21+
22+
expect(result).toBe(expected);
23+
});
24+
});
25+
});

src/commit-template.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type PromptAnswers = {
1212

1313
export type Question = DistinctQuestion<PromptAnswers>;
1414

15-
export function header(type?: string, scope?: string, subject?: string): string {
15+
export function headerTemplate(type?: string, scope?: string, subject?: string): string {
1616
let header = `${type}`;
1717
if (scope) {
1818
header += `(${scope})`;
@@ -27,10 +27,12 @@ export function header(type?: string, scope?: string, subject?: string): string
2727
return header;
2828
}
2929

30-
export function commitTemplate(answers: Answers) {
31-
const head = header(answers.type, answers.scope, answers.subject);
30+
export function commitTemplate(answers: PromptAnswers) {
31+
let template = headerTemplate(answers.type, answers.scope, answers.subject);
3232

33-
const template = head + answers.breakingChange ? answers.breakingBody : answers.body;
33+
template += answers.body ?? '';
34+
35+
template += answers.footer ?? '';
3436

3537
return template;
3638
}

src/filters.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ describe('filters', () => {
3232

3333
expect(result).toBe('foo');
3434
});
35+
36+
test('does not add blank line to empty string', () => {
37+
const result = leadingBlankFilter('', [2, 'always', undefined]);
38+
39+
expect(result).toBe('');
40+
});
3541
});
3642

3743
describe('fullStopFilter', () => {
@@ -90,6 +96,12 @@ describe('filters', () => {
9096

9197
expect(result).toBe('FOO_bar');
9298
});
99+
100+
test('should not change when rule is array of cases', () => {
101+
const result = wordCaseFilter('foo', [Level.Error, 'always', ['upper-case', 'lower-case']]);
102+
103+
expect(result).toBe('foo');
104+
});
93105
});
94106

95107
describe('maxLineLengthFilter', () => {

src/prompts/subject-maker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { red, green } from 'chalk';
33
import { pipeWith, valueFromRule } from '../utils';
44
import { caseValidator, emptyValidator, maxLengthValidator, minLengthValidator, validate } from '../validators';
55
import { fullStopFilter, wordCaseFilter } from '../filters';
6-
import { header, PromptAnswers, Question } from '../commit-template';
6+
import { headerTemplate, PromptAnswers, Question } from '../commit-template';
77

88
export function validatorFactory(rules: Rules) {
99
return (value: string, answers: PromptAnswers) => {
10-
const headerValue = header(answers.type, answers.scope, value);
10+
const headerValue = headerTemplate(answers.type, answers.scope, value);
1111

1212
return validate([
1313
{
@@ -62,7 +62,7 @@ export function messageFactory(rules: Rules) {
6262
}
6363

6464
return `Write a short, imperative tense description of the change (max ${maxLength -
65-
header(answers.type, answers.scope).length} chars):\n`;
65+
headerTemplate(answers.type, answers.scope).length} chars):\n`;
6666
};
6767
}
6868

@@ -73,7 +73,8 @@ export function transformerFactory(rules: Rules) {
7373
const headerMaxLength = valueFromRule(rules['header-max-length']);
7474

7575
if (headerMaxLength) {
76-
const color = filter(value).length <= headerMaxLength - header(answers.type, answers.scope).length ? green : red;
76+
const color =
77+
filter(value).length <= headerMaxLength - headerTemplate(answers.type, answers.scope).length ? green : red;
7778
return color(`(${value.length}) ${value}`);
7879
}
7980

src/utils.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Case, Level } from '@commitlint/load';
2-
import { getLongest, pipeWith, valueFromRule, wordCase } from './utils';
2+
import { green, red } from 'chalk';
3+
import { getLongest, pipeWith, valueFromRule, wordCase, maxLengthTransformerFactory } from './utils';
34

45
describe('getLongest', () => {
56
test('return longest string length in array', () => {
@@ -70,4 +71,18 @@ describe('wordCase', () => {
7071
expect(result).toBe(72);
7172
});
7273
});
74+
75+
describe('maxLengthTransformerFactory', () => {
76+
test.each<[number | undefined, string, string]>([
77+
[3, 'foo', green('(3) foo')],
78+
[3, 'foo bar', red('(7) foo bar')],
79+
[undefined, 'foo', 'foo']
80+
])('should transform when length: %n to expected: %s', (length, value, expected) => {
81+
const factory = maxLengthTransformerFactory(length);
82+
83+
const result = factory(value);
84+
85+
expect(result).toBe(expected);
86+
});
87+
});
7388
});

0 commit comments

Comments
 (0)