You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been testing my application, but the text match doesn't work properly, I've come across 2 bugs, one which is covered by #59 and another that is described in this issue.
What happened:
The toHaveTextContent doesn't validate properly all the text content of the given element.
Reproduction:
import{render,act}from'_test_utils/unit-render';importReactfrom'react';import{View,Text,TouchableOpacity}from'react-native';constHeyIDontHaveChildren=({ text })=>{return(<TouchableOpacity><Text>{text}</Text></TouchableOpacity>);};constFoo=()=>{constversion='2.16.0 (v2)';return(<ViewtestID="VersionViewContainer"><Text>{version}</Text><HeyIDontHaveChildrentext={'Verificar atualizações'}/></View>);};describe('Foo',function(){test('Text test',async()=>{letroot=render(<Foo/>);awaitact(async()=>{});expect(root.getByTestId('VersionViewContainer')).toHaveTextContent('2.16.0 (v2) Verificar atualizações',);});});
Only handles components that receive children, so, if I have a custom component that doesn't receive children but instead takes a prop and passes it down bellow to its children, the getText code will not collect properly this text..
Suggested solution:
Can you help us fix this issue by submitting a pull request?
I probably can fix this.. but I know myself and I'm lazy so it will take a while to open a PR. So here's my suggestion to fix this:
I'm unsure about corner cases and about why props.children was used in the first place.. but instead of relying on props.children, use children directly:
constgetChildrenText=(element)=>{if(!element.children){returntypeofelement==='string' ? element : ''}// since this is an example this is the code.. but I would use a imperative approach with mutability for perf insteadreturnelement.children.reduce((texts,child)=>[
...texts,
...getChildrenText(child)],[])}getChildrenText(root.getByTestId('VersionViewContainer')).join('')// 2.16.0 (v2) Verificar atualizações
Oh, and the ramda part can look like this:
import{pipe,join,}from'ramda'// (I would also recommend using babel-transform-imports to transform ramda imports)constcollectText=pipe(getChildrenText,join(''),normalize)consttoHaveTextContent=(element,checkWith)=>{consttextContent=collectText(element)....
}
The text was updated successfully, but these errors were encountered:
Grohden
changed the title
toHaveTextContent doesn't work when a custom component doesn't use children
toHaveTextContent doesn't work properly when a custom component doesn't use children
Jun 18, 2021
Uh oh!
There was an error while loading. Please reload this page.
react-native
version: 0.64.2@testing-library/react-native
version: 7.2.0jest-preset
: 'react-native'node
version: v15.13.0@testing-library/jest-native
version: 4.0.1Relevant code or config:
See reproduction section.
What you did:
I've been testing my application, but the text match doesn't work properly, I've come across 2 bugs, one which is covered by #59 and another that is described in this issue.
What happened:
The
toHaveTextContent
doesn't validate properly all the text content of the given element.Reproduction:
Problem description:
The current code of the getText:
jest-native/src/to-have-text-content.js
Lines 6 to 18 in 3cfd279
Only handles components that receive children, so, if I have a custom component that doesn't receive children but instead takes a prop and passes it down bellow to its children, the getText code will not collect properly this text..
Suggested solution:
Can you help us fix this issue by submitting a pull request?
I probably can fix this.. but I know myself and I'm lazy so it will take a while to open a PR. So here's my suggestion to fix this:
I'm unsure about corner cases and about why
props.children
was used in the first place.. but instead of relying onprops.children
, usechildren
directly:Oh, and the ramda part can look like this:
The text was updated successfully, but these errors were encountered: