forked from jest-community/jest-extended
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
148 additions
and
17 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
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
17 changes: 17 additions & 0 deletions
17
src/matchers/toBeValidDate/__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,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</>" | ||
`; |
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,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) }; | ||
} | ||
}; |
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,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(); | ||
}); | ||
}); |
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,7 @@ | ||
let is = type => value => Object.prototype.toString.call(value) === `[object ${type}]`; | ||
|
||
let hasDateType = is('Date'); | ||
|
||
let isValidDate = value => hasDateType(value) && !isNaN(value) && !isNaN(value.getTime()); | ||
|
||
export default expected => isValidDate(expected); |
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 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); | ||
}); | ||
}); |
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