From 45af563156f2e38e9a5b37921bccf4c426a4289d Mon Sep 17 00:00:00 2001 From: Alex Maras <49382733+bibli-alex@users.noreply.github.com> Date: Sun, 15 Dec 2019 18:25:38 +0800 Subject: [PATCH] Bugfix: Choices.push() breaks index if a disabled item was already in 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 --- packages/inquirer/lib/objects/choices.js | 4 +++- packages/inquirer/test/specs/objects/choices.js | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/inquirer/lib/objects/choices.js b/packages/inquirer/lib/objects/choices.js index 205ede2e7..e3c16d05e 100644 --- a/packages/inquirer/lib/objects/choices.js +++ b/packages/inquirer/lib/objects/choices.js @@ -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; } }; diff --git a/packages/inquirer/test/specs/objects/choices.js b/packages/inquirer/test/specs/objects/choices.js index 4bd3b9514..208c7b21f 100644 --- a/packages/inquirer/test/specs/objects/choices.js +++ b/packages/inquirer/test/specs/objects/choices.js @@ -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); }); });