forked from leoforfree/cz-customizable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestionsSpec.js
305 lines (262 loc) · 9.99 KB
/
questionsSpec.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* eslint-disable nada/path-case */
const fs = require('fs');
const questions = require('../questions.js');
describe('cz-customizable', () => {
let config;
beforeEach(() => {
config = null;
});
const mockedCz = {
Separator: jasmine.createSpy(),
};
const getQuestion = number => questions.getQuestions(config, mockedCz)[number - 1];
it('should array of questions be returned', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
allowTicketNumber: true,
isTicketNumberRequired: true,
ticketNumberPrefix: 'TICKET-',
ticketNumberRegExp: '\\d{1,5}',
subjectLimit: 40,
};
// question 1 - TYPE
expect(getQuestion(1).name).toEqual('type');
expect(getQuestion(1).type).toEqual('list');
expect(getQuestion(1).choices[0]).toEqual({
value: 'feat',
name: 'feat: my feat',
});
// question 2 - SCOPE
expect(getQuestion(2).name).toEqual('scope');
expect(getQuestion(2).choices({})[0]).toEqual({ name: 'myScope' });
expect(getQuestion(2).choices({ type: 'fix' })[0]).toEqual({
name: 'fixOverride',
}); // should override scope
expect(getQuestion(2).when({ type: 'fix' })).toEqual(true);
expect(getQuestion(2).when({ type: 'WIP' })).toEqual(false);
expect(getQuestion(2).when({ type: 'wip' })).toEqual(false);
// question 3 - SCOPE CUSTOM
expect(getQuestion(3).name).toEqual('scope');
expect(getQuestion(3).when({ scope: 'custom' })).toEqual(true);
expect(getQuestion(3).when({ scope: false })).toEqual(false);
expect(getQuestion(3).when({ scope: 'scope' })).toEqual(false);
// question 4 - TICKET_NUMBER
expect(getQuestion(4).name).toEqual('ticketNumber');
expect(getQuestion(4).type).toEqual('input');
expect(getQuestion(4).message.indexOf('Enter the ticket number following this pattern')).toEqual(0);
expect(getQuestion(4).validate()).toEqual(false); // mandatory question
// question 5 - SUBJECT
expect(getQuestion(5).name).toEqual('subject');
expect(getQuestion(5).type).toEqual('input');
expect(getQuestion(5).default).toEqual(null);
expect(getQuestion(5).message).toMatch(/IMPERATIVE tense description/);
expect(getQuestion(5).filter('Subject')).toEqual('subject');
expect(getQuestion(5).validate('bad subject that exceed limit for 6 characters')).toEqual('Exceed limit: 40');
expect(getQuestion(5).validate('good subject')).toEqual(true);
// question 6 - BODY
expect(getQuestion(6).name).toEqual('body');
expect(getQuestion(6).type).toEqual('input');
expect(getQuestion(6).default).toEqual(null);
// question 7 - BREAKING CHANGE
expect(getQuestion(7).name).toEqual('breaking');
expect(getQuestion(7).type).toEqual('input');
expect(getQuestion(7).when({ type: 'feat' })).toEqual(true);
expect(getQuestion(7).when({ type: 'fix' })).toEqual(false);
// question 8 - FOOTER
expect(getQuestion(8).name).toEqual('footer');
expect(getQuestion(8).type).toEqual('input');
expect(getQuestion(8).when({ type: 'fix' })).toEqual(true);
expect(getQuestion(8).when({ type: 'WIP' })).toEqual(false);
// question 9, last one, CONFIRM COMMIT OR NOT
expect(getQuestion(9).name).toEqual('confirmCommit');
expect(getQuestion(9).type).toEqual('expand');
const answers = {
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature',
};
expect(getQuestion(9).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
});
it('default length limit of subject should be 100', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
};
expect(getQuestion(5).validate('good subject')).toEqual(true);
expect(
getQuestion(5).validate(
'bad subject that exceed limit bad subject that exceed limitbad subject that exceed limit test test test'
)
).toEqual('Exceed limit: 100');
});
it('subject should be lowercased by default', () => {
config = {};
expect(getQuestion(5).filter('Some subject')).toEqual('some subject');
});
it('subject should be capitilized when config property "upperCaseSubject" is set to true', () => {
config = {
upperCaseSubject: true,
};
expect(getQuestion(5).filter('some subject')).toEqual('Some subject');
});
describe('optional fixOverride and allowBreakingChanges', () => {
it('should restrict BREAKING CHANGE question when config property "allowBreakingChanges" specifies array of types', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
allowBreakingChanges: ['fix'],
};
expect(getQuestion(7).name).toEqual('breaking');
const answers = {
type: 'feat',
};
expect(getQuestion(7).when(answers)).toEqual(false); // not allowed
});
it('should allow BREAKING CHANGE question when config property "allowBreakingChanges" specifies array of types and answer is one of those', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
allowBreakingChanges: ['fix', 'feat'],
};
expect(getQuestion(7).name).toEqual('breaking');
const answers = {
type: 'feat',
};
expect(getQuestion(7).when(answers)).toEqual(true); // allowed
});
});
describe('Optional scopes', () => {
it('should use scope override', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopeOverrides: {
feat: [{ name: 'myScope' }],
},
};
// question 2 with
expect(getQuestion(2).name).toEqual('scope');
expect(getQuestion(2).choices({})[0]).toBeUndefined();
expect(getQuestion(2).choices({ type: 'feat' })[0]).toEqual({
name: 'myScope',
}); // should override scope
expect(getQuestion(2).when({ type: 'feat' })).toEqual(true);
const answers = { type: 'fix' };
expect(getQuestion(2).when(answers)).toEqual(false);
expect(answers.scope).toEqual('custom');
});
});
describe('no TicketNumber question', () => {
it('should use scope override', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
allowTicketNumber: false,
};
expect(getQuestion(4).name).toEqual('ticketNumber');
expect(getQuestion(4).when()).toEqual(false);
});
});
describe('ask for breaking change first', () => {
it('when config askForBreakingChangeFirst is true', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
askForBreakingChangeFirst: true,
};
expect(getQuestion(1).name).toEqual('breaking');
expect(getQuestion(1).when()).toEqual(true);
});
});
describe('TicketNumber', () => {
it('disable TicketNumber question', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
allowTicketNumber: false,
};
expect(getQuestion(4).name).toEqual('ticketNumber');
expect(getQuestion(4).when()).toEqual(false);
});
it('custom message defined', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
allowTicketNumber: true,
messages: {
ticketNumber: 'ticket number',
},
};
expect(getQuestion(4).name).toEqual('ticketNumber');
expect(getQuestion(4).message).toEqual('ticket number');
});
describe('validation', () => {
it('invalid because empty and required', () => {
config = {
isTicketNumberRequired: true,
};
expect(getQuestion(4).validate('')).toEqual(false);
});
it('empty but valid because optional', () => {
config = {
isTicketNumberRequired: false,
};
expect(getQuestion(4).validate('')).toEqual(true);
});
it('valid because there is no regexp defined', () => {
config = {
isTicketNumberRequired: true,
ticketNumberRegExp: undefined,
};
expect(getQuestion(4).validate('21234')).toEqual(true);
});
it("invalid because regexp don't match", () => {
config = {
isTicketNumberRequired: true,
ticketNumberRegExp: '\\d{1,5}',
};
expect(getQuestion(4).validate('sddsa')).toEqual(false);
});
it('valid because regexp match', () => {
config = {
isTicketNumberRequired: true,
ticketNumberRegExp: '\\d{1,5}',
};
expect(getQuestion(4).validate('12345')).toEqual(true);
});
});
});
describe('commit already prepared', () => {
let existsSync;
let readFileSync;
beforeEach(() => {
config = {};
existsSync = spyOn(fs, 'existsSync');
readFileSync = spyOn(fs, 'readFileSync');
});
it('should ignore if there is no prepared commit file', () => {
existsSync.andReturn(false);
expect(getQuestion(5).default).toEqual(null);
expect(getQuestion(6).default).toEqual(null);
});
it('should ignore an empty prepared commit', () => {
existsSync.andReturn(true);
readFileSync.andReturn('');
expect(getQuestion(5).default).toEqual(null);
expect(getQuestion(6).default).toEqual(null);
});
it('should take a single line commit as the subject', () => {
existsSync.andReturn(true);
readFileSync.andReturn('my commit');
expect(getQuestion(5).default).toEqual('my commit');
expect(getQuestion(6).default).toEqual(null);
});
it('should split multi line commit between the subject and the body', () => {
existsSync.andReturn(true);
readFileSync.andReturn('my commit\nmessage\n\nis on several lines');
expect(getQuestion(5).default).toEqual('my commit');
expect(getQuestion(6).default).toEqual(`message|is on several lines`);
});
});
});