Skip to content

Commit a3b7f21

Browse files
test: add tests for template
1 parent 5ff8bd2 commit a3b7f21

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/commit-template.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { PromptAnswers, headerTemplate, commitTemplate } from './commit-template';
1+
import { Answers, headerTemplate, commitTemplate } from './commit-template';
22

33
describe('commit-template', () => {
44
describe('headerTemplate', () => {
5-
test.each<[PromptAnswers, string]>([
5+
test.each<[Answers, string]>([
66
[{ type: 'foo', scope: 'bar', subject: 'baz' }, 'foo(bar): baz'],
77
[{ type: 'foo' }, 'foo: '],
88
[{ type: 'foo', subject: 'bar' }, 'foo: bar']
@@ -13,9 +13,14 @@ describe('commit-template', () => {
1313
});
1414
});
1515

16-
describe('headerTemplate', () => {
17-
test.each<[PromptAnswers, string]>([
18-
[{ type: 'foo', scope: 'bar', subject: 'baz', body: '\nbody', footer: '\nfooter' }, `foo(bar): baz\nbody\nfooter`]
16+
describe('commitTemplate', () => {
17+
test.each<[Answers, string]>([
18+
[
19+
{ type: 'foo', scope: 'bar', subject: 'baz', body: '\nbody', breaking: '\nfooter' },
20+
`foo(bar): baz\n\nbody\n\nfooter`
21+
],
22+
[{ type: 'foo', subject: 'bar' }, 'foo: bar'],
23+
[{ type: 'foo', subject: 'bar', breaking: '\nbaz', issue: '\nbuz' }, 'foo: bar\n\nbaz\nbuz']
1924
])(`should convert answers %o to template '%s'`, (answers, expected) => {
2025
const result = commitTemplate(answers);
2126

src/commit-template.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import { Answers, DistinctQuestion } from 'inquirer';
1+
import { DistinctQuestion } from 'inquirer';
22

3-
export type PromptAnswers = {
3+
export type Answers = {
44
type?: string;
55
scope?: string;
66
subject?: string;
77
body?: string;
88
isBreaking?: boolean;
9+
breaking?: string;
910
isIssue?: boolean;
10-
footer?: string;
11+
issue?: string;
1112
};
1213

13-
export type Question = DistinctQuestion<PromptAnswers>;
14+
export type Question = DistinctQuestion<Answers>;
1415

1516
export function headerTemplate(type?: string, scope?: string, subject?: string): string {
1617
let header = `${type}`;
@@ -27,12 +28,22 @@ export function headerTemplate(type?: string, scope?: string, subject?: string):
2728
return header;
2829
}
2930

30-
export function commitTemplate(answers: PromptAnswers) {
31+
function renderSection(value: string | undefined): string {
32+
return value ? `\n${value}` : '';
33+
}
34+
35+
export function commitTemplate(answers: Answers) {
3136
let template = headerTemplate(answers.type, answers.scope, answers.subject);
3237

33-
template += answers.body ?? '';
38+
template += renderSection(answers.body);
3439

35-
template += answers.footer ?? '';
40+
template += renderSection(answers.breaking);
41+
42+
if (answers.breaking) {
43+
template += answers.issue ?? '';
44+
} else {
45+
template += renderSection(answers.issue);
46+
}
3647

3748
return template;
3849
}

0 commit comments

Comments
 (0)