Skip to content

Commit

Permalink
Add support for numeric timestamps on DateAssert
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Madeiras committed Apr 27, 2016
1 parent b787895 commit ddf9ab1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/asserts/date-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ export default function dateAssert({ format } = {}) {
*/

this.validate = value => {
if (typeof value === 'number') {
if (new Date(value).toString() === 'Invalid Date') {
throw new Violation(this, value);
}

return true;
}

if (typeof value !== 'string' && Object.prototype.toString.call(value) !== '[object Date]') {
throw new Violation(this, value, { value: 'must_be_a_date_or_a_string' });
throw new Violation(this, value, { value: 'must_be_a_date_or_a_string_or_a_number' });
}

if (isNaN(Date.parse(value)) === true) {
Expand Down
31 changes: 25 additions & 6 deletions test/asserts/date-assert_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const Assert = BaseAssert.extend({
*/

describe('DateAssert', () => {
it('should throw an error if the input value is not a string or a date', () => {
const choices = [[], {}, 123];
it('should throw an error if the input value is not a date or a string or a number', () => {
const choices = [[], {}];

choices.forEach(choice => {
try {
Expand All @@ -30,7 +30,7 @@ describe('DateAssert', () => {
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.violation.value.should.equal('must_be_a_date_or_a_string');
e.violation.value.should.equal('must_be_a_date_or_a_string_or_a_number');
}
});
});
Expand Down Expand Up @@ -61,6 +61,17 @@ describe('DateAssert', () => {
}
});

it('should throw an error if the input value is an invalid timestamp', () => {
try {
new Assert().Date().validate(-Number.MAX_VALUE);

should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('Date');
}
});

it('should throw an error if value does not pass strict validation', () => {
try {
new Assert().Date({ format: 'YYYY-MM-DD' }).validate('2000.12.30');
Expand All @@ -82,15 +93,23 @@ describe('DateAssert', () => {
}
});

it('should accept a `Date`', () => {
it('should accept an instance of `Date`', () => {
new Assert().Date().validate(new Date());
});

it('should accept a correctly formatted date', () => {
new Assert().Date({ format: 'YYYY-MM-DD' }).validate('2000-12-30');
});

it('should accept a `string`', () => {
new Assert().Date().validate('2014-10-16');
it('should accept a string date', () => {
new Assert().Date().validate('2016-04-23');
});

it('should accept an ISO-8601 string date', () => {
new Assert().Date().validate('2016-04-23T00:51:18.570Z');
});

it('should accept a numeric timestamp', () => {
new Assert().Date().validate(Date.now());
});
});

0 comments on commit ddf9ab1

Please sign in to comment.