Skip to content

Commit

Permalink
Merge pull request #542 from mschaaf/patch-1
Browse files Browse the repository at this point in the history
Collect prompt validation errors on generator
  • Loading branch information
SBoudrias committed May 7, 2014
2 parents ff1039c + e8ea863 commit 3673061
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ exports.mockPrompt = function (generator, answers) {
if (!(prompt.name in answers)) {
answers[prompt.name] = prompt.default;
}

if (_.isFunction(prompt.validate)) {
var validation = prompt.validate(answers[prompt.name]);
if (validation !== true) {
if (generator.prompt.errors == null) {
generator.prompt.errors = [];
}
generator.prompt.errors.push({
name: prompt.name,
message: validation
});
}
}
});
setTimeout(done.bind(null, answers), 0);
};
Expand Down
31 changes: 31 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,37 @@ describe('yeoman.test', function () {
});
val.push(1);
});

it('does not add errors if validation pass', function (done) {
helpers.mockPrompt(this.generator, [{ answer1: 'foo' }, { answer2: 'foo' }]);
this.generator.prompt([{ name: 'answser1', type: 'input' }, { name: 'answser2', type: 'input', validate: { not: 'aFunction' }}], function () {
assert.ok(this.generator.prompt.errors == null, 'The errors array should not be attached in case of no validation error.');
done();
}.bind(this));
});

it('validation function returns no validation error', function (done) {
var validationTrue = function () {
return true;
};
this.generator.prompt({ name: 'answser', type: 'input', validate: validationTrue }, function () {
assert.ok(this.generator.prompt.errors == null, 'The errors array should not be attached in case of no validation error.');
done();
}.bind(this));
});

it('validation function returns validation error', function (done) {
var validationError = function () {
return 'error message';
};
this.generator.prompt({ name: 'answer', type: 'input', validate: validationError }, function () {
assert.ok(this.generator.prompt.errors != null, 'The errors array should be attached in case of a validation error.');
assert.equal(this.generator.prompt.errors.length, 1, 'There should be only 1 error attached.');
assert.equal(this.generator.prompt.errors[0].name, 'answer', 'The error is not for the expected prompt.');
assert.equal(this.generator.prompt.errors[0].message, 'error message', 'The returned error message is not expected.');
done();
}.bind(this));
});
});

describe('.before()', function () {
Expand Down

0 comments on commit 3673061

Please sign in to comment.