Skip to content

Commit 927c5a4

Browse files
DanielaValeroDaniela Valero
andauthored
feat: Deprecate toBeEmpty in favour of toBeEmptyDomElement (#216) (#254)
In order to prevent name clashes with jest-extended, the toBeEmpty will have from now on a more specific name. Co-authored-by: Daniela Valero <[email protected]>
1 parent 85cf707 commit 927c5a4

File tree

6 files changed

+91
-1
lines changed

6 files changed

+91
-1
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ clear to read and to maintain.
5252
- [`toBeDisabled`](#tobedisabled)
5353
- [`toBeEnabled`](#tobeenabled)
5454
- [`toBeEmpty`](#tobeempty)
55+
- [`toBeEmptyDOMElement`](#tobeemptydomelement)
5556
- [`toBeInTheDocument`](#tobeinthedocument)
5657
- [`toBeInvalid`](#tobeinvalid)
5758
- [`toBeRequired`](#toberequired)
@@ -207,6 +208,31 @@ expect(getByTestId('empty')).toBeEmpty()
207208
expect(getByTestId('not-empty')).not.toBeEmpty()
208209
```
209210

211+
> Note: This matcher is being deprecated due to a name clash with
212+
> `jest-extended`. See more info in #216. In the future, please use only:
213+
> [`toBeEmptyDOMElement`](#toBeEmptyDOMElement)
214+
215+
<hr />
216+
217+
### `toBeEmptyDOMElement`
218+
219+
```typescript
220+
toBeEmptyDOMElement()
221+
```
222+
223+
This allows you to assert whether an element has content or not.
224+
225+
#### Examples
226+
227+
```html
228+
<span data-testid="not-empty"><span data-testid="empty"></span></span>
229+
```
230+
231+
```javascript
232+
expect(getByTestId('empty')).toBeEmptyDOMElement()
233+
expect(getByTestId('not-empty')).not.toBeEmptyDOMElement()
234+
```
235+
210236
<hr />
211237

212238
### `toBeInTheDocument`
@@ -1135,6 +1161,7 @@ Thanks goes to these people ([emoji key][emojis]):
11351161

11361162
<!-- markdownlint-enable -->
11371163
<!-- prettier-ignore-end -->
1164+
11381165
<!-- ALL-CONTRIBUTORS-LIST:END -->
11391166

11401167
This project follows the [all-contributors][all-contributors] specification.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {render} from './helpers/test-utils'
2+
3+
test('.toBeEmptyDOMElement', () => {
4+
const {queryByTestId} = render(`
5+
<span data-testid="not-empty">
6+
<span data-testid="empty"></span>
7+
<svg data-testid="svg-empty"></svg>
8+
</span>`)
9+
10+
const empty = queryByTestId('empty')
11+
const notEmpty = queryByTestId('not-empty')
12+
const svgEmpty = queryByTestId('svg-empty')
13+
const nonExistantElement = queryByTestId('not-exists')
14+
const fakeElement = {thisIsNot: 'an html element'}
15+
16+
expect(empty).toBeEmptyDOMElement()
17+
expect(svgEmpty).toBeEmptyDOMElement()
18+
expect(notEmpty).not.toBeEmptyDOMElement()
19+
20+
// negative test cases wrapped in throwError assertions for coverage.
21+
expect(() => expect(empty).not.toBeEmptyDOMElement()).toThrowError()
22+
23+
expect(() => expect(svgEmpty).not.toBeEmptyDOMElement()).toThrowError()
24+
25+
expect(() => expect(notEmpty).toBeEmptyDOMElement()).toThrowError()
26+
27+
expect(() => expect(fakeElement).toBeEmptyDOMElement()).toThrowError()
28+
29+
expect(() => {
30+
expect(nonExistantElement).toBeEmptyDOMElement()
31+
}).toThrowError()
32+
})

src/__tests__/to-be-empty.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {render} from './helpers/test-utils'
22

33
test('.toBeEmpty', () => {
4+
// @deprecated intentionally hiding warnings for test clarity
5+
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
46
const {queryByTestId} = render(`
57
<span data-testid="not-empty">
68
<span data-testid="empty"></span>
@@ -29,4 +31,5 @@ test('.toBeEmpty', () => {
2931
expect(() => {
3032
expect(nonExistantElement).toBeEmpty()
3133
}).toThrowError()
34+
spy.mockRestore()
3235
})

src/matchers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {toBeInTheDOM} from './to-be-in-the-dom'
22
import {toBeInTheDocument} from './to-be-in-the-document'
33
import {toBeEmpty} from './to-be-empty'
4+
import {toBeEmptyDOMElement} from './to-be-empty-dom-element'
45
import {toContainElement} from './to-contain-element'
56
import {toContainHTML} from './to-contain-html'
67
import {toHaveTextContent} from './to-have-text-content'
@@ -23,6 +24,7 @@ export {
2324
toBeInTheDOM,
2425
toBeInTheDocument,
2526
toBeEmpty,
27+
toBeEmptyDOMElement,
2628
toContainElement,
2729
toContainHTML,
2830
toHaveTextContent,

src/to-be-empty-dom-element.js

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

src/to-be-empty.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import {matcherHint, printReceived} from 'jest-matcher-utils'
2-
import {checkHtmlElement} from './utils'
2+
import {checkHtmlElement, deprecate} from './utils'
33

44
export function toBeEmpty(element) {
5+
deprecate(
6+
'toBeEmpty',
7+
'Please use instead toBeEmptyDOMElement for finding empty nodes in the DOM.',
8+
)
59
checkHtmlElement(element, toBeEmpty, this)
610

711
return {

0 commit comments

Comments
 (0)