Skip to content

Commit

Permalink
feat: add async validator test (Close #39) (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
loopingz committed Oct 31, 2020
1 parent 4b273f8 commit 917930b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ async function prompt(message, options) {

// Validator verification
try {
value = options.validator.reduce((value, validator) => validator(value), value);
for (const i in options.validator) {
value = await options.validator[i](value);
}
} catch (err) {
// Retry automatically if the retry option is enabled
if (options.retry) {
Expand Down
20 changes: 20 additions & 0 deletions test/prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,23 @@ it('should take input value if not timed out', async () => {
sendLine('plop2', 10);
expect(await promptly.prompt('prompt: ', { timeout: 20, default: 'plop' })).toEqual('plop2');
});

it('should allow async validator', async () => {
sendLine('plop');
expect(await promptly.prompt('prompt: ', { validator: async () => 'fixed' })).toEqual('fixed');
});

it('should allow async validator error', async () => {
let called = false;
const validator = async (val) => {
if (!called) {
called = true;
throw new Error('Not valid');
}

return val;
};

sendLine('plop');
expect(await promptly.prompt('prompt: ', { default: 'fixed', timeout: 10, useDefaultOnTimeout: true, validator })).toEqual('fixed');
});

0 comments on commit 917930b

Please sign in to comment.