-
-
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.
* Adds toBeDate() * Adds contributor * Fixes toBeDate() snapshot test
- Loading branch information
1 parent
0d8ac8b
commit 1882d36
Showing
7 changed files
with
132 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toBeDate fails when given a date 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toBeDate(</><dim>)</> | ||
Expected value to not be a date received: | ||
<red>2018-01-01T13:00:00.000Z</>" | ||
`; | ||
exports[`.toBeDate fails when not given a date 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toBeDate(</><dim>)</> | ||
Expected value to be a date received: | ||
<red>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,26 @@ | ||
import { matcherHint, printReceived } from 'jest-matcher-utils'; | ||
|
||
import predicate from './predicate'; | ||
|
||
const passMessage = received => () => | ||
matcherHint('.not.toBeDate', 'received', '') + | ||
'\n\n' + | ||
'Expected value to not be a date received:\n' + | ||
` ${printReceived(received)}`; | ||
|
||
const failMessage = received => () => | ||
matcherHint('.toBeDate', 'received', '') + | ||
'\n\n' + | ||
'Expected value to be a date received:\n' + | ||
` ${printReceived(received)}`; | ||
|
||
export default { | ||
toBeDate: 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,35 @@ | ||
import each from 'jest-each'; | ||
|
||
import matcher from './'; | ||
|
||
expect.extend(matcher); | ||
|
||
describe('.toBeDate', () => { | ||
test('passes when given a date', () => { | ||
expect(new Date()).toBeDate(); | ||
}); | ||
|
||
test('fails when not given a date', () => { | ||
expect(() => expect(false).toBeDate()).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toBeDate', () => { | ||
each([ | ||
[false], | ||
[true], | ||
[0], | ||
[''], | ||
[{}], | ||
[() => {}], | ||
[undefined], | ||
[null], | ||
[NaN] | ||
]).test('passes when not given a date: %s', given => { | ||
expect(given).not.toBeDate(); | ||
}); | ||
|
||
test('fails when given a date', () => { | ||
expect(() => expect(new Date('2018-01-01T13:00:00.000Z')).not.toBeDate()).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,11 @@ | ||
function is(type) { | ||
return value => Object.prototype.toString.call(value) === `[object ${type}]`; | ||
} | ||
|
||
let hasDateType = is('Date'); | ||
|
||
function isDate(value) { | ||
return hasDateType(value) && !isNaN(value); | ||
} | ||
|
||
export default expected => isDate(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,15 @@ | ||
import each from 'jest-each'; | ||
import predicate from './predicate'; | ||
|
||
describe('toBeDate Predicate', () => { | ||
test('returns true when given a date', () => { | ||
expect(predicate(new Date('12/25/2017'))).toBe(true); | ||
}); | ||
|
||
each([[true], [false], [''], [0], [{}], [() => {}], [undefined], [null], [NaN]]).test( | ||
'returns false when given: %s', | ||
given => { | ||
expect(predicate(given)).toBe(false); | ||
} | ||
); | ||
}); |