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 toBeDateString matcher #252

Merged
merged 5 commits into from
Oct 6, 2021
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [String](#string)
- [.toBeString()](#tobestring)
- [.toBeHexadecimal(string)](#tobehexadecimal)
- [.toBeDateString(string)](#tobedatestringstring)
- [.toEqualCaseInsensitive(string)](#toequalcaseinsensitivestring)
- [.toStartWith(prefix)](#tostartwithprefix)
- [.toEndWith(suffix)](#toendwithsuffix)
Expand Down Expand Up @@ -880,6 +881,18 @@ test('passes when value is a valid hexadecimal', () => {
});
```

#### .toBeDateString(string)

Use `.toBeDateString` when checking if a value is a valid date string.

```js
test('passes when value is a valid toBeDateString', () => {
expect('2019-11-27T14:05:07.520Z').toBeDateString();
expect('11/12/21').toBeDateString();
expect('not a date').not.toBeDateString();
});
```

#### .toEqualCaseInsensitive(string)

Use `.toEqualCaseInsensitive` when checking if a string is equal (===) to another ignoring the casing of both strings.
Expand Down
15 changes: 15 additions & 0 deletions src/matchers/toBeDateString/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

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

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

Expected value to be a date string received:
<red>\\"not a date\\"</>"
`;
26 changes: 26 additions & 0 deletions src/matchers/toBeDateString/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.toBeDateString', 'received', '') +
'\n\n' +
'Expected value to not be a date string received:\n' +
` ${printReceived(received)}`;

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

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

return { pass: false, message: failMessage(expected) };
},
};
23 changes: 23 additions & 0 deletions src/matchers/toBeDateString/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import matcher from './';

expect.extend(matcher);

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

test('fails when not given a date string', () => {
expect(() => expect('not a date').toBeDateString()).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeDateString', () => {
test('passes when not given a date string', () => {
expect('not a date').not.toBeDateString();
});

test('fails when given a date string', () => {
expect(() => expect('2018-01-01T13:00:00.000Z').not.toBeDateString()).toThrowErrorMatchingSnapshot();
});
});
3 changes: 3 additions & 0 deletions src/matchers/toBeDateString/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const isDateString = value => !isNaN(Date.parse(value));

export default isDateString;
11 changes: 11 additions & 0 deletions src/matchers/toBeDateString/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import predicate from './predicate';

describe('toBeDateString Predicate', () => {
test('returns true when given a date string', () => {
expect(predicate(new Date().toISOString())).toBe(true);
});

test('returns false when given a non date string', () => {
expect(predicate('not a date')).toBe(false);
});
});
14 changes: 14 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ declare namespace jest {
*/
toBeFunction(): R;

/**
* Use `.toBeDateString` when checking if a value is a valid date string.
*
* @param {String} string
*/
toBeDateString(string: string): R;

/**
* Use `.toBeHexadecimal` when checking if a value is a valid HTML hex color.
*
Expand Down Expand Up @@ -479,6 +486,13 @@ declare namespace jest {
*/
toBeFunction(): any;

/**
* Use `.toBeDateString` when checking if a value is a valid date string.
*
* @param {String} string
*/
toBeDateString(string: string): any;

/**
* Use `.toBeHexadecimal` when checking if a value is a valid HTML hex color.
*
Expand Down