Skip to content

Commit

Permalink
Merge 48a2b5d into e53ee69
Browse files Browse the repository at this point in the history
  • Loading branch information
idan-at authored Nov 27, 2019
2 parents e53ee69 + 48a2b5d commit a35a4a7
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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 @@ -858,6 +859,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(</><dim>)</>
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(</><dim>)</>
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 @@ -474,6 +481,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

0 comments on commit a35a4a7

Please sign in to comment.