Skip to content

Commit 32cdb02

Browse files
committed
fix: check for any Node (fixes #306)
1 parent c11f9c5 commit 32cdb02

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/__tests__/to-have-text-content.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ describe('.toHaveTextContent', () => {
1010
expect(queryByTestId('count-value')).not.toHaveTextContent('21')
1111
})
1212

13+
test('handles non-element nodes', () => {
14+
const {container} = render(`<span>example</span>`)
15+
16+
expect(container.querySelector('span').firstChild).toHaveTextContent(
17+
'example',
18+
)
19+
})
20+
1321
test('handles negative test cases', () => {
1422
const {queryByTestId} = render(`<span data-testid="count-value">2</span>`)
1523

src/to-have-text-content.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import {checkHtmlElement, getMessage, matches, normalize} from './utils'
1+
import {getMessage, HtmlElementTypeError, matches, normalize} from './utils'
22

33
export function toHaveTextContent(
44
htmlElement,
55
checkWith,
66
options = {normalizeWhitespace: true},
77
) {
8-
checkHtmlElement(htmlElement, toHaveTextContent, this)
8+
const window = htmlElement.ownerDocument.defaultView
9+
10+
if (!(htmlElement instanceof window.Node)) {
11+
throw new HtmlElementTypeError(toHaveTextContent, this)
12+
}
913

1014
const textContent = options.normalizeWhitespace
1115
? normalize(htmlElement.textContent)

0 commit comments

Comments
 (0)