Skip to content

Commit

Permalink
Add toBeValidDate matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Jun 2, 2018
1 parent aee660b commit f10311b
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .all-contributorsrc
Expand Up @@ -277,7 +277,9 @@
"avatar_url": "https://avatars2.githubusercontent.com/u/980783?v=4",
"profile": "http://foss-geek.blogspot.com/",
"contributions": [
"code"
"code",
"test",
"doc"
]
},
{
Expand Down
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -53,6 +53,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
* [.toBeFalse()](#tobefalse)
* [Date](#date)
* [.toBeDate()](#tobedate)
* [.toBeValidDate()](#tobevaliddate)
* Further proposals in [#117](https://github.com/jest-community/jest-extended/issues/117) PRs welcome
* [Function](#function)
* [.toBeFunction()](#tobefunction)
Expand Down Expand Up @@ -332,6 +333,20 @@ test('passes when value is a date', () => {
});
```
### .toBeValidDate()
Use `.toBeValidDate` when checking if a given `Date` object is valid.
```
test('passes when Date is valid', () => {
expect(new Date()).toBeValidDate();
expect('01/01/2018').not.toBeValidDate();
expect(new Date('01/01/2018').toBeValidDate();
expect(new Date('01/90/2018').not.toBeValidDate();
expect(undefined).not.toBeValidDate();
});
```
### Function
#### .toBeFunction()
Expand Down
4 changes: 3 additions & 1 deletion src/matchers/index.js
Expand Up @@ -43,6 +43,7 @@ import toBeEmpty from './toBeEmpty';
import toBeSealed from './toBeSealed';
import toIncludeRepeated from './toIncludeRepeated';
import toHaveBeenCalledBefore from './toHaveBeenCalledBefore';
import toBeValidDate from './toBeValidDate';

export default [
toBeEven,
Expand Down Expand Up @@ -89,5 +90,6 @@ export default [
toBeSealed,
toSatisfy,
toIncludeRepeated,
toHaveBeenCalledBefore
toHaveBeenCalledBefore,
toBeValidDate
].reduce((acc, matcher) => ({ ...acc, ...matcher }), {});
3 changes: 3 additions & 0 deletions src/matchers/toBeValidDate/__snapshots__/index.test.js.snap
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.toBeValidDate fails when not given an invalid date 1`] = `"Invalid time value"`;
26 changes: 26 additions & 0 deletions src/matchers/toBeValidDate/index.js
@@ -0,0 +1,26 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = received => () =>
matcherHint('.not.toBeValidDate', 'received', '') +
'\n\n' +
'Expected value to not be a valid date received:\n' +
` ${printReceived(received)}`;

const failMessage = received => () =>
matcherHint('.toBeValidDate', 'received', '') +
'\n\n' +
'Expected value to be a valid date received:\n' +
` ${printReceived(received)}`;

export default {
toBeValidDate: expected => {
const pass = predicate(expected);
if (pass) {
return { pass: true, message: passMessage(expected) };
}

return { pass: false, message: failMessage(expected) };
}
};
33 changes: 33 additions & 0 deletions src/matchers/toBeValidDate/index.test.js
@@ -0,0 +1,33 @@
import each from 'jest-each';

import matcher from './';

expect.extend(matcher);

describe('.toBeValidDate', () => {
test('passes when given a valid date', () => {
expect(new Date()).toBeValidDate();
});

test('fails when not given an invalid date', () => {
expect(() => expect(new Date('31/01/2018')).toBeValidDate()).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeValidDate', () => {
each([
[new Date('01/90/2018')],
[new Date('32/01/2018')],
[false],
[true],
[0],
[''],
[{}],
[() => {}],
[undefined],
[null],
[NaN]
]).test('passes when not given a date: %s', given => {
expect(given).not.toBeValidDate();
});
});
7 changes: 7 additions & 0 deletions src/matchers/toBeValidDate/predicate.js
@@ -0,0 +1,7 @@
let is = type => value => Object.prototype.toString.call(value) === `[object ${type}]`;

let hasDateType = is('Date');

let isValidDate = value => hasDateType(value) && !isNaN(value) && !isNaN(value.getTime());

export default expected => isValidDate(expected);
24 changes: 24 additions & 0 deletions src/matchers/toBeValidDate/predicate.test.js
@@ -0,0 +1,24 @@
import each from 'jest-each';
import predicate from './predicate';

describe('toBeDate Predicate', () => {
test('returns true when given a valid date', () => {
expect(predicate(new Date('12/25/2017'))).toBe(true);
});

each([
[new Date('01/90/2018')],
[new Date('32/01/2018')],
[true],
[false],
[''],
[0],
[{}],
[() => {}],
[undefined],
[null],
[NaN]
]).test('returns false when given: %s', given => {
expect(predicate(given)).toBe(false);
});
});

0 comments on commit f10311b

Please sign in to comment.