Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toBeValidDate matcher #133

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
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
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toBeBoolean()](#tobeboolean)
- [.toBeTrue()](#tobetrue)
- [.toBeFalse()](#tobefalse)
- [~~Date~~](#date)
- [.toBeDate()](#tobedate)
- [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)
- [Mock](#mock)
Expand Down Expand Up @@ -333,6 +335,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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import toBeSealed from './toBeSealed';
import toIncludeRepeated from './toIncludeRepeated';
import toHaveBeenCalledBefore from './toHaveBeenCalledBefore';
import toBeDate from './toBeDate';
import toBeValidDate from './toBeValidDate';

export default [
toBeEven,
Expand Down Expand Up @@ -91,5 +92,6 @@ export default [
toSatisfy,
toIncludeRepeated,
toHaveBeenCalledBefore,
toBeDate
toBeDate,
toBeValidDate
].reduce((acc, matcher) => ({ ...acc, ...matcher }), {});
19 changes: 6 additions & 13 deletions src/matchers/toBeDate/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@ describe('.toBeDate', () => {
});

describe('.not.toBeDate', () => {
each([
[false],
[true],
[0],
[''],
[{}],
[() => {}],
[undefined],
[null],
[NaN]
]).test('passes when not given a date: %s', given => {
expect(given).not.toBeDate();
});
each([[false], [true], [0], [''], [{}], [() => {}], [undefined], [null], [NaN]]).test(
'passes when not given a date: %s',
given => {
expect(given).not.toBeDate();
}
);

test('fails when given a date', () => {
expect(() => expect(new Date('2018-01-01T13:00:00.000Z')).not.toBeDate()).toThrowErrorMatchingSnapshot();
Expand Down
10 changes: 3 additions & 7 deletions src/matchers/toBeDate/predicate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
function is(type) {
return value => Object.prototype.toString.call(value) === `[object ${type}]`;
}
import { is } from '../../utils';

let hasDateType = is('Date');

function isDate(value) {
return hasDateType(value) && !isNaN(value);
}
const isDate = value => hasDateType(value) && !isNaN(value);

export default expected => isDate(expected);
export default isDate;
17 changes: 17 additions & 0 deletions src/matchers/toBeValidDate/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeValidDate fails when given a valid date value 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeValidDate(</><dim>)</>

Expected value to not be a valid date received:
<red>2018-01-01T13:00:00.000Z</>"
`;

exports[`.toBeValidDate fails when given an invalid date 1`] = `"Invalid time value"`;

exports[`.toBeValidDate fails when not given non-date values 1`] = `
"<dim>expect(</><red>received</><dim>).toBeValidDate(</><dim>)</>

Expected value to be a valid date received:
<red>1</>"
`;
26 changes: 26 additions & 0 deletions src/matchers/toBeValidDate/index.js
Original file line number Diff line number Diff line change
@@ -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) };
}
};
41 changes: 41 additions & 0 deletions src/matchers/toBeValidDate/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 given an invalid date', () => {
expect(() => expect(new Date('2018-90-90T13:00:00.000Z')).toBeValidDate()).toThrowErrorMatchingSnapshot();
});

test('fails when not given non-date values', () => {
expect(() => expect(1).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();
});

test('fails when given a valid date value', () => {
expect(() => expect(new Date('2018-01-01T13:00:00.000Z')).not.toBeValidDate()).toThrowErrorMatchingSnapshot();
});
});
7 changes: 7 additions & 0 deletions src/matchers/toBeValidDate/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { is } from '../../utils';

const hasDateType = is('Date');

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

export default isValidDate;
24 changes: 24 additions & 0 deletions src/matchers/toBeValidDate/predicate.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export const determinePropertyMessage = (actual, property, message = 'Not Access
return actual && actual[property] ? actual[property] : message;
};

export const is = type => value => Object.prototype.toString.call(value) === `[object ${type}]`;

export { equals };
8 changes: 6 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ declare namespace jest {
toBeFalse(): R;

/**
* Use .toBeDate when checking if a value is a Date.
* Use `.toBeDate` when checking if a value is a `Date`.
*/
toBeDate(): R;

/**
* Use `.toBeValidDate` when checking if a value is a `valid Date`.
*/
toBeValidDate(): R;

/**
* Use `.toBeFunction` when checking if a value is a `Function`.
*/
Expand Down Expand Up @@ -301,4 +306,3 @@ declare namespace jest {
toIncludeMultiple(substring: string[]): R;
}
}