Skip to content

Commit

Permalink
feat(): allow empty and custom scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Guria committed Jan 10, 2016
1 parent fdda979 commit 2906b07
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 27 deletions.
1 change: 1 addition & 0 deletions cz-config-EXAMPLE.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
]
},

allowCustomScopes: true,
allowBreakingChanges: ['feat', 'fix']

};
37 changes: 34 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,43 @@ module.exports = {
name: 'scope',
message: '\nDenote the SCOPE of this change:\n',
choices: function(answers) {
var scopes = [];
if (config.scopeOverrides[answers.type]) {
return config.scopeOverrides[answers.type];
scopes = scopes.concat(config.scopeOverrides[answers.type]);
} else {
scopes = scopes.concat(config.scopes);
}
return config.scopes;
if (config.allowCustomScopes || scopes.length === 0) {
scopes = scopes.concat([
new cz.Separator(),
{ name: 'empty', value: false },
{ name: 'custom', value: 'custom' }
]);
}
return scopes;
},
when: isNotWip
when: function(answers) {
var hasScope = false;
if (config.scopeOverrides[answers.type]) {
hasScope = !!(config.scopeOverrides[answers.type].length > 0);
} else {
hasScope = !!(config.scopes && (config.scopes.length > 0));
}
if (!hasScope) {
answers.scope = 'custom';
return false;
} else {
return isNotWip(answers);
}
}
},
{
type: 'input',
name: 'scope',
message: '\nDenote the SCOPE of this change:\n',
when: function(answers) {
return answers.scope === 'custom';
}
},
{
type: 'input',
Expand Down
95 changes: 71 additions & 24 deletions spec/czCustomizableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ describe('cz-customizable', function() {
scopeOverrides: {
fix: [{name: 'fixOverride'}]
},
allowCustomScopes: true,
allowBreakingChanges: ['feat']
};
}
});

cz = {prompt: jasmine.createSpy()};
cz = jasmine.createSpyObj('cz', ['prompt', 'Separator']);
commit = jasmine.createSpy();
});

Expand All @@ -53,31 +54,37 @@ describe('cz-customizable', function() {
expect(getQuestion(2).when({type: 'wip'})).toEqual(false);

//question 3
expect(getQuestion(3).name).toEqual('subject');
expect(getQuestion(3).type).toEqual('input');
expect(getQuestion(3).message).toMatch(/IMPERATIVE tense description/);
expect(getQuestion(3).validate()).toEqual(false); //mandatory question
expect(getQuestion(3).filter('Subject')).toEqual('subject');
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
expect(getQuestion(4).name).toEqual('body');
expect(getQuestion(4).name).toEqual('subject');
expect(getQuestion(4).type).toEqual('input');
expect(getQuestion(4).message).toMatch(/IMPERATIVE tense description/);
expect(getQuestion(4).validate()).toEqual(false); //mandatory question
expect(getQuestion(4).filter('Subject')).toEqual('subject');

//question 5
expect(getQuestion(5).name).toEqual('breaking');
expect(getQuestion(5).name).toEqual('body');
expect(getQuestion(5).type).toEqual('input');
expect(getQuestion(5).when({type: 'feat'})).toEqual(true);
expect(getQuestion(5).when({type: 'fix'})).toEqual(false);

//question 6
expect(getQuestion(6).name).toEqual('footer');
expect(getQuestion(6).name).toEqual('breaking');
expect(getQuestion(6).type).toEqual('input');
expect(getQuestion(6).when({type: 'fix'})).toEqual(true);
expect(getQuestion(6).when({type: 'WIP'})).toEqual(false);
expect(getQuestion(6).when({type: 'feat'})).toEqual(true);
expect(getQuestion(6).when({type: 'fix'})).toEqual(false);

//question 7, last one
expect(getQuestion(7).name).toEqual('confirmCommit');
expect(getQuestion(7).type).toEqual('confirm');
//question 7
expect(getQuestion(7).name).toEqual('footer');
expect(getQuestion(7).type).toEqual('input');
expect(getQuestion(7).when({type: 'fix'})).toEqual(true);
expect(getQuestion(7).when({type: 'WIP'})).toEqual(false);

//question 8, last one
expect(getQuestion(8).name).toEqual('confirmCommit');
expect(getQuestion(8).type).toEqual('confirm');


var answers = {
Expand All @@ -86,7 +93,7 @@ describe('cz-customizable', function() {
scope: 'myScope',
subject: 'create a new cool feature'
};
expect(getQuestion(7).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
expect(getQuestion(8).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
});

it('should not call commit() function if there is no final confirmation', function() {
Expand Down Expand Up @@ -190,7 +197,7 @@ describe('cz-customizable', function() {
}
});

cz = {prompt: jasmine.createSpy()};
cz = jasmine.createSpyObj('cz', ['prompt', 'Separator']);
commit = jasmine.createSpy();
});

Expand All @@ -215,19 +222,59 @@ describe('cz-customizable', function() {
expect(getQuestion(2).when({type: 'WIP'})).toEqual(false);
expect(getQuestion(2).when({type: 'wip'})).toEqual(false);

//question 5
expect(getQuestion(5).name).toEqual('breaking');
expect(getQuestion(5).when({type: 'feat'})).toEqual(true);
expect(getQuestion(5).when({type: 'fix'})).toEqual(true);
expect(getQuestion(5).when({type: 'FIX'})).toEqual(true);
//question 6
expect(getQuestion(6).name).toEqual('breaking');
expect(getQuestion(6).when({type: 'feat'})).toEqual(true);
expect(getQuestion(6).when({type: 'fix'})).toEqual(true);
expect(getQuestion(6).when({type: 'FIX'})).toEqual(true);

var answers = {
confirmCommit: true,
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature'
};
expect(getQuestion(7).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
expect(getQuestion(8).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
});
});

describe('optional scopes', function() {

beforeEach(function() {
module.__set__({
readConfigFile: function() {
return {
types: [{value: 'feat', name: 'feat: my feat'}],
scopeOverrides: {
feat: [{name: 'myScope'}]
}
};
}
});

cz = jasmine.createSpyObj('cz', ['prompt', 'Separator']);
commit = jasmine.createSpy();
});


it('should call cz.prompt with questions', function() {
module.prompter(cz, commit);

var getQuestion = function(number) {
return cz.prompt.mostRecentCall.args[0][number - 1];
};

//question 2
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);
(function () {
var answers = {type: 'fix'};
expect(getQuestion(2).when(answers)).toEqual(false);
expect(answers.scope).toEqual('custom');
})();

});
});

Expand Down

0 comments on commit 2906b07

Please sign in to comment.