UNPKG

9.75 kBJavaScriptView Raw
1const rewire = require('rewire');
2
3// TODO: enable tests
4describe.skip('cz-customizable', () => {
5 let module;
6 // let cz;
7 let commit;
8
9 beforeEach(() => {
10 module = rewire('../index.js');
11
12 // eslint-disable-next-line no-underscore-dangle
13 module.__set__({
14 log: {
15 info() {},
16 },
17
18 readConfigFile() {
19 return {
20 types: [{ value: 'feat', name: 'feat: my feat' }],
21 scopes: [{ name: 'myScope' }],
22 scopeOverrides: {
23 fix: [{ name: 'fixOverride' }],
24 },
25 allowCustomScopes: true,
26 allowBreakingChanges: ['feat'],
27 };
28 },
29 });
30
31 // cz = jasmine.createSpyObj('cz', ['prompt', 'Separator']);
32 commit = jasmine.createSpy();
33 });
34
35 function getMockedCz(answers) {
36 return {
37 prompt() {
38 return {
39 then(cb) {
40 cb(answers);
41 },
42 };
43 },
44 };
45 }
46
47 it('should commit without confirmation', () => {
48 const answers = {
49 confirmCommit: 'yes',
50 type: 'feat',
51 subject: 'do it all',
52 };
53
54 const mockCz = getMockedCz(answers);
55
56 // run commitizen plugin
57 module.prompter(mockCz, commit);
58
59 expect(commit).toHaveBeenCalledWith('feat: do it all');
60 });
61
62 it('should escape special characters sush as backticks', () => {
63 const answers = {
64 confirmCommit: 'yes',
65 type: 'feat',
66 subject: 'with backticks `here`',
67 };
68
69 const mockCz = getMockedCz(answers);
70 module.prompter(mockCz, commit);
71
72 expect(commit).toHaveBeenCalledWith('feat: with backticks \\`here\\`');
73 });
74
75 it('should not call commit() function if there is no final confirmation and display log message saying commit has been canceled', () => {
76 const mockCz = getMockedCz({});
77
78 // run commitizen plugin
79 module.prompter(mockCz, commit);
80
81 expect(commit).not.toHaveBeenCalled();
82 });
83
84 it('should call commit() function with commit message when user confirms commit and split body when pipes are present', () => {
85 const answers = {
86 confirmCommit: 'yes',
87 type: 'feat',
88 scope: 'myScope',
89 subject: 'create a new cool feature',
90 body: '-line1|-line2',
91 breaking: 'breaking',
92 footer: 'my footer',
93 };
94
95 const mockCz = getMockedCz(answers);
96 module.prompter(mockCz, commit);
97
98 expect(commit).toHaveBeenCalledWith(
99 'feat(myScope): create a new cool feature\n\n-line1\n-line2\n\nBREAKING CHANGE:\nbreaking\n\nISSUES CLOSED: my footer'
100 );
101 });
102
103 it('should call commit() function with commit message with the minimal required fields', () => {
104 const answers = {
105 confirmCommit: 'yes',
106 type: 'feat',
107 scope: 'myScope',
108 subject: 'create a new cool feature',
109 };
110
111 const mockCz = getMockedCz(answers);
112 module.prompter(mockCz, commit);
113 expect(commit).toHaveBeenCalledWith('feat(myScope): create a new cool feature');
114 });
115
116 it('should suppress scope when commit type is WIP', () => {
117 const answers = {
118 confirmCommit: 'yes',
119 type: 'WIP',
120 subject: 'this is my work-in-progress',
121 };
122
123 const mockCz = getMockedCz(answers);
124 module.prompter(mockCz, commit);
125 expect(commit).toHaveBeenCalledWith('WIP: this is my work-in-progress');
126 });
127
128 it('should allow edit message before commit', done => {
129 process.env.EDITOR = 'true';
130
131 const answers = {
132 confirmCommit: 'edit',
133 type: 'feat',
134 subject: 'create a new cool feature',
135 };
136
137 const mockCz = getMockedCz(answers);
138 module.prompter(mockCz, commit);
139
140 setTimeout(() => {
141 expect(commit).toHaveBeenCalledWith('feat: create a new cool feature');
142 done();
143 }, 100);
144 });
145
146 it('should not commit if editor returned non-zero value', done => {
147 process.env.EDITOR = 'false';
148
149 const answers = {
150 confirmCommit: 'edit',
151 type: 'feat',
152 subject: 'create a new cool feature',
153 };
154
155 const mockCz = getMockedCz(answers);
156 module.prompter(mockCz, commit);
157
158 setTimeout(() => {
159 expect(commit.wasCalled).toEqual(false);
160 done();
161 }, 100);
162 });
163
164 it('should truncate subject if number of characters is higher than 100', () => {
165 const chars100 =
166 '0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789';
167
168 // this string will be prepend: "ISSUES CLOSED: " = 15 chars
169 const footerChars100 =
170 '0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-012345';
171
172 const answers = {
173 confirmCommit: 'yes',
174 type: 'feat',
175 scope: 'myScope',
176 subject: chars100,
177 body: `${chars100} body-second-line`,
178 footer: `${footerChars100} footer-second-line`,
179 };
180
181 const mockCz = getMockedCz(answers);
182 module.prompter(mockCz, commit);
183
184 const firstPart = 'feat(myScope): ';
185
186 const firstLine = commit.mostRecentCall.args[0].split('\n\n')[0];
187 expect(firstLine).toEqual(firstPart + answers.subject.slice(0, 100));
188
189 // it should wrap body
190 const body = commit.mostRecentCall.args[0].split('\n\n')[1];
191 expect(body).toEqual(`${chars100}\nbody-second-line`);
192
193 // it should wrap footer
194 const footer = commit.mostRecentCall.args[0].split('\n\n')[2];
195 expect(footer).toEqual(`ISSUES CLOSED: ${footerChars100}\nfooter-second-line`);
196 });
197
198 it('should call commit() function with custom breaking prefix', () => {
199 const answers = {
200 confirmCommit: 'yes',
201 type: 'feat',
202 scope: 'myScope',
203 subject: 'create a new cool feature',
204 breaking: 'breaking',
205 footer: 'my footer',
206 };
207
208 // eslint-disable-next-line no-underscore-dangle
209 module.__set__({
210 log: {
211 info() {},
212 },
213
214 readConfigFile() {
215 return {
216 types: [{ value: 'feat', name: 'feat: my feat' }],
217 scopes: [{ name: 'myScope' }],
218 scopeOverrides: {
219 fix: [{ name: 'fixOverride' }],
220 },
221 allowCustomScopes: true,
222 allowBreakingChanges: ['feat'],
223 breakingPrefix: 'WARNING:',
224 };
225 },
226 });
227
228 const mockCz = getMockedCz(answers);
229 module.prompter(mockCz, commit);
230
231 expect(commit).toHaveBeenCalledWith(
232 'feat(myScope): create a new cool feature\n\nWARNING:\nbreaking\n\nISSUES CLOSED: my footer'
233 );
234 });
235
236 it('should call commit() function with custom footer prefix', () => {
237 const answers = {
238 confirmCommit: 'yes',
239 type: 'feat',
240 scope: 'myScope',
241 subject: 'create a new cool feature',
242 breaking: 'breaking',
243 footer: 'my footer',
244 };
245
246 // eslint-disable-next-line no-underscore-dangle
247 module.__set__({
248 log: {
249 info() {},
250 },
251
252 readConfigFile() {
253 return {
254 types: [{ value: 'feat', name: 'feat: my feat' }],
255 scopes: [{ name: 'myScope' }],
256 scopeOverrides: {
257 fix: [{ name: 'fixOverride' }],
258 },
259 allowCustomScopes: true,
260 allowBreakingChanges: ['feat'],
261 footerPrefix: 'FIXES:',
262 };
263 },
264 });
265
266 const mockCz = getMockedCz(answers);
267 module.prompter(mockCz, commit);
268
269 expect(commit).toHaveBeenCalledWith(
270 'feat(myScope): create a new cool feature\n\nBREAKING CHANGE:\nbreaking\n\nFIXES: my footer'
271 );
272 });
273
274 it('should call commit() function with custom footer prefix set to empty string', () => {
275 const answers = {
276 confirmCommit: 'yes',
277 type: 'feat',
278 scope: 'myScope',
279 subject: 'create a new cool feature',
280 breaking: 'breaking',
281 footer: 'my footer',
282 };
283
284 // eslint-disable-next-line no-underscore-dangle
285 module.__set__({
286 log: {
287 info() {},
288 },
289
290 readConfigFile() {
291 return {
292 types: [{ value: 'feat', name: 'feat: my feat' }],
293 scopes: [{ name: 'myScope' }],
294 scopeOverrides: {
295 fix: [{ name: 'fixOverride' }],
296 },
297 allowCustomScopes: true,
298 allowBreakingChanges: ['feat'],
299 footerPrefix: '',
300 };
301 },
302 });
303
304 const mockCz = getMockedCz(answers);
305 module.prompter(mockCz, commit);
306
307 expect(commit).toHaveBeenCalledWith(
308 'feat(myScope): create a new cool feature\n\nBREAKING CHANGE:\nbreaking\n\nmy footer'
309 );
310 });
311
312 it('should call commit() function with ticket number', () => {
313 const answers = {
314 confirmCommit: 'yes',
315 type: 'feat',
316 scope: 'myScope',
317 subject: 'create a new cool feature',
318 ticketNumber: 'TICKET-1234',
319 };
320
321 const mockCz = getMockedCz(answers);
322 module.prompter(mockCz, commit);
323 expect(commit).toHaveBeenCalledWith('feat(myScope): TICKET-1234 create a new cool feature');
324 });
325
326 it('should call commit() function with ticket number and prefix', () => {
327 // eslint-disable-next-line no-underscore-dangle
328 module.__set__({
329 log: {
330 info() {},
331 },
332
333 readConfigFile() {
334 return {
335 types: [{ value: 'feat', name: 'feat: my feat' }],
336 scopes: [{ name: 'myScope' }],
337 scopeOverrides: {
338 fix: [{ name: 'fixOverride' }],
339 },
340 allowCustomScopes: true,
341 allowBreakingChanges: ['feat'],
342 breakingPrefix: 'WARNING:',
343 ticketNumberPrefix: 'TICKET-',
344 };
345 },
346 });
347
348 const answers = {
349 confirmCommit: 'yes',
350 type: 'feat',
351 scope: 'myScope',
352 subject: 'create a new cool feature',
353 ticketNumber: '1234',
354 };
355
356 const mockCz = getMockedCz(answers);
357 module.prompter(mockCz, commit);
358 expect(commit).toHaveBeenCalledWith('feat(myScope): TICKET-1234 create a new cool feature');
359 });
360});