-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
toHaveTextContent()
matcher (#1461)
* feat: toHaveTextContent() matcher chore: fix typescript, remove legacy jest-native tests * refactor: tweaks
- Loading branch information
1 parent
344e9b3
commit b428cc5
Showing
7 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/// <reference path="../extend-expect.d.ts" /> | ||
|
||
import * as React from 'react'; | ||
import { View, Text } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
test('toHaveTextContent() example test', () => { | ||
render( | ||
<View testID="view"> | ||
<Text>Hello</Text> <Text>World</Text> | ||
</View> | ||
); | ||
|
||
const view = screen.getByTestId('view'); | ||
expect(view).toHaveTextContent('Hello World'); | ||
expect(view).not.toHaveTextContent('Hello there'); | ||
}); | ||
|
||
test('toHaveTextContent() handles positive test cases', () => { | ||
render(<Text testID="text">Hello World</Text>); | ||
|
||
const text = screen.getByTestId('text'); | ||
expect(text).toHaveTextContent('Hello World'); | ||
expect(text).toHaveTextContent('Hello', { exact: false }); | ||
expect(text).toHaveTextContent(/Hello World/); | ||
}); | ||
|
||
test('toHaveTextContent() does exact match by default', () => { | ||
render(<Text testID="text">Hello World</Text>); | ||
|
||
const text = screen.getByTestId('text'); | ||
expect(text).toHaveTextContent('Hello World'); | ||
expect(text).not.toHaveTextContent('Hello'); | ||
expect(text).not.toHaveTextContent('World'); | ||
}); | ||
|
||
test('toHaveTextContent() handles nested text', () => { | ||
render( | ||
<Text testID="text"> | ||
Hello <Text>React</Text> | ||
</Text> | ||
); | ||
|
||
const text = screen.getByTestId('text'); | ||
expect(text).toHaveTextContent('Hello React'); | ||
}); | ||
|
||
test('toHaveTextContent() negative test cases', () => { | ||
render(<Text testID="text">Hello World</Text>); | ||
|
||
const text = screen.getByTestId('text'); | ||
expect(text).not.toHaveTextContent(/Hello React/); | ||
expect(() => expect(text).toHaveTextContent(/Hello React/)) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toHaveTextContent() | ||
Expected element to have text content: | ||
/Hello React/ | ||
Received: | ||
Hello World" | ||
`); | ||
|
||
expect(text).not.toHaveTextContent('Yellow', { exact: false }); | ||
expect(() => expect(text).toHaveTextContent('Yellow', { exact: false })) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toHaveTextContent() | ||
Expected element to have text content: | ||
Yellow | ||
Received: | ||
Hello World" | ||
`); | ||
}); | ||
|
||
test('toHaveTextContent() on null element', () => { | ||
expect(() => expect(null).toHaveTextContent('Hello World')) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(received).toHaveTextContent() | ||
received value must be a host element. | ||
Received has value: null" | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint } from 'jest-matcher-utils'; | ||
import { getTextContent } from '../helpers/text-content'; | ||
import { TextMatch, TextMatchOptions, matches } from '../matches'; | ||
import { checkHostElement, formatMessage } from './utils'; | ||
|
||
export function toHaveTextContent( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance, | ||
expectedText: TextMatch, | ||
options?: TextMatchOptions | ||
) { | ||
checkHostElement(element, toHaveTextContent, this); | ||
|
||
const text = getTextContent(element); | ||
|
||
return { | ||
pass: matches(expectedText, text, options?.normalizer, options?.exact), | ||
message: () => { | ||
return [ | ||
formatMessage( | ||
matcherHint( | ||
`${this.isNot ? '.not' : ''}.toHaveTextContent`, | ||
'element', | ||
'' | ||
), | ||
`Expected element ${this.isNot ? 'not to' : 'to'} have text content`, | ||
expectedText, | ||
'Received', | ||
text | ||
), | ||
].join('\n'); | ||
}, | ||
}; | ||
} |