Skip to content

Commit

Permalink
fix: Do not apply user defaults when question.choices is a function. Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanvanoss authored and SBoudrias committed Dec 21, 2017
1 parent 825305e commit f7b8167
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/util/prompt-suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
84 changes: 84 additions & 0 deletions test/prompt-suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit f7b8167

Please sign in to comment.