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
9 changed files
with
120 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.toBeValidDate fails when not given an invalid date 1`] = `"Invalid time value"`; |
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,33 @@ | ||
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 not given an invalid date', () => { | ||
expect(() => expect(new Date('31/01/2018')).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(); | ||
}); | ||
}); |
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