diff --git a/.all-contributorsrc b/.all-contributorsrc index 4453790b..95deef08 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -326,6 +326,19 @@ "avatar_url": "https://avatars0.githubusercontent.com/u/15064535?v=4", "profile": "https://github.com/LucianBuzzo", "contributions": [] + }, + { + "login": "thiagodp", + "name": "Thiago Delgado Pinto", + "avatar_url": "https://avatars3.githubusercontent.com/u/2997844?v=4", + "profile": "https://github.com/thiagodp", + "contributions": [ + "code", + "doc", + "example", + "ideas", + "test" + ] } ] } diff --git a/README.md b/README.md index 0c3e1d4b..0ff83c7f 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,8 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin * [.toBeTrue()](#tobetrue) * [.toBeFalse()](#tobefalse) * [~~Date~~](#date) - * _No APIs proposed yet_ + * Proposal in #117 (under development) + * [Function](#function) * [.toBeFunction()](#tobefunction) * [Mock](#mock) @@ -298,7 +299,20 @@ test('returns false', () => { ### ~~Date~~ -_No APIs proposed yet_ +Proposal in #117 (*under development*) + +### .toBeDate() + +Use `.toBeDate` when checking if a value is a `Date`. + +```js +test('passes when value is a date', () => { + expect(new Date()).toBeDate(); + expect('01/01/2018').not.toBeDate(); + expect(new Date('01/01/2018').toBeDate(); + expect(undefined).not.toBeDate(); +}); +``` ### Function @@ -748,7 +762,7 @@ test('passes when value includes all substrings', () => { | [
Andrew Hayward](http://andrewhayward.net)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=andrewhayward "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=andrewhayward "Tests") | [
Oliver Schneider](https://ols.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=olsio "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=olsio "Tests") | [
Tyle Whalen](https://github.com/tjwhalen16)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tjwhalen16 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tjwhalen16 "Documentation") | [
Martius](https://github.com/martiuslim)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=martiuslim "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=martiuslim "Tests") | [
Eli Collis](https://github.com/ecollis6)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=ecollis6 "Code") | [
Marcin LichwaΕ‚a](https://github.com/marcinlichwala)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=marcinlichwala "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=marcinlichwala "Tests") | [
Massimo Prencipe](https://github.com/mprencipe)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mprencipe "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mprencipe "Tests") | | [
mjmiles](https://github.com/mjmiles)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Documentation") | [
Gary Meehan](https://github.com/garmeeh)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Tests") | [
Fredrik MΓ€kilΓ€](https://github.com/GitHug)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Tests") | [
Daniel Reinoso](http://kloc.io/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Tests") | [
Chris Hut](https://github.com/tophernuts)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Tests") | [
Kelvin Ly](https://github.com/cactorium)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Tests") | [
Francis Ngo](https://github.com/francisngo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Tests") | | [
Amish Shah](https://hydrabolt.me/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [
Dave Cooper](http://davecooper.org)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [
Swann Polydor](https://github.com/soueuls)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [
vikneshwar](https://github.com/vikneshwar)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [
Budi Irawan](http://budiirawan.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [
Tejas Bubane](http://foss-geek.blogspot.com/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Code") | [
Subinoy Ghosh](https://github.com/subinoy7)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") | -| [
Simen Bekkhus](https://github.com/SimenB)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [
Orta](http://orta.io)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [
Tom](https://jsdevtom.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [πŸ’‘](#example-jsdevtom "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [
Lucian Buzzo](https://github.com/LucianBuzzo)
| +| [
Simen Bekkhus](https://github.com/SimenB)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [
Orta](http://orta.io)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [
Tom](https://jsdevtom.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [πŸ’‘](#example-jsdevtom "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [
Lucian Buzzo](https://github.com/LucianBuzzo)
| [
Thiago Delgado Pinto](https://github.com/thiagodp)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Documentation") [πŸ’‘](#example-thiagodp "Examples") [πŸ€”](#ideas-thiagodp "Ideas, Planning, & Feedback") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Tests") | ## LICENSE diff --git a/src/matchers/toBeDate/__snapshots__/index.test.js.snap b/src/matchers/toBeDate/__snapshots__/index.test.js.snap new file mode 100644 index 00000000..b0f6d568 --- /dev/null +++ b/src/matchers/toBeDate/__snapshots__/index.test.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`.not.toBeDate fails when given a date 1`] = ` +"expect(received).not.toBeDate() + +Expected value to not be a date received: + 2018-01-01T13:00:00.000Z" +`; + +exports[`.toBeDate fails when not given a date 1`] = ` +"expect(received).toBeDate() + +Expected value to be a date received: + false" +`; diff --git a/src/matchers/toBeDate/index.js b/src/matchers/toBeDate/index.js new file mode 100644 index 00000000..5e3af505 --- /dev/null +++ b/src/matchers/toBeDate/index.js @@ -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) }; + } +}; diff --git a/src/matchers/toBeDate/index.test.js b/src/matchers/toBeDate/index.test.js new file mode 100644 index 00000000..19370a21 --- /dev/null +++ b/src/matchers/toBeDate/index.test.js @@ -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(); + }); +}); diff --git a/src/matchers/toBeDate/predicate.js b/src/matchers/toBeDate/predicate.js new file mode 100644 index 00000000..fe0d79ee --- /dev/null +++ b/src/matchers/toBeDate/predicate.js @@ -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); diff --git a/src/matchers/toBeDate/predicate.test.js b/src/matchers/toBeDate/predicate.test.js new file mode 100644 index 00000000..1fcab069 --- /dev/null +++ b/src/matchers/toBeDate/predicate.test.js @@ -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); + } + ); +});