I have a simple component which includes two buttons and the heading field. So far I tested button clicks but I want to test heading text field in the <h3> tag.
My component
class Popup extends React.Component {
render() {
return (
<div className="popupOuter">
<div className="popupInner text-center">
<br/>
<h3>{this.props.config.text}</h3>
<br/>
<div>
<Button type='NoButton' value={'No'} onClick={this.props.config.back} />
<Button type = 'YesButton' value={'Yes'} onClick={this.props.config.next}/>
</div>
</div>
</div>
);
}
}
My test
test('Text test ', ()=>{
const component = shallow(
<Popup config={config}/>
);
expect(component.find('h3')).toEqual("h3");
});
My test fails with
Error: expect(received).toEqual(expected) // deep equality Expected: "h3" Received: {}
What went wrong? Explanation pls?
Thanks.
.find(selector) => ShallowWrapper returns shallow wrapper, obviously, the shallow wrapper is not equal to the string
h3. If you want to get the text of thish3shallow wrapper, you need to call .text() => String on the shallow wrapper.E.g.
index.tsx:index.test.tsx:unit test results: