@incdevco/framework
Version:
node.js lambda framework
115 lines (71 loc) • 2.22 kB
JavaScript
var Expect = require('chai').expect;
var StringValidator = require('../validators/is-string');
var Validator = require('./index');
describe('validator', function () {
'use strict';
var expected, validator;
beforeEach(function () {
expected = 'expected';
validator = new Validator();
});
describe('validate', function () {
it('should reject when required is true and input is null', function (done) {
expected = new Error('Required');
validator.required = true;
validator.validate(null)
.then(function () {
throw new Error('succeeded');
})
.catch(function (actual) {
Expect(actual).to.deep.equal(expected, 'result');
done();
})
.catch(done);
});
it('should resolve true when required is false and input is null', function (done) {
validator.validate(null)
.then(function (actual) {
Expect(actual).to.be.ok;
done();
})
.catch(done);
});
it('should loop through validators for each key when validators is object', function(done) {
validator.validators = {
test: new Validator({
validators: [
new StringValidator()
]
})
};
validator.validate({
test: 'test'
})
.then(function (actual) {
Expect(actual).to.be.ok;
done();
})
.catch(done);
});
it('should set the key on the exception when validators is an object and fails validation', function(done) {
validator.validators = {
test: new Validator({
validators: [
new StringValidator()
]
})
};
validator.validate({
test: 2
})
.then(function () {
throw new Error('succeeded');
})
.catch(function (actual) {
Expect(actual.key).to.equal('test', 'key');
done();
})
.catch(done);
});
});
});