Skip to content

Commit 40db857

Browse files
smacpherson64gnapse
authored andcommitted
feat: #9: Normalized whitespace using dom-testing-library (#56)
BREAKING CHANGE: normalizing whitespace is now the default, though users can opt out of it.
1 parent 042db4d commit 40db857

File tree

6 files changed

+66
-23
lines changed

6 files changed

+66
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ The usual rules of css precedence apply.
535535
### `toHaveTextContent`
536536
537537
```typescript
538-
toHaveTextContent(text: string | RegExp)
538+
toHaveTextContent(text: string | RegExp, options?: {normalizeWhitespace: boolean})
539539
```
540540
541541
This API allows you to check whether the given element has a text content or not.

extend-expect.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ declare namespace jest {
1313
toHaveAttribute(attr: string, value?: string): R
1414
toHaveClass(...classNames: string[]): R
1515
toHaveStyle(css: string): R
16-
toHaveTextContent(text: string | RegExp): R
16+
toHaveTextContent(
17+
text: string | RegExp,
18+
options?: {normalizeWhitespace: boolean},
19+
): R
1720
toHaveFocus(): R
1821
}
1922
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"dependencies": {
3535
"chalk": "^2.4.1",
3636
"css": "^2.2.3",
37+
"dom-testing-library": "^3.5.0",
3738
"jest-diff": "^22.4.3",
3839
"jest-matcher-utils": "^22.4.3",
3940
"pretty-format": "^23.0.1",

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

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,48 @@
11
import {render} from './helpers/test-utils'
22

3-
test('.toHaveTextContent', () => {
4-
const {queryByTestId} = render(`<span data-testid="count-value">2</span>`)
5-
6-
expect(queryByTestId('count-value')).toHaveTextContent('2')
7-
expect(queryByTestId('count-value')).toHaveTextContent(2)
8-
expect(queryByTestId('count-value')).toHaveTextContent(/2/)
9-
expect(queryByTestId('count-value')).not.toHaveTextContent('21')
10-
expect(() =>
11-
expect(queryByTestId('count-value2')).toHaveTextContent('2'),
12-
).toThrowError()
13-
14-
expect(() =>
15-
expect(queryByTestId('count-value')).toHaveTextContent('3'),
16-
).toThrowError()
17-
expect(() =>
18-
expect(queryByTestId('count-value')).not.toHaveTextContent('2'),
19-
).toThrowError()
3+
describe('.toHaveTextContent', () => {
4+
test('handles positive test cases', () => {
5+
const {queryByTestId} = render(`<span data-testid="count-value">2</span>`)
6+
7+
expect(queryByTestId('count-value')).toHaveTextContent('2')
8+
expect(queryByTestId('count-value')).toHaveTextContent(2)
9+
expect(queryByTestId('count-value')).toHaveTextContent(/2/)
10+
expect(queryByTestId('count-value')).not.toHaveTextContent('21')
11+
})
12+
13+
test('handles negative test cases', () => {
14+
const {queryByTestId} = render(`<span data-testid="count-value">2</span>`)
15+
16+
expect(() =>
17+
expect(queryByTestId('count-value2')).toHaveTextContent('2'),
18+
).toThrowError()
19+
20+
expect(() =>
21+
expect(queryByTestId('count-value')).toHaveTextContent('3'),
22+
).toThrowError()
23+
expect(() =>
24+
expect(queryByTestId('count-value')).not.toHaveTextContent('2'),
25+
).toThrowError()
26+
})
27+
28+
test('normalizes whitespace by default', () => {
29+
const {container} = render(`
30+
<span>
31+
Step
32+
1
33+
of
34+
4
35+
</span>
36+
`)
37+
38+
expect(container.querySelector('span')).toHaveTextContent('Step 1 of 4')
39+
})
40+
41+
test('allows whitespace normalization to be turned off', () => {
42+
const {container} = render(`<span>&nbsp;&nbsp;Step 1 of 4</span>`)
43+
44+
expect(container.querySelector('span')).toHaveTextContent(' Step 1 of 4', {
45+
normalizeWhitespace: false,
46+
})
47+
})
2048
})

src/to-have-text-content.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import {matcherHint} from 'jest-matcher-utils'
2+
import {getNodeText} from 'dom-testing-library'
23
import {checkHtmlElement, getMessage, matches} from './utils'
34

4-
export function toHaveTextContent(htmlElement, checkWith) {
5+
export function toHaveTextContent(
6+
htmlElement,
7+
checkWith,
8+
options = {normalizeWhitespace: true},
9+
) {
510
checkHtmlElement(htmlElement, toHaveTextContent, this)
6-
const textContent = htmlElement.textContent
11+
12+
const textContent = options.normalizeWhitespace
13+
? getNodeText(htmlElement)
14+
.replace(/\s+/g, ' ')
15+
.trim()
16+
: getNodeText(htmlElement).replace(/\u00a0/g, ' ') // Replace &nbsp; with normal spaces
17+
718
return {
8-
pass: matches(textContent, htmlElement, checkWith),
19+
pass: matches(textContent, checkWith),
920
message: () => {
1021
const to = this.isNot ? 'not to' : 'to'
1122
return getMessage(

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function getMessage(
125125
].join('\n')
126126
}
127127

128-
function matches(textToMatch, node, matcher) {
128+
function matches(textToMatch, matcher) {
129129
if (matcher instanceof RegExp) {
130130
return matcher.test(textToMatch)
131131
} else {

0 commit comments

Comments
 (0)