Skip to content

Commit

Permalink
Use helper in tests (1st part)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Sep 11, 2019
1 parent 460b703 commit 64d71c4
Show file tree
Hide file tree
Showing 11 changed files with 574 additions and 637 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
browser
dist
examples
sandbox.js
/benchmarks/node_modules
367 changes: 191 additions & 176 deletions test/base.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('cast', () => {
b: Joi.x('{a + 1}')
});

expect(schema.validate({ a: 5, b: 6 }).error).to.not.exist();
Helper.validate(schema, [[{ a: 5, b: 6 }, true]]);
});

it('compiles null schema', () => {
Expand Down
5 changes: 3 additions & 2 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ exports.validate = function (schema, prefs, tests) {
try {
expect(schema.$_root.build(schema.describe())).to.equal(schema, { deepFunction: true, skip: ['$_temp'] });

for (const [input, pass, expected] of tests) {
for (const test of tests) {
const [input, pass, expected] = test;
if (!pass) {
expect(expected, 'Failing tests messages must be tested').to.exist();
}
Expand All @@ -57,7 +58,7 @@ exports.validate = function (schema, prefs, tests) {

expect(!error).to.equal(pass);

if (expected === undefined) {
if (test.length === 2) {
continue;
}

Expand Down
32 changes: 18 additions & 14 deletions test/types/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,42 @@ describe('boolean', () => {
it('respects case for allow', () => {

const schema = Joi.boolean().allow('X');
expect(schema.validate('X').error).to.not.exist();
expect(schema.validate('x').error).to.be.an.error('"value" must be a boolean');
Helper.validate(schema, [
['X', true],
['x', false, '"value" must be a boolean']
]);
});

describe('cast()', () => {

it('casts value to number', () => {

const schema = Joi.boolean().cast('number');
expect(schema.validate(true).value).to.equal(1);
expect(schema.validate(false).value).to.equal(0);
Helper.validate(schema, [
[true, true, 1],
[false, true, 0]
]);
});

it('casts value to string', () => {

const schema = Joi.boolean().cast('string');
expect(schema.validate(true).value).to.equal('true');
expect(schema.validate(false).value).to.equal('false');
Helper.validate(schema, [
[true, true, 'true'],
[false, true, 'false']
]);
});

it('ignores null', () => {

const schema = Joi.boolean().allow(null).cast('string');
expect(schema.validate(null).value).to.be.null();
Helper.validate(schema, [[null, true, null]]);
});

it('ignores string', () => {

const schema = Joi.boolean().allow('x').cast('string');
expect(schema.validate('x').value).to.equal('x');
Helper.validate(schema, [['x', true, 'x']]);
});
});

Expand Down Expand Up @@ -203,7 +209,7 @@ describe('boolean', () => {
it('should default to case insensitive', () => {

const schema = Joi.boolean().truthy('Y');
expect(schema.validate('y').error).not.to.exist();
Helper.validate(schema, [['y', true, true]]);
});

it('should stick to case insensitive if called', () => {
Expand All @@ -215,14 +221,12 @@ describe('boolean', () => {
it('should be able to do strict comparison', () => {

const schema = Joi.boolean().truthy('Y').sensitive();
const error = schema.validate('y').error;
expect(error).to.be.an.error('"value" must be a boolean');
expect(error.details).to.equal([{
Helper.validate(schema, [['y', false, {
message: '"value" must be a boolean',
path: [],
type: 'boolean.base',
context: { label: 'value', value: 'y' }
}]);
}]]);
});

it('should return the same instance if nothing changed', () => {
Expand Down Expand Up @@ -599,7 +603,7 @@ describe('boolean', () => {
it('errors on truthy without convert', () => {

const schema = Joi.boolean().truthy('y');
expect(schema.validate('y', { convert: false }).error).be.an.error('"value" must be a boolean');
Helper.validate(schema, { convert: false }, [['y', false, '"value" must be a boolean']]);
});
});
});
110 changes: 51 additions & 59 deletions test/types/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,33 @@ describe('date', () => {
it('matches specific date', () => {

const now = Date.now();
expect(Joi.date().valid(new Date(now)).validate(new Date(now)).error).to.not.exist();
expect(Joi.date().valid(new Date(now)).validate(new Date(now).toISOString()).error).to.not.exist();
Helper.validate(Joi.date().valid(new Date(now)), [
[new Date(now), true],
[new Date(now).toISOString(), true]
]);
});

it('errors on invalid input and convert disabled', () => {

const err = Joi.date().prefs({ convert: false }).validate('1-1-2013 UTC').error;
expect(err).to.be.an.error('"value" must be a valid date');
expect(err.details).to.equal([{
Helper.validate(Joi.date().prefs({ convert: false }), [['1-1-2013 UTC', false, {
message: '"value" must be a valid date',
path: [],
type: 'date.base',
context: { label: 'value', value: '1-1-2013 UTC' }
}]);
}]]);
});

it('validates date', () => {

expect(Joi.date().validate(new Date()).error).to.not.exist();
Helper.validate(Joi.date(), [[new Date(), true]]);
});

it('validates millisecond date as a string', () => {

const now = new Date();
const mili = now.getTime();

expect(Joi.date().validate(mili.toString())).to.equal({ value: now });
Helper.validate(Joi.date(), [[mili.toString(), true, now]]);
});

it('validates only valid dates', () => {
Expand Down Expand Up @@ -146,31 +146,31 @@ describe('date', () => {
it('casts value to number', () => {

const schema = Joi.date().cast('number');
expect(schema.validate(new Date('1974-05-07')).value).to.equal(137116800000);
Helper.validate(schema, [[new Date('1974-05-07'), true, 137116800000]]);
});

it('casts value to string', () => {

const schema = Joi.date().cast('string');
expect(schema.validate(new Date('1974-05-07')).value).to.equal('1974-05-07T00:00:00.000Z');
Helper.validate(schema, [[new Date('1974-05-07'), true, '1974-05-07T00:00:00.000Z']]);
});

it('casts value to string (custom format)', () => {

const schema = Joi.date().prefs({ dateFormat: 'date' }).cast('string');
expect(schema.validate(new Date('1974-05-07')).value).to.equal('Tue May 07 1974');
Helper.validate(schema, [[new Date('1974-05-07'), true, 'Tue May 07 1974']]);
});

it('ignores null', () => {

const schema = Joi.date().allow(null).cast('string');
expect(schema.validate(null).value).to.be.null();
Helper.validate(schema, [[null, true, null]]);
});

it('ignores string', () => {

const schema = Joi.date().allow('x').cast('string');
expect(schema.validate('x').value).to.equal('x');
Helper.validate(schema, [['x', true, 'x']]);
});
});

Expand All @@ -193,9 +193,11 @@ describe('date', () => {
}
});

expect(custom.date().format('unknown').validate('x').error).to.be.an.error('"value" must be in unknown format');
expect(custom.date().format(['unknown']).validate('x').error).to.be.an.error('"value" must be in [unknown] format');
expect(custom.date().format('unknown').validate(Date.now()).error).to.not.exist();
Helper.validate(custom.date().format('unknown'), [
['x', false, '"value" must be in unknown format'],
[Date.now(), true]
]);
Helper.validate(custom.date().format(['unknown']), [['x', false, '"value" must be in [unknown] format']]);
});
});

Expand Down Expand Up @@ -251,15 +253,14 @@ describe('date', () => {
const now = Date.now();
const past = new Date(now - 1000000);

const err = Joi.date().greater('now').validate(past).error;
const message = '"value" must be greater than "now"';
expect(err).to.be.an.error(message);
expect(err.details).to.equal([{
message: '"value" must be greater than "now"',
path: [],
type: 'date.greater',
context: { limit: 'now', label: 'value', value: past }
}]);
Helper.validate(Joi.date().greater('now'), [
[past, false, {
message: '"value" must be greater than "now"',
path: [],
type: 'date.greater',
context: { limit: 'now', label: 'value', value: past }
}]
]);
});

it('accepts references as greater date', () => {
Expand Down Expand Up @@ -480,20 +481,20 @@ describe('date', () => {
it('validates isoDate with a friendly error message', () => {

const schema = { item: Joi.date().iso() };
const err = Joi.compile(schema).validate({ item: 'something' }).error;
expect(err.message).to.equal('"item" must be in ISO 8601 date format');
expect(err.details).to.equal([{
message: '"item" must be in ISO 8601 date format',
path: ['item'],
type: 'date.format',
context: { label: 'item', key: 'item', value: 'something', format: 'iso' }
}]);
Helper.validate(Joi.compile(schema), [
[{ item: 'something' }, false, {
message: '"item" must be in ISO 8601 date format',
path: ['item'],
type: 'date.format',
context: { label: 'item', key: 'item', value: 'something', format: 'iso' }
}]
]);
});

it('validates isoDate after clone', () => {

const schema = { item: Joi.date().iso().clone() };
expect(Joi.compile(schema).validate({ item: '2013-06-07T14:21:46.295Z' }).error).to.not.exist();
Helper.validate(Joi.compile(schema), [[{ item: '2013-06-07T14:21:46.295Z' }, true]]);
});
});

Expand Down Expand Up @@ -547,23 +548,20 @@ describe('date', () => {
it('accepts "now" as the less date', () => {

const past = new Date(Date.now() - 1000000);
expect(Joi.date().less('now').validate(past)).to.equal({ value: past });
Helper.validate(Joi.date().less('now'), [[past, true, past]]);
});

it('errors if .less("now") is used with a future date', () => {

const now = Date.now();
const future = new Date(now + 1000000);

const err = Joi.date().less('now').validate(future).error;
const message = '"value" must be less than "now"';
expect(err).to.be.an.error(message);
expect(err.details).to.equal([{
Helper.validate(Joi.date().less('now'), [[future, false, {
message: '"value" must be less than "now"',
path: [],
type: 'date.less',
context: { limit: 'now', label: 'value', value: future }
}]);
}]]);
});

it('accepts references as less date', () => {
Expand Down Expand Up @@ -700,23 +698,20 @@ describe('date', () => {
it('accepts "now" as the max date', () => {

const past = new Date(Date.now() - 1000000);
expect(Joi.date().max('now').validate(past)).to.equal({ value: past });
Helper.validate(Joi.date().max('now'), [[past, true, past]]);
});

it('errors if .max("now") is used with a future date', () => {

const now = Date.now();
const future = new Date(now + 1000000);

const err = Joi.date().max('now').validate(future).error;
const message = '"value" must be less than or equal to "now"';
expect(err).to.be.an.error(message);
expect(err.details).to.equal([{
message,
Helper.validate(Joi.date().max('now'), [[future, false, {
message: '"value" must be less than or equal to "now"',
path: [],
type: 'date.max',
context: { limit: 'now', label: 'value', value: future }
}]);
}]]);
});

it('accepts references as max date', () => {
Expand Down Expand Up @@ -877,15 +872,12 @@ describe('date', () => {
const now = Date.now();
const past = new Date(now - 1000000);

const err = Joi.date().min('now').validate(past).error;
const message = '"value" must be larger than or equal to "now"';
expect(err).to.be.an.error(message);
expect(err.details).to.equal([{
message,
Helper.validate(Joi.date().min('now'), [[past, false, {
message: '"value" must be larger than or equal to "now"',
path: [],
type: 'date.min',
context: { limit: 'now', label: 'value', value: past }
}]);
}]]);
});

it('accepts references as min date', () => {
Expand Down Expand Up @@ -1036,19 +1028,19 @@ describe('date', () => {
const now = new Date();
const milliseconds = now.getTime();

expect(Joi.date().timestamp().validate(milliseconds)).to.equal({ value: now });
expect(Joi.date().timestamp('javascript').validate(milliseconds)).to.equal({ value: now });
expect(Joi.date().timestamp('unix').timestamp('javascript').validate(milliseconds)).to.equal({ value: now });
Helper.validate(Joi.date().timestamp(), [[milliseconds, true, now]]);
Helper.validate(Joi.date().timestamp('javascript'), [[milliseconds, true, now]]);
Helper.validate(Joi.date().timestamp('unix').timestamp('javascript'), [[milliseconds, true, now]]);
});

it('validates unix timestamp', () => {

const now = new Date();
const seconds = now.getTime() / 1000;

expect(Joi.date().timestamp('unix').validate(seconds)).to.equal({ value: now });
expect(Joi.date().timestamp().timestamp('unix').validate(seconds)).to.equal({ value: now });
expect(Joi.date().timestamp('javascript').timestamp('unix').validate(seconds)).to.equal({ value: now });
Helper.validate(Joi.date().timestamp('unix'), [[seconds, true, now]]);
Helper.validate(Joi.date().timestamp().timestamp('unix'), [[seconds, true, now]]);
Helper.validate(Joi.date().timestamp('javascript').timestamp('unix'), [[seconds, true, now]]);
});

it('validates timestamps with decimals', () => {
Expand Down
Loading

0 comments on commit 64d71c4

Please sign in to comment.