Skip to content

Commit

Permalink
test: add validations coverage (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Feb 27, 2018
1 parent c2e878f commit 145ee4e
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/klay/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage/
node_modules/
36 changes: 28 additions & 8 deletions packages/klay/lib/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ var _ = require('lodash');
var assert = require('./assert');

module.exports = {
undefined: {
__default: function (value) {
assert.typeof(value, 'undefined');
}
},
boolean: {
__default: function (value) {
assert.typeof(value, 'boolean');
Expand All @@ -12,16 +17,31 @@ module.exports = {
assert.typeof(value, 'number');
}
},
string: {
__default: function (value) {
assert.typeof(value, 'string');
}
},
date: {
__default: function (value) {
assert.typeof(value, 'date');
assert.ok(!_.isNaN(value.getTime()), 'invalid date');
}
},
object: {
__default: function (value) {
assert.typeof(value, 'object');
}
},
array: {
__default: function (value) {
assert.typeof(value, 'array');
}
},
conditional: {
__default: function (value, root, path) {
var options = this._getOrThrow('options');
var defaultOption = _.find(options, opt => !opt.condition);
var applicableOptions = _.filter(options, opt => (opt.condition || _.noop)(value));
assert.ok(applicableOptions.length || defaultOption, 'no applicable model');
assert.ok(applicableOptions.length <= 1, 'mutiple conditions should not apply');

var option = applicableOptions[0] || defaultOption;
option.model._validate(value, root, path);
var options = this._getApplicableOptions(root);
options[0].model._validate(value, root, path);
}
}
};
16 changes: 13 additions & 3 deletions packages/klay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"scripts": {
"test": "xo ./lib/**/*.js && mocha --reporter spec --require test/bootstrap test/**/*.test.js",
"test-watch": "mocha --watch --reporter spec --require test/bootstrap test/**/*.test.js"
"test-coverage": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- --require test/bootstrap test/**/*.test.js",
"test-watch": "mocha --watch --reporter dot --require test/bootstrap test/**/*.test.js"
},
"repository": {
"type": "git",
Expand All @@ -29,6 +30,7 @@
},
"devDependencies": {
"chai": "^3.5.0",
"istanbul": "^0.4.4",
"mocha": "^2.5.3",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0",
Expand All @@ -42,10 +44,18 @@
],
"space": 2,
"rules": {
"complexity": ["warn", 10],
"complexity": [
"warn",
10
],
"xo/filename-case": 0,
"no-else-return": 0,
"max-statements-per-line": ["error", {"max": 2}],
"max-statements-per-line": [
"error",
{
"max": 2
}
],
"brace-style": [
"error",
"1tbs",
Expand Down
115 changes: 115 additions & 0 deletions packages/klay/test/validations.test.js
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);
});
});

});

0 comments on commit 145ee4e

Please sign in to comment.