From 462eec7dac9ce39caefb72fde21493f1e85e228a Mon Sep 17 00:00:00 2001 From: Seth MacPherson Date: Wed, 4 Jul 2018 10:20:12 -0500 Subject: [PATCH 1/5] #8: Added test for toBeEmpty matcher --- src/__tests__/index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 9ce1af4e..662c9247 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -22,6 +22,32 @@ test('.toBeInTheDOM', () => { ).toThrowError() }) +test('.toBeEmpty', () => { + const {queryByTestId} = render(` + + + `) + + const empty = queryByTestId('empty') + const notEmpty = queryByTestId('not-empty') + const nonExistantElement = queryByTestId('not-exists') + const fakeElement = {thisIsNot: 'an html element'} + + expect(empty).toBeEmpty() + expect(notEmpty).not.toBeEmpty() + + // negative test cases wrapped in throwError assertions for coverage. + expect(() => expect(empty).not.toBeEmpty()).toThrowError() + + expect(() => expect(notEmpty).toBeEmpty()).toThrowError() + + expect(() => expect(fakeElement).toBeEmpty()).toThrowError() + + expect(() => { + expect(nonExistantElement).toBeEmpty() + }).toThrowError() +}) + test('.toHaveTextContent', () => { const {queryByTestId} = render(`2`) From 35c8e1a4195eba905c8758d930ac3434d70f5a20 Mon Sep 17 00:00:00 2001 From: Seth MacPherson Date: Wed, 4 Jul 2018 10:20:47 -0500 Subject: [PATCH 2/5] #8: Added toBeEmpty matcher --- src/index.js | 2 ++ src/to-be-empty.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/to-be-empty.js diff --git a/src/index.js b/src/index.js index 6750ac4d..3d1f00f4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ import {toBeInTheDOM} from './to-be-in-the-dom' +import {toBeEmpty} from './to-be-empty' import {toHaveTextContent} from './to-have-text-content' import {toHaveAttribute} from './to-have-attribute' import {toHaveClass} from './to-have-class' @@ -7,6 +8,7 @@ import {toBeVisible} from './to-be-visible' export { toBeInTheDOM, + toBeEmpty, toHaveTextContent, toHaveAttribute, toHaveClass, diff --git a/src/to-be-empty.js b/src/to-be-empty.js new file mode 100644 index 00000000..6a033592 --- /dev/null +++ b/src/to-be-empty.js @@ -0,0 +1,18 @@ +import {matcherHint, printReceived} from 'jest-matcher-utils' +import {checkHtmlElement} from './utils' + +export function toBeEmpty(element) { + checkHtmlElement(element, toBeEmpty, this) + + return { + pass: element.innerHTML === '', + message: () => { + return [ + matcherHint(`${this.isNot ? '.not' : ''}.toBeEmpty`, 'element', ''), + '', + 'Received:', + ` ${printReceived(element.innerHTML)}`, + ].join('\n') + }, + } +} From 584de0a561247a67aa7d354955d7cd56c7b8d2eb Mon Sep 17 00:00:00 2001 From: Seth MacPherson Date: Wed, 4 Jul 2018 10:21:01 -0500 Subject: [PATCH 3/5] #8: Added documentation for toBeEmpty matcher --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 8361e60c..57ea0c0f 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,21 @@ expect(queryByTestId(container, 'count-value1')).not.toBeInTheDOM() > (like `queryByTestId`) rather than a get function (like `getByTestId`). > Otherwise the `get*` function could throw an error before your assertion. +### `toBeEmpty` + +This allows you to assert whether an element has content or not. + +```javascript +// add the custom expect matchers once +import 'jest-dom/extend-expect' + +// ... +// +expect(queryByTestId(container, 'empty')).toBeEmpty() +expect(queryByTestId(container, 'not-empty')).not.toBeEmpty() +// ... +``` + ### `toHaveTextContent` This API allows you to check whether the given element has a text content or not. From efc0988f9d47c9080766fd9795bffabd43c27ce2 Mon Sep 17 00:00:00 2001 From: Seth MacPherson Date: Wed, 4 Jul 2018 10:21:17 -0500 Subject: [PATCH 4/5] #8: Updated types for toBeEmpty matcher --- extend-expect.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/extend-expect.d.ts b/extend-expect.d.ts index 016aa800..70df82b7 100644 --- a/extend-expect.d.ts +++ b/extend-expect.d.ts @@ -5,6 +5,7 @@ declare namespace jest { toHaveClass: (className: string) => R toBeInTheDOM: () => R toBeVisible: () => R + toBeEmpty: () => R toHaveStyle: (css: string) => R } } From 0fb7a0132bc4118a46040b411a8fc890481f1530 Mon Sep 17 00:00:00 2001 From: Seth MacPherson Date: Wed, 4 Jul 2018 10:24:12 -0500 Subject: [PATCH 5/5] Added user as contributor --- .all-contributorsrc | 11 +++++++++++ README.md | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index c99e0980..998ea778 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -147,6 +147,17 @@ "bug", "code" ] + }, + { + "login": "smacpherson64", + "name": "smacpherson64", + "avatar_url": "https://avatars1.githubusercontent.com/u/1659099?v=4", + "profile": "https://github.com/smacpherson64", + "contributions": [ + "code", + "doc", + "test" + ] } ] } diff --git a/README.md b/README.md index 57ea0c0f..adda1026 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ [![downloads][downloads-badge]][npmtrends] [![MIT License][license-badge]][license] -[![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors) [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc] @@ -280,7 +280,7 @@ Thanks goes to these people ([emoji key][emojis]): | [
Kent C. Dodds](https://kentcdodds.com)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Documentation") [πŸš‡](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/gnapse/jest-dom/commits?author=kentcdodds "Tests") | [
Ryan Castner](http://audiolion.github.io)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=audiolion "Documentation") | [
Daniel Sandiego](https://www.dnlsandiego.com)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=dnlsandiego "Code") | [
PaweΕ‚ MikoΕ‚ajczyk](https://github.com/Miklet)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=Miklet "Code") | [
Alejandro ÑÑñez Ortiz](http://co.linkedin.com/in/alejandronanez/)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=alejandronanez "Documentation") | [
Matt Parrish](https://github.com/pbomb)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Apbomb "Bug reports") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=pbomb "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=pbomb "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=pbomb "Tests") | [
Justin Hall](https://github.com/wKovacs64)
[πŸ“¦](#platform-wKovacs64 "Packaging/porting to new platform") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [
Anto Aravinth](https://github.com/antoaravinth)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Tests") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=antoaravinth "Documentation") | [
Jonah Moses](https://github.com/JonahMoses)
[πŸ“–](https://github.com/gnapse/jest-dom/commits?author=JonahMoses "Documentation") | [
Łukasz Gandecki](http://team.thebrain.pro)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Code") [⚠️](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Tests") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=lgandecki "Documentation") | [
Ivan Babak](https://sompylasar.github.io)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Asompylasar "Bug reports") [πŸ€”](#ideas-sompylasar "Ideas, Planning, & Feedback") | [
Jesse Day](https://github.com/jday3)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=jday3 "Code") | [
Ernesto GarcΓ­a](http://gnapse.github.io)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=gnapse "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=gnapse "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=gnapse "Tests") | [
Mark Volkmann](http://ociweb.com/mark/)
[πŸ›](https://github.com/gnapse/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [πŸ’»](https://github.com/gnapse/jest-dom/commits?author=mvolkmann "Code") | - +| [
smacpherson64](https://github.com/smacpherson64)
[πŸ’»](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Code") [πŸ“–](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Documentation") [⚠️](https://github.com/gnapse/jest-dom/commits?author=smacpherson64 "Tests") | This project follows the [all-contributors][all-contributors] specification.