Skip to content

Commit

Permalink
Bugfix: Choices.push() breaks index if a disabled item was already in…
Browse files Browse the repository at this point in the history
… the Choices (#869)

* Filters disabled choices from the list after a push, preventing index mismatches

* Adds disabled property to Choices prior to push, testing that the disabled item is not included in the realChoices
  • Loading branch information
bibli-alex authored and SBoudrias committed Dec 15, 2019
1 parent 8248ee5 commit 45af563
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/inquirer/lib/objects/choices.js
Expand Up @@ -110,7 +110,9 @@ module.exports = class Choices {
push() {
var objs = _.map(arguments, val => new Choice(val));
this.choices.push.apply(this.choices, objs);
this.realChoices = this.choices.filter(Separator.exclude);
this.realChoices = this.choices
.filter(Separator.exclude)
.filter(item => !item.disabled);
return this.choices;
}
};
16 changes: 12 additions & 4 deletions packages/inquirer/test/specs/objects/choices.js
Expand Up @@ -72,11 +72,19 @@ describe('Choices collection', function() {
});

it('should façade push and update the realChoices internally', function() {
var choices = new Choices(['a']);
var choices = new Choices(['a', { name: 'b', disabled: true }]);
choices.push('b', new inquirer.Separator());
expect(choices.length).to.equal(3);
expect(choices.length).to.equal(4);
expect(choices.realLength).to.equal(2);
expect(choices.getChoice(1)).to.be.instanceOf(Choice);
expect(choices.get(2)).to.be.instanceOf(inquirer.Separator);
expect(choices.getChoice(0))
.to.be.instanceOf(Choice)
.and.have.property('name', 'a');
expect(choices.getChoice(1))
.to.be.instanceOf(Choice)
.and.have.property('name', 'b');
expect(choices.get(1))
.to.be.instanceOf(Choice)
.and.have.property('disabled', true);
expect(choices.get(3)).to.be.instanceOf(inquirer.Separator);
});
});

0 comments on commit 45af563

Please sign in to comment.