|
1 | 1 | import sinon from 'sinon';
|
2 | 2 | import chai from 'chai';
|
3 | 3 | import sinonChai from 'sinon-chai';
|
4 |
| -import {prompter} from './index'; |
| 4 | +import proxyquire from 'proxyquire'; |
5 | 5 |
|
6 | 6 | const {expect} = chai;
|
7 | 7 | chai.use(sinonChai);
|
8 | 8 |
|
9 | 9 | describe(`prompter`, () => {
|
10 |
| - let cz, commit; |
11 |
| - beforeEach(() => { |
12 |
| - cz = {prompt: sinon.spy()}; |
| 10 | + let inquirer, commit, prompter, commitAnswers; |
| 11 | + |
| 12 | + before(() => { |
13 | 13 | commit = sinon.spy();
|
| 14 | + inquirer = {prompt: sinon.spy()}; |
| 15 | + prompter = proxyquire('./', {inquirer}).prompter; |
14 | 16 | });
|
15 | 17 |
|
16 |
| - it(`call cz.prompt`, () => { |
17 |
| - prompter(cz); |
18 |
| - expect(cz.prompt).to.have.been.calledWith(sinon.match.array, sinon.match.func); |
| 18 | + beforeEach(() => { |
| 19 | + prompter(null, commit); |
| 20 | + commitAnswers = inquirer.prompt.getCall(0).args[1]; |
19 | 21 | });
|
20 | 22 |
|
21 |
| - describe(`commitAnswers`, () => { |
22 |
| - let commitAnswers; |
23 |
| - beforeEach(() => { |
24 |
| - prompter(cz, commit); |
25 |
| - commitAnswers = cz.prompt.getCall(0).args[1]; |
26 |
| - }); |
| 23 | + it(`should call commit with the proper message`, () => { |
| 24 | + const message = 'sample commit message'; |
| 25 | + const issues = 'CZ-234 CZ-235'; |
| 26 | + const workflow = 'closed'; |
| 27 | + const time = '3y 2w 7d 8h 30m'; |
| 28 | + const comment = 'This took waaaaay too long'; |
| 29 | + commitAnswers({message, issues, workflow, time, comment}); |
| 30 | + expect(commit).to.have.been.calledWith([ |
| 31 | + message, |
| 32 | + issues, |
| 33 | + `#${workflow}`, |
| 34 | + `#time ${time}`, |
| 35 | + `#comment ${comment}` |
| 36 | + ].join(' ')); |
| 37 | + }); |
27 | 38 |
|
28 |
| - it(`should call commit with the proper message`, () => { |
| 39 | + ['workflow', 'time', 'comment'].forEach((item) => { |
| 40 | + it(`should just leave off ${item} if it's not specified`, () => { |
29 | 41 | const message = 'sample commit message';
|
30 | 42 | const issues = 'CZ-234 CZ-235';
|
31 | 43 | const workflow = 'closed';
|
32 | 44 | const time = '3y 2w 7d 8h 30m';
|
33 | 45 | const comment = 'This took waaaaay too long';
|
34 |
| - commitAnswers({message, issues, workflow, time, comment}); |
35 |
| - expect(commit).to.have.been.calledWith([ |
| 46 | + const answers = {message, issues, workflow, time, comment}; |
| 47 | + delete answers[item]; |
| 48 | + commitAnswers(answers); |
| 49 | + expect(commit).to.have.been.calledWith(filter([ |
36 | 50 | message,
|
37 | 51 | issues,
|
38 |
| - `#${workflow}`, |
39 |
| - `#time ${time}`, |
40 |
| - `#comment ${comment}` |
41 |
| - ].join(' ')); |
| 52 | + item !== 'workflow' ? `#${workflow}` : undefined, |
| 53 | + item !== 'time' ? `#time ${time}` : undefined, |
| 54 | + item !== 'comment' ? `#comment ${comment}` : undefined |
| 55 | + ]).join(' ')); |
42 | 56 | });
|
43 |
| - |
44 |
| - ['workflow', 'time', 'comment'].forEach((item) => { |
45 |
| - it(`should just leave off ${item} if it's not specified`, () => { |
46 |
| - const message = 'sample commit message'; |
47 |
| - const issues = 'CZ-234 CZ-235'; |
48 |
| - const workflow = 'closed'; |
49 |
| - const time = '3y 2w 7d 8h 30m'; |
50 |
| - const comment = 'This took waaaaay too long'; |
51 |
| - const answers = {message, issues, workflow, time, comment}; |
52 |
| - delete answers[item]; |
53 |
| - commitAnswers(answers); |
54 |
| - expect(commit).to.have.been.calledWith(filter([ |
55 |
| - message, |
56 |
| - issues, |
57 |
| - item !== 'workflow' ? `#${workflow}` : undefined, |
58 |
| - item !== 'time' ? `#time ${time}` : undefined, |
59 |
| - item !== 'comment' ? `#comment ${comment}` : undefined |
60 |
| - ]).join(' ')); |
61 |
| - }); |
62 |
| - }) |
63 | 57 | });
|
64 |
| - |
65 | 58 | });
|
66 | 59 |
|
67 | 60 | function filter(array) {
|
68 |
| - return array.filter(function(item) { |
69 |
| - return !!item; |
70 |
| - }); |
| 61 | + return array.filter(Boolean); |
71 | 62 | }
|
0 commit comments