-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathSectionHeader.test.ts
148 lines (136 loc) Β· 3.75 KB
/
SectionHeader.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {RuleConfigSeverity} from '@commitlint/types';
import {
combineCommitMessage,
getQuestions,
getQuestionConfig,
} from './SectionHeader';
import {setPromptConfig} from './store/prompts';
import {setRules} from './store/rules';
beforeEach(() => {
setRules({});
setPromptConfig({});
});
describe('getQuestions', () => {
test("should contain 'type','scope','subject'", () => {
const questions = getQuestions();
expect(questions).toHaveLength(3);
expect(questions).toEqual([
expect.objectContaining({
name: 'type',
}),
expect.objectContaining({
name: 'scope',
}),
expect.objectContaining({
name: 'subject',
}),
]);
});
test('should exclude question when must be empty', () => {
setRules({
'scope-empty': [RuleConfigSeverity.Error, 'always'],
});
const questions = getQuestions();
expect(questions).toHaveLength(2);
expect(questions).toEqual([
expect.objectContaining({
name: 'type',
}),
expect.objectContaining({
name: 'subject',
}),
]);
});
});
describe('getQuestionConfig', () => {
test("should 'scope' supports multiple items separated with ',\\/'", () => {
const config = getQuestionConfig('scope');
expect(config).toEqual(
expect.objectContaining({
multipleValueDelimiters: /\/|\\|,/g,
})
);
});
test("should 'scope' supports multiple select separated with settings.scopeEnumSeparator", () => {
setPromptConfig({
settings: {
scopeEnumSeparator: '/',
},
});
const config = getQuestionConfig('scope');
expect(config).toEqual(
expect.objectContaining({
multipleSelectDefaultDelimiter: '/',
})
);
});
});
describe('combineCommitMessage', () => {
test('should return correct string when type,scope,subject are not empty', () => {
const commitMessage = combineCommitMessage({
type: 'build',
scope: 'typescript',
subject: 'update tsconfig.json',
});
expect(commitMessage).toBe('build(typescript): update tsconfig.json');
});
test('when type is empty', () => {
let commitMessage = combineCommitMessage({
scope: 'typescript',
subject: 'update tsconfig.json',
});
expect(commitMessage).toBe('(typescript): update tsconfig.json');
commitMessage = combineCommitMessage({
scope: 'typescript',
});
expect(commitMessage).toBe('(typescript)');
});
test('when scope is empty', () => {
let commitMessage = combineCommitMessage({
type: 'build',
subject: 'update tsconfig.json',
});
expect(commitMessage).toBe('build: update tsconfig.json');
commitMessage = combineCommitMessage({
subject: 'update tsconfig.json',
});
expect(commitMessage).toBe('update tsconfig.json');
});
test('when subject is empty', () => {
const commitMessage = combineCommitMessage({
type: 'build',
scope: 'typescript',
});
expect(commitMessage).toBe('build(typescript)');
});
});
describe('HeaderQuestion', () => {
test('should limited by header maxLength and minLength', () => {
setRules({
'header-max-length': [RuleConfigSeverity.Error, 'always', 20],
'header-min-length': [RuleConfigSeverity.Error, 'always', 10],
'subject-max-length': [RuleConfigSeverity.Error, 'always', 10],
'subject-min-length': [RuleConfigSeverity.Error, 'always', 5],
});
setPromptConfig({
messages: {
skip: '(press enter to skip)',
max: 'upper %d chars',
min: '%d chars at least',
emptyWarning: '%s can not be empty',
upperLimitWarning: '%s: %s over limit %d',
lowerLimitWarning: '%s: %s below limit %d',
},
});
const questions = getQuestions();
const answers = {
type: ''.padEnd(8, 'x'),
scope: ''.padEnd(6, 'y'),
};
const lastQuestion = questions[2];
(lastQuestion.message as any)(answers);
expect(lastQuestion?.validate?.(''.padEnd(10, 'z'), answers)).toBe(
'subject: subject over limit 6'
);
});
});