diff --git a/lib/util/prompt-suggestion.js b/lib/util/prompt-suggestion.js index ca2c5039..4e391e1f 100644 --- a/lib/util/prompt-suggestion.js +++ b/lib/util/prompt-suggestion.js @@ -115,7 +115,9 @@ promptSuggestion.prefillQuestions = (store, questions) => { const storedValue = promptValues[question.name]; - if (storedValue === undefined) { + if ((storedValue === undefined) || _.isFunction(question.choices)) { + // Do not override prompt default when question.choices is a function, + // since can't guarantee that the `storedValue` will even be in the returned choices return question; } diff --git a/test/prompt-suggestion.js b/test/prompt-suggestion.js index ff857439..81e05d5a 100644 --- a/test/prompt-suggestion.js +++ b/test/prompt-suggestion.js @@ -179,6 +179,90 @@ describe('PromptSuggestion', () => { }); }); + describe('take a checkbox with choices from a function', () => { + beforeEach(function () { + this.store.set('promptValues', { + respuesta: ['foo'] + }); + }); + + it('does not override default from an array with objects', function () { + const question = { + type: 'checkbox', + name: 'respuesta', + default: ['bar'], + store: true, + choices: () => [{ + value: 'foo', + name: 'foo' + }, new inquirer.Separator('spacer'), { + value: 'bar', + name: 'bar' + }, { + value: 'baz', + name: 'baz' + }] + }; + const result = promptSuggestion.prefillQuestions(this.store, question)[0]; + + assert.deepEqual(result.default, ['bar']); + }); + + it('does not override default from an array with strings', function () { + const question = { + type: 'checkbox', + name: 'respuesta', + default: ['bar'], + store: true, + choices: () => ['foo', new inquirer.Separator('spacer'), 'bar', 'baz'] + }; + const result = promptSuggestion.prefillQuestions(this.store, question)[0]; + assert.deepEqual(result.default, ['bar']); + }); + + describe('does not override even with multiple defaults', () => { + beforeEach(function () { + this.store.set('promptValues', { + respuesta: ['foo', 'bar'] + }); + }); + + it('from an array with objects', function () { + const question = { + type: 'checkbox', + name: 'respuesta', + default: ['bar'], + store: true, + choices: () => [{ + value: 'foo', + name: 'foo' + }, new inquirer.Separator('spacer'), { + value: 'bar', + name: 'bar' + }, { + value: 'baz', + name: 'baz' + }] + }; + const result = promptSuggestion.prefillQuestions(this.store, question)[0]; + + assert.deepEqual(result.default, ['bar']); + }); + + it('from an array with strings', function () { + const question = { + type: 'checkbox', + name: 'respuesta', + default: ['bar'], + store: true, + choices: () => ['foo', new inquirer.Separator('spacer'), 'bar', 'baz'] + }; + const result = promptSuggestion.prefillQuestions(this.store, question)[0]; + assert.deepEqual(result.default, ['bar']); + }); + }); + }); + describe('take a rawlist / expand', () => { beforeEach(function () { this.store.set('promptValues', {