Skip to content

Commit c48cb63

Browse files
committed
Add more tests
1 parent fcf390a commit c48cb63

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

lib/src/Gren.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,12 @@ class Gren {
637637
_groupByLabel(issues) {
638638
const groups = [];
639639

640-
issues.forEach(issue => {
641-
if (!issue.labels.length && this.options.template.noLabel) {
640+
Object.values(ObjectAssign({}, issues)).forEach(issue => {
641+
if (!issue.labels.length) {
642+
if (!this.options.template.noLabel) {
643+
return;
644+
}
645+
642646
issue.labels.push({name: this.options.template.noLabel});
643647
}
644648

@@ -664,8 +668,9 @@ class Gren {
664668
*
665669
* @return {Array}
666670
*/
667-
_groupBy(issues) {
671+
_groupBy(passedIssues) {
668672
const { groupBy } = this.options;
673+
const issues = Object.values(ObjectAssign({}, passedIssues));
669674

670675
if (!groupBy) {
671676
return issues.map(this._templateIssue.bind(this));
@@ -675,14 +680,14 @@ class Gren {
675680
return this._groupByLabel(issues);
676681
}
677682

678-
if (typeof groupBy !== 'object') {
683+
if (typeof groupBy !== 'object' || Array.isArray(groupBy)) {
679684
throw chalk.red('The option for groupBy is invalid, please check the documentation');
680685
}
681686

682687
const allLabels = Object.values(groupBy).reduce((carry, group) => carry.concat(group), []);
683688

684689
const groups = Object.keys(groupBy).reduce((carry, group) => {
685-
const groupIssues = Array.from(issues).filter(issue => {
690+
const groupIssues = issues.filter(issue => {
686691
if (!issue.labels.length && this.options.template.noLabel) {
687692
issue.labels.push({name: this.options.template.noLabel});
688693
}
@@ -759,7 +764,7 @@ class Gren {
759764
return this._getClosedIssues(releaseRanges)
760765
.then(issues => releaseRanges
761766
.map(range => {
762-
const filteredIssues = issues
767+
const filteredIssues = Array.from(issues)
763768
.filter(this._filterIssue.bind(this))
764769
.filter(this._filterBlockIssue.bind(this, range));
765770
const body = (!range[0].body || this.options.override) && this._groupBy(filteredIssues);

test/Gren.spec.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ describe('Gren', () => {
115115

116116
describe('_groupBy, _groupByLabel', () => {
117117
it('Should return the just templated issues', () => {
118-
const { normal } = issues;
118+
const { normal, noLabel } = issues;
119119

120120
gren.options.groupBy = false;
121121
assert.deepEqual(gren._groupBy(normal), [normal[0].title], 'The groupBy option is false');
122+
assert.deepEqual(gren._groupBy(noLabel), [noLabel[0].title], 'The groupBy option is false');
122123
});
123124

124125
it('Should return the group based on issue labels', () => {
@@ -129,9 +130,34 @@ describe('Gren', () => {
129130
assert.deepEqual(gren._groupBy(noLabel), [`closed\n${noLabel[0].title}`], 'Group option is "label" with no labels');
130131
});
131132

133+
it('Should not return labels without labels', () => {
134+
const { normal, noLabel } = issues;
135+
136+
gren.options.template.noLabel = false;
137+
gren.options.groupBy = 'label';
138+
139+
assert.lengthOf(gren._groupBy(noLabel), 0, 'When the noLabel is false and only an issue with no labels has been passed');
140+
assert.lengthOf(gren._groupBy(noLabel.concat(normal)), 1, 'When the noLabel is false and only one issue with labels has been passed');
141+
});
142+
143+
it('Should throw an error', () => {
144+
const error = chalk.red('The option for groupBy is invalid, please check the documentation');
145+
146+
gren.options.groupBy = 'an unrecognised string';
147+
assert.throws(gren._groupBy.bind(gren, issues), error, 'Passing an unrecognised string');
148+
149+
gren.options.groupBy = [1, 2, 3];
150+
assert.throws(gren._groupBy.bind(gren, issues), error, 'Passing an Array');
151+
152+
gren.options.groupBy = 123;
153+
assert.throws(gren._groupBy.bind(gren, issues), error, 'Passing a number');
154+
});
155+
132156
it('Should group the issues based on the option', () => {
133157
const { normal, noLabel } = issues;
134158

159+
gren.options.template.noLabel = 'closed';
160+
135161
gren.options.groupBy = {
136162
'Test': ['enhancement'],
137163
'Others': ['closed']

0 commit comments

Comments
 (0)