-
Notifications
You must be signed in to change notification settings - Fork 404
/
to-have-text-content.js
36 lines (32 loc) · 1.07 KB
/
to-have-text-content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import {matcherHint} from 'jest-matcher-utils'
import {checkHtmlElement, getMessage, matches, normalize} from './utils'
export function toHaveTextContent(
htmlElement,
checkWith,
options = {normalizeWhitespace: true},
) {
checkHtmlElement(htmlElement, toHaveTextContent, this)
const textContent = options.normalizeWhitespace
? normalize(htmlElement.textContent)
: htmlElement.textContent.replace(/\u00a0/g, ' ') // Replace with normal spaces
const checkingWithEmptyString = textContent !== '' && checkWith === ''
return {
pass: !checkingWithEmptyString && matches(textContent, checkWith),
message: () => {
const to = this.isNot ? 'not to' : 'to'
return getMessage(
matcherHint(
`${this.isNot ? '.not' : ''}.toHaveTextContent`,
'element',
'',
),
checkingWithEmptyString
? `Checking with empty string will always match, use .toBeEmpty() instead`
: `Expected element ${to} have text content`,
checkWith,
'Received',
textContent,
)
},
}
}