Skip to content

Commit

Permalink
ensure options.initial is handled properly when defined as a function
Browse files Browse the repository at this point in the history
Pointed out here: #295
  • Loading branch information
jonschlinkert committed Jul 26, 2023
1 parent 60a1cdd commit 6f3decc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/prompts/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const BooleanPrompt = require('../types/boolean');
class TogglePrompt extends BooleanPrompt {
async initialize() {
await super.initialize();
this.value = this.initial = !!this.options.initial;
this.value = this.initial = this.resolve(this.options.initial);
this.disabled = this.options.disabled || 'no';
this.enabled = this.options.enabled || 'yes';
await this.render();
Expand Down
28 changes: 28 additions & 0 deletions test/promt.toggle.js → test/prompt.toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ describe('toggle', function() {
assert.equal(answer, true);
});
});

it('toggle should call options.initial when a function is defined (false)', () => {
prompt = new Prompt({
message: 'prompt-toggle',
initial: () => false
});

prompt.once('run', () => prompt.submit());

return prompt.run()
.then(answer => {
assert.equal(answer, false);
});
});

it('toggle should call options.initial when a function is defined (true)', () => {
prompt = new Prompt({
message: 'prompt-toggle',
initial: () => true
});

prompt.once('run', () => prompt.submit());

return prompt.run()
.then(answer => {
assert.equal(answer, true);
});
});
});

describe('key handling', () => {
Expand Down

0 comments on commit 6f3decc

Please sign in to comment.