Skip to content

Commit

Permalink
Add 'toBeBetween', 'toBeAfterOrEqualTo' and 'toBeBeforeOrEqualTo' mat…
Browse files Browse the repository at this point in the history
…chers. (#229)
  • Loading branch information
hansal7014 authored Oct 11, 2021
1 parent a2904bd commit b7e2332
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toBeValidDate()](#tobevaliddate)
- [.toBeAfter(date)](#tobeafterdate)
- [.toBeBefore(date)](#tobebeforedate)
- [.toBeAfterOrEqualTo(date)](#tobeafterorequaltodate)
- [.toBeBeforeOrEqualTo(date)](#tobebeforeorequaltodate)
- [.toBeBetween(startDate, endDate)](#tobebetweenstartdate-enddate)
- Further proposals in [#117](https://github.com/jest-community/jest-extended/issues/117) PRs welcome
- [Function](#function)
- [.toBeFunction()](#tobefunction)
Expand Down Expand Up @@ -462,6 +465,42 @@ test('passes when input is before date', () => {
});
```

### .toBeAfterOrEqualTo(date)

Use `.toBeAfterOrEqualTo` when checking if a date equals to or occurs after `date`.

```js
test('passes when input is equal to or after date', () => {
expect(new Date('01/01/2019')).toBeAfterOrEqualTo(new Date('01/01/2018'));
expect(new Date('01/01/2019')).toBeAfterOrEqualTo(new Date('01/01/2019'));
expect('01/01/2018').not.toBeAfterOrEqualTo(new Date('01/01/2019'));
});
```

### .toBeBeforeOrEqualTo(date)

Use `.toBeBeforeOrEqualTo` when checking if a date equals to or occurs before `date`.

```js
test('passes when input is equal to or before date', () => {
expect(new Date('01/01/2018')).toBeBeforeOrEqualTo(new Date('01/01/2019'));
expect(new Date('01/01/2018')).toBeBeforeOrEqualTo(new Date('01/01/2018'));
expect('01/01/2019').not.toBeBeforeOrEqualTo(new Date('01/01/2018'));
});
```

### .toBeBetween(startDate, endDate)

Use `.toBeBetween` when checking if a date equals or occurs after `startDate` and equals or occurs before `endDate`.

```js
test('passes when input is in given date range', () => {
expect(new Date('05/01/2019')).toBeBetween(new Date('01/01/2019'), new Date('10/01/2019');
expect(new Date('05/01/2019')).toBeBetween(new Date('05/01/2019'), new Date('10/01/2019');
expect(new Date('01/01/2019')).not.toBeBetween(new Date('05/01/2019'), new Date('10/01/2019'));
});
```
### Function
#### .toBeFunction()
Expand Down
3 changes: 3 additions & 0 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export { fail } from './fail';
export { pass } from './pass';
export { toBeAfter } from './toBeAfter';
export { toBeAfterOrEqualTo } from './toBeAfterOrEqualTo';
export { toBeArray } from './toBeArray';
export { toBeArrayOfSize } from './toBeArrayOfSize';
export { toBeBefore } from './toBeBefore';
export { toBeBeforeOrEqualTo } from './toBeBeforeOrEqualTo';
export { toBeBetween } from './toBeBetween';
export { toBeBoolean } from './toBeBoolean';
export { toBeDate } from './toBeDate';
export { toBeDateString } from './toBeDateString';
Expand Down
22 changes: 22 additions & 0 deletions src/matchers/toBeAfterOrEqualTo/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeAfterOrEqualTo fails when given a later date 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeAfterOrEqualTo()</>
Expected date to be after or equal to <red>2019-09-01T22:00:00.000Z</> but received:
<red>2019-09-10T22:00:00.000Z</>"
`;
exports[`.not.toBeAfterOrEqualTo fails when given an equal date 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeAfterOrEqualTo()</>
Expected date to be after or equal to <red>2019-09-01T22:00:00.000Z</> but received:
<red>2019-09-01T22:00:00.000Z</>"
`;
exports[`.toBeAfterOrEqualTo fails when given an earlier date 1`] = `
"<dim>expect(</><red>received</><dim>).toBeAfterOrEqualTo()</>
Expected date to be after or equal to <red>2019-09-10T22:00:00.000Z</> but received:
<red>2019-09-01T22:00:00.000Z</>"
`;
24 changes: 24 additions & 0 deletions src/matchers/toBeAfterOrEqualTo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (received, before) => () =>
matcherHint('.not.toBeAfterOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be after or equal to ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;

const failMessage = (received, before) => () =>
matcherHint('.toBeAfterOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be after or equal to ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;

export function toBeAfterOrEqualTo(date, after) {
const pass = predicate(date, after);
if (pass) {
return { pass: true, message: passMessage(date, after) };
}

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

expect.extend(matcher);

const EARLIER = new Date('2019-09-01T22:00:00.000Z');
const LATER = new Date('2019-09-10T22:00:00.000Z');

describe('.toBeAfterOrEqualTo', () => {
test('passes when given a later date', () => {
expect(LATER).toBeAfterOrEqualTo(EARLIER);
});

test('passes when given an equal date', () => {
expect(EARLIER).toBeAfterOrEqualTo(EARLIER);
});

test('fails when given an earlier date', () => {
expect(() => {
expect(EARLIER).toBeAfterOrEqualTo(LATER);
}).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeAfterOrEqualTo', () => {
test('passes when given an earlier date', () => {
expect(EARLIER).not.toBeAfterOrEqualTo(LATER);
});

test('fails when given a later date', () => {
expect(() => {
expect(LATER).not.toBeAfterOrEqualTo(EARLIER);
}).toThrowErrorMatchingSnapshot();
});

test('fails when given an equal date', () => {
expect(() => {
expect(EARLIER).not.toBeAfterOrEqualTo(EARLIER);
}).toThrowErrorMatchingSnapshot();
});
});
5 changes: 5 additions & 0 deletions src/matchers/toBeAfterOrEqualTo/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function toBeAfterOrEqualTo(date, after) {
return date >= after;
}

export default (date, after) => toBeAfterOrEqualTo(date, after);
18 changes: 18 additions & 0 deletions src/matchers/toBeAfterOrEqualTo/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import predicate from './predicate';

const EARLIER = new Date('01/09/2019');
const LATER = new Date('07/09/2019');

describe('toBeAfterOrEqualTo Predicate', () => {
test('returns true when given an earlier date', () => {
expect(predicate(LATER, EARLIER)).toBe(true);
});

test('returns true when given an equal date', () => {
expect(predicate(EARLIER, EARLIER)).toBe(true);
});

test('returns false when given a later date', () => {
expect(predicate(EARLIER, LATER)).toBe(false);
});
});
22 changes: 22 additions & 0 deletions src/matchers/toBeBeforeOrEqualTo/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeBeforeOrEqualTo fails when given an earlier date 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeBeforeOrEqualTo()</>
Expected date to be before or equal to <red>2019-09-10T22:00:00.000Z</> but received:
<red>2019-09-01T22:00:00.000Z</>"
`;
exports[`.not.toBeBeforeOrEqualTo fails when given an equal date 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeBeforeOrEqualTo()</>
Expected date to be before or equal to <red>2019-09-01T22:00:00.000Z</> but received:
<red>2019-09-01T22:00:00.000Z</>"
`;
exports[`.toBeBeforeOrEqualTo fails when given a later date 1`] = `
"<dim>expect(</><red>received</><dim>).toBeBeforeOrEqualTo()</>
Expected date to be before or equal to <red>2019-09-01T22:00:00.000Z</> but received:
<red>2019-09-10T22:00:00.000Z</>"
`;
24 changes: 24 additions & 0 deletions src/matchers/toBeBeforeOrEqualTo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (received, before) => () =>
matcherHint('.not.toBeBeforeOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be before or equal to ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;

const failMessage = (received, before) => () =>
matcherHint('.toBeBeforeOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be before or equal to ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;

export function toBeBeforeOrEqualTo(date, before) {
const pass = predicate(date, before);
if (pass) {
return { pass: true, message: passMessage(date, before) };
}

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

expect.extend(matcher);

const EARLIER = new Date('2019-09-01T22:00:00.000Z');
const LATER = new Date('2019-09-10T22:00:00.000Z');

describe('.toBeBeforeOrEqualTo', () => {
test('passes when given an earlier date', () => {
expect(EARLIER).toBeBeforeOrEqualTo(LATER);
});

test('passes when given an equal date', () => {
expect(EARLIER).toBeBeforeOrEqualTo(EARLIER);
});

test('fails when given a later date', () => {
expect(() => {
expect(LATER).toBeBeforeOrEqualTo(EARLIER);
}).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeBeforeOrEqualTo', () => {
test('passes when given an earlier date', () => {
expect(LATER).not.toBeBeforeOrEqualTo(EARLIER);
});

test('fails when given an earlier date', () => {
expect(() => {
expect(EARLIER).not.toBeBeforeOrEqualTo(LATER);
}).toThrowErrorMatchingSnapshot();
});

test('fails when given an equal date', () => {
expect(() => {
expect(EARLIER).not.toBeBeforeOrEqualTo(EARLIER);
}).toThrowErrorMatchingSnapshot();
});
});
5 changes: 5 additions & 0 deletions src/matchers/toBeBeforeOrEqualTo/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function toBeBeforeOrEqualTo(date, before) {
return date <= before;
}

export default (date, before) => toBeBeforeOrEqualTo(date, before);
18 changes: 18 additions & 0 deletions src/matchers/toBeBeforeOrEqualTo/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import predicate from './predicate';

const EARLIER = new Date('01/09/2019');
const LATER = new Date('07/09/2019');

describe('toBeBeforeOrEqualTo Predicate', () => {
test('returns true when given a later date', () => {
expect(predicate(EARLIER, LATER)).toBe(true);
});

test('returns true when given an equal date', () => {
expect(predicate(EARLIER, EARLIER)).toBe(true);
});

test('returns false when given an earlier date', () => {
expect(predicate(LATER, EARLIER)).toBe(false);
});
});
15 changes: 15 additions & 0 deletions src/matchers/toBeBetween/__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.toBeBefore fails when date is in given range 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeBetween()</>
Expected date to be between <red>2019-09-01T22:00:00.000Z</> and <red>2019-09-10T22:00:00.000Z</> but received:
<red>2019-09-03T22:00:00.000Z</>"
`;
exports[`.toBeBetween fails when date is not in given range 1`] = `
"<dim>expect(</><red>received</><dim>).toBeBetween()</>
Expected date to be between <red>2019-09-03T22:00:00.000Z</> and <red>2019-09-10T22:00:00.000Z</> but received:
<red>2019-09-01T22:00:00.000Z</>"
`;
24 changes: 24 additions & 0 deletions src/matchers/toBeBetween/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (received, startDate, endDate) => () =>
matcherHint('.not.toBeBetween', 'received', '') +
'\n\n' +
`Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` +
` ${printReceived(received)}`;

const failMessage = (received, startDate, endDate) => () =>
matcherHint('.toBeBetween', 'received', '') +
'\n\n' +
`Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` +
` ${printReceived(received)}`;

export function toBeBetween(date, startDate, endDate) {
const pass = predicate(date, startDate, endDate);
if (pass) {
return { pass: true, message: passMessage(date, startDate, endDate) };
}

return { pass: false, message: failMessage(date, startDate, endDate) };
}
31 changes: 31 additions & 0 deletions src/matchers/toBeBetween/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as matcher from './';

expect.extend(matcher);

const TESTDATE1 = new Date('2019-09-01T22:00:00.000Z');
const TESTDATE2 = new Date('2019-09-10T22:00:00.000Z');
const TESTDATE3 = new Date('2019-09-03T22:00:00.000Z');

describe('.toBeBetween', () => {
test('passes when date is in given range', () => {
expect(TESTDATE3).toBeBetween(TESTDATE1, TESTDATE2);
});

test('fails when date is not in given range', () => {
expect(() => {
expect(TESTDATE1).toBeBetween(TESTDATE3, TESTDATE2);
}).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeBefore', () => {
test('passes when date is not in given range', () => {
expect(TESTDATE1).not.toBeBetween(TESTDATE3, TESTDATE2);
});

test('fails when date is in given range', () => {
expect(() => {
expect(TESTDATE3).not.toBeBetween(TESTDATE1, TESTDATE2);
}).toThrowErrorMatchingSnapshot();
});
});
5 changes: 5 additions & 0 deletions src/matchers/toBeBetween/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function toBeBetween(date, startDate, endDate) {
return date >= startDate && date <= endDate;
}

export default (date, startDate, endDate) => toBeBetween(date, startDate, endDate);
15 changes: 15 additions & 0 deletions src/matchers/toBeBetween/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import predicate from './predicate';

const TESTDATE1 = new Date('01/09/2019');
const TESTDATE2 = new Date('10/09/2019');
const TESTDATE3 = new Date('03/09/2019');

describe('toBeBetween Predicate', () => {
test('returns true when date is in given range', () => {
expect(predicate(TESTDATE3, TESTDATE1, TESTDATE2)).toBe(true);
});

test('returns false when date is not in given range', () => {
expect(predicate(TESTDATE1, TESTDATE3, TESTDATE2)).toBe(false);
});
});
Loading

0 comments on commit b7e2332

Please sign in to comment.