-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add validations coverage (#10)
- Loading branch information
1 parent
c2e878f
commit 145ee4e
Showing
4 changed files
with
157 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
coverage/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
var _ = require('lodash'); | ||
var assert = require('assert'); | ||
var should = require('chai').should(); | ||
var Model = relativeRequire('Model'); | ||
|
||
defineTest('validations.js', function (validations) { | ||
var mapIntoFunctions = function (values, validate, model) { | ||
return values.map(value => () => validate.call(model, value)); | ||
}; | ||
|
||
var testPassingValues = function () { | ||
mapIntoFunctions.apply(null, arguments).forEach(f => f.should.not.throw()); | ||
}; | ||
|
||
var testFailingValues = function () { | ||
mapIntoFunctions.apply(null, arguments).forEach(f => f.should.throw()); | ||
}; | ||
|
||
describe('#undefined', function () { | ||
var validate = validations.undefined.__default; | ||
|
||
it('should pass when undefined', function () { | ||
testPassingValues([undefined], validate); | ||
}); | ||
|
||
it('should fail when not undefined', function () { | ||
testFailingValues([null, false, 1, 'foo', {}, [], new Date()], validate); | ||
}); | ||
}); | ||
|
||
describe('#boolean', function () { | ||
var validate = validations.boolean.__default; | ||
|
||
it('should pass when boolean', function () { | ||
testPassingValues([true, false], validate); | ||
}); | ||
|
||
it('should fail when not boolean', function () { | ||
testFailingValues([null, 0, 'true', {}, []], validate); | ||
}); | ||
}); | ||
|
||
describe('#number', function () { | ||
var validate = validations.number.__default; | ||
|
||
it('should pass when number', function () { | ||
testPassingValues([0, 1.232, 1253252, -9988723], validate); | ||
}); | ||
|
||
it('should fail when not number', function () { | ||
testFailingValues([null, true, '', 'true', new Date()], validate); | ||
}); | ||
}); | ||
|
||
describe('#number', function () { | ||
var validate = validations.number.__default; | ||
|
||
it('should pass when number', function () { | ||
testPassingValues([0, 1.232, 1253252, -9988723], validate); | ||
}); | ||
|
||
it('should fail when not number', function () { | ||
testFailingValues([null, true, '', 'true', new Date()], validate); | ||
}); | ||
}); | ||
|
||
describe('#string', function () { | ||
var validate = validations.string.__default; | ||
|
||
it('should pass when string', function () { | ||
testPassingValues(['', 'foo', 'something long', '123'], validate); | ||
}); | ||
|
||
it('should fail when not string', function () { | ||
testFailingValues([null, true, 12, 0, {}, new Date()], validate); | ||
}); | ||
}); | ||
|
||
describe('#date', function () { | ||
var validate = validations.date.__default; | ||
|
||
it('should pass when date', function () { | ||
testPassingValues([new Date(), new Date('2016-01-12')], validate); | ||
}); | ||
|
||
it('should fail when not date', function () { | ||
testFailingValues([null, true, 12, 0, {}, '2011-01-01', new Date('foo')], validate); | ||
}); | ||
}); | ||
|
||
describe('#object', function () { | ||
var validate = validations.object.__default; | ||
|
||
it('should pass when object', function () { | ||
testPassingValues([new Date(), {foo: 'bar'}, null, []], validate); | ||
}); | ||
|
||
it('should fail when not object', function () { | ||
testFailingValues([true, 12, '2011-01-01'], validate); | ||
}); | ||
}); | ||
|
||
describe('#array', function () { | ||
var validate = validations.array.__default; | ||
|
||
it('should pass when array', function () { | ||
testPassingValues([[], [1, 2, 3]], validate); | ||
}); | ||
|
||
it('should fail when not array', function () { | ||
testFailingValues([null, true, 1, 'array', {'0': 1, '1': 2}], validate); | ||
}); | ||
}); | ||
|
||
}); |