-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'toBeBetween', 'toBeAfterOrEqualTo' and 'toBeBeforeOrEqualTo' mat…
…chers. (#229)
- Loading branch information
1 parent
a2904bd
commit b7e2332
Showing
18 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/matchers/toBeAfterOrEqualTo/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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</>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/matchers/toBeBeforeOrEqualTo/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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</>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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</>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.