Add test coverage for `validatesInclusionOf` (#1249)

* Fix indentation

* Improve tests formatting

* Use instanceof and should.match
This commit is contained in:
Rémi Bèges 2017-03-18 11:50:10 +01:00 committed by Sakib Hasan
parent edc1fd3d83
commit 55128f0c9b
1 changed files with 49 additions and 1 deletions

View File

@ -610,7 +610,55 @@ describe('validations', function() {
});
describe('inclusion', function() {
it('should validate inclusion');
it('fails when included value is not used for property', function(done) {
User.validatesInclusionOf('name', {in: ['bob', 'john']});
User.create({name: 'bobby'}, function(err) {
err.should.be.instanceof(Error);
err.details.messages.should.match({name: /is not included in the list/});
done();
});
});
it('passes when included value is used for property', function(done) {
User.validatesInclusionOf('name', {in: ['bob', 'john']});
User.create({name: 'bob'}, function(err, user) {
if (err) return done(err);
user.name.should.eql('bob');
done();
});
});
it('fails with a custom error message', function(done) {
User.validatesInclusionOf('name', {in: ['bob', 'john'], message: 'not used'});
User.create({name: 'dude'}, function(err) {
err.should.be.instanceof(Error);
err.details.messages.should.match({name: /not used/});
done();
});
});
it('fails with a null value when allowNull is false', function(done) {
User.validatesInclusionOf('name', {in: ['bob'], allowNull: false});
User.create({name: null}, function(err) {
err.should.be.instanceof(Error);
err.details.messages.should.match({name: /is null/});
done();
});
});
it('passes with a null value when allowNull is true', function(done) {
User.validatesInclusionOf('name', {in: ['bob'], allowNull: true});
User.create({name: null}, done);
});
it('fails if value is used for integer property', function(done) {
User.validatesInclusionOf('age', {in: [123, 456]});
User.create({age: 789}, function(err) {
err.should.be.instanceof(Error);
err.details.messages.should.match({age: /is not included in the list/});
done();
});
});
});
describe('exclusion', function() {