Skip to content

Commit dd4c8a3

Browse files
smacpherson64Kent C. Dodds
authored andcommitted
feat: Add toBeEmpty (#26)
* #8: Added test for toBeEmpty matcher * #8: Added toBeEmpty matcher * #8: Added documentation for toBeEmpty matcher * #8: Updated types for toBeEmpty matcher * Added user as contributor
1 parent 99504d7 commit dd4c8a3

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

.all-contributorsrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@
147147
"bug",
148148
"code"
149149
]
150+
},
151+
{
152+
"login": "smacpherson64",
153+
"name": "smacpherson64",
154+
"avatar_url": "https://avatars1.githubusercontent.com/u/1659099?v=4",
155+
"profile": "https://github.com/smacpherson64",
156+
"contributions": [
157+
"code",
158+
"doc",
159+
"test"
160+
]
150161
}
151162
]
152163
}

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -112,6 +112,21 @@ expect(queryByTestId(container, 'count-value1')).not.toBeInTheDOM()
112112
> (like `queryByTestId`) rather than a get function (like `getByTestId`).
113113
> Otherwise the `get*` function could throw an error before your assertion.
114114
115+
### `toBeEmpty`
116+
117+
This allows you to assert whether an element has content or not.
118+
119+
```javascript
120+
// add the custom expect matchers once
121+
import 'jest-dom/extend-expect'
122+
123+
// ...
124+
// <span data-testid="not-empty"><span data-testid="empty"></span></span>
125+
expect(queryByTestId(container, 'empty')).toBeEmpty()
126+
expect(queryByTestId(container, 'not-empty')).not.toBeEmpty()
127+
// ...
128+
```
129+
115130
### `toHaveTextContent`
116131

117132
This API allows you to check whether the given element has a text content or not.
@@ -265,7 +280,7 @@ Thanks goes to these people ([emoji key][emojis]):
265280
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](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") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/gnapse/jest-dom/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/gnapse/jest-dom/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](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") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
266281
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
267282
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](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") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/gnapse/jest-dom/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](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") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/gnapse/jest-dom/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/gnapse/jest-dom/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💻](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") | [<img src="https://avatars0.githubusercontent.com/u/79312?v=4" width="100px;"/><br /><sub><b>Mark Volkmann</b></sub>](http://ociweb.com/mark/)<br />[🐛](https://github.com/gnapse/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [💻](https://github.com/gnapse/jest-dom/commits?author=mvolkmann "Code") |
268-
283+
| [<img src="https://avatars1.githubusercontent.com/u/1659099?v=4" width="100px;"/><br /><sub><b>smacpherson64</b></sub>](https://github.com/smacpherson64)<br />[💻](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") |
269284
<!-- ALL-CONTRIBUTORS-LIST:END -->
270285

271286
This project follows the [all-contributors][all-contributors] specification.

extend-expect.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare namespace jest {
55
toHaveClass: (className: string) => R
66
toBeInTheDOM: () => R
77
toBeVisible: () => R
8+
toBeEmpty: () => R
89
toHaveStyle: (css: string) => R
910
}
1011
}

src/__tests__/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ test('.toBeInTheDOM', () => {
2222
).toThrowError()
2323
})
2424

25+
test('.toBeEmpty', () => {
26+
const {queryByTestId} = render(`
27+
<span data-testid="not-empty">
28+
<span data-testid="empty"></span>
29+
</span>`)
30+
31+
const empty = queryByTestId('empty')
32+
const notEmpty = queryByTestId('not-empty')
33+
const nonExistantElement = queryByTestId('not-exists')
34+
const fakeElement = {thisIsNot: 'an html element'}
35+
36+
expect(empty).toBeEmpty()
37+
expect(notEmpty).not.toBeEmpty()
38+
39+
// negative test cases wrapped in throwError assertions for coverage.
40+
expect(() => expect(empty).not.toBeEmpty()).toThrowError()
41+
42+
expect(() => expect(notEmpty).toBeEmpty()).toThrowError()
43+
44+
expect(() => expect(fakeElement).toBeEmpty()).toThrowError()
45+
46+
expect(() => {
47+
expect(nonExistantElement).toBeEmpty()
48+
}).toThrowError()
49+
})
50+
2551
test('.toHaveTextContent', () => {
2652
const {queryByTestId} = render(`<span data-testid="count-value">2</span>`)
2753

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {toBeInTheDOM} from './to-be-in-the-dom'
2+
import {toBeEmpty} from './to-be-empty'
23
import {toHaveTextContent} from './to-have-text-content'
34
import {toHaveAttribute} from './to-have-attribute'
45
import {toHaveClass} from './to-have-class'
@@ -7,6 +8,7 @@ import {toBeVisible} from './to-be-visible'
78

89
export {
910
toBeInTheDOM,
11+
toBeEmpty,
1012
toHaveTextContent,
1113
toHaveAttribute,
1214
toHaveClass,

src/to-be-empty.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {matcherHint, printReceived} from 'jest-matcher-utils'
2+
import {checkHtmlElement} from './utils'
3+
4+
export function toBeEmpty(element) {
5+
checkHtmlElement(element, toBeEmpty, this)
6+
7+
return {
8+
pass: element.innerHTML === '',
9+
message: () => {
10+
return [
11+
matcherHint(`${this.isNot ? '.not' : ''}.toBeEmpty`, 'element', ''),
12+
'',
13+
'Received:',
14+
` ${printReceived(element.innerHTML)}`,
15+
].join('\n')
16+
},
17+
}
18+
}

0 commit comments

Comments
 (0)