Skip to content

Commit 6a6531d

Browse files
marcelbarnergnapse
andauthored
fix: Ignore comment nodes in toBeEmptyDOMElement (#317)
Co-authored-by: Ernesto García <gnapse@gmail.com>
1 parent f930668 commit 6a6531d

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,21 @@ expect(getByTestId('not-empty')).not.toBeEmpty()
232232
toBeEmptyDOMElement()
233233
```
234234

235-
This allows you to assert whether an element has content or not.
235+
This allows you to assert whether an element has no visible content for the user.
236+
It ignores comments but will fail if the element contains white-space.
236237

237238
#### Examples
238239

239240
```html
240241
<span data-testid="not-empty"><span data-testid="empty"></span></span>
242+
<span data-testid="with-whitespace"> </span>
243+
<span data-testid="with-comment"><!-- comment --></span>
241244
```
242245

243246
```javascript
244247
expect(getByTestId('empty')).toBeEmptyDOMElement()
245248
expect(getByTestId('not-empty')).not.toBeEmptyDOMElement()
249+
expect(getByTestId('with-whitespace')).not.toBeEmptyDOMElement()
246250
```
247251

248252
<hr />

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,35 @@ test('.toBeEmptyDOMElement', () => {
55
<span data-testid="not-empty">
66
<span data-testid="empty"></span>
77
<svg data-testid="svg-empty"></svg>
8-
</span>`)
8+
</span>
9+
<span data-testid="with-comment"><!-- This Comment --></span>
10+
<span data-testid="with-multiple-comments"><!-- Comment1 --><!-- Comment2 --></span>
11+
<span data-testid="with-element"><span></span></span>
12+
<span data-testid="with-element-and-comment"><!--Comment--><span></span></span>
13+
<span data-testid="with-whitespace"> </span>
14+
<span data-testid="with-text">Text</span>`)
915

1016
const empty = queryByTestId('empty')
1117
const notEmpty = queryByTestId('not-empty')
1218
const svgEmpty = queryByTestId('svg-empty')
19+
const withComment = queryByTestId('with-comment')
20+
const withMultipleComments = queryByTestId('with-multiple-comments')
21+
const withElement = queryByTestId('with-element')
22+
const withElementAndComment = queryByTestId('with-element-and-comment')
23+
const withWhitespace = queryByTestId('with-whitespace')
24+
const withText = queryByTestId('with-whitespace')
1325
const nonExistantElement = queryByTestId('not-exists')
1426
const fakeElement = {thisIsNot: 'an html element'}
1527

1628
expect(empty).toBeEmptyDOMElement()
1729
expect(svgEmpty).toBeEmptyDOMElement()
1830
expect(notEmpty).not.toBeEmptyDOMElement()
31+
expect(withComment).toBeEmptyDOMElement()
32+
expect(withMultipleComments).toBeEmptyDOMElement()
33+
expect(withElement).not.toBeEmptyDOMElement()
34+
expect(withElementAndComment).not.toBeEmptyDOMElement()
35+
expect(withWhitespace).not.toBeEmptyDOMElement()
36+
expect(withText).not.toBeEmptyDOMElement()
1937

2038
// negative test cases wrapped in throwError assertions for coverage.
2139
expect(() => expect(empty).not.toBeEmptyDOMElement()).toThrowError()
@@ -24,6 +42,18 @@ test('.toBeEmptyDOMElement', () => {
2442

2543
expect(() => expect(notEmpty).toBeEmptyDOMElement()).toThrowError()
2644

45+
expect(() => expect(withComment).not.toBeEmptyDOMElement()).toThrowError()
46+
47+
expect(() => expect(withMultipleComments).not.toBeEmptyDOMElement()).toThrowError()
48+
49+
expect(() => expect(withElement).toBeEmptyDOMElement()).toThrowError()
50+
51+
expect(() => expect(withElementAndComment).toBeEmptyDOMElement()).toThrowError()
52+
53+
expect(() => expect(withWhitespace).toBeEmptyDOMElement()).toThrowError()
54+
55+
expect(() => expect(withText).toBeEmptyDOMElement()).toThrowError()
56+
2757
expect(() => expect(fakeElement).toBeEmptyDOMElement()).toThrowError()
2858

2959
expect(() => {

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function toBeEmptyDOMElement(element) {
44
checkHtmlElement(element, toBeEmptyDOMElement, this)
55

66
return {
7-
pass: element.innerHTML === '',
7+
pass: isEmptyElement(element),
88
message: () => {
99
return [
1010
this.utils.matcherHint(
@@ -19,3 +19,16 @@ export function toBeEmptyDOMElement(element) {
1919
},
2020
}
2121
}
22+
23+
/**
24+
* Identifies if an element doesn't contain child nodes (excluding comments)
25+
* ℹ Node.COMMENT_NODE can't be used because of the following issue
26+
* https://github.com/jsdom/jsdom/issues/2220
27+
*
28+
* @param {*} element an HtmlElement or SVGElement
29+
* @return {*} true if the element only contains comments or none
30+
*/
31+
function isEmptyElement(element){
32+
const nonCommentChildNodes = [...element.childNodes].filter(node => node.nodeType !== 8);
33+
return nonCommentChildNodes.length === 0;
34+
}

0 commit comments

Comments
 (0)