am just learning testing in React using shallow
I have a parent Component and a child component
The parent component has one state defined as:
const [searchOpen, setSearchOpen] = useState(false);
const performAnAction = () => {
setSearchOpen(false);
}
const changeSearch = async () => {
await setSearchOpen(true);
};
and inside the return
of this parent component is calling the child component, but it does check the condition flag searchOpen , like -
return (
if(searchOpen &&
<childComponent performAnAction= {performAnAction} changeSearch={changeSearch}/>
}
)
where performAnAction
is a function.
But in the parentComponent.test.js
it('should render the Child Component', async () => {
const component = shallow(<ParentComponent {...props} />);
const child = component.find('childComponent');
expect(child).toBeDefined();
await child.props().performAnAction();
});
But am getting this error:
Method “props” is meant to be run on 1 node. 0 found instead.
When I remove the flag, it's working as expected. But since on the page load it's false, I am not sure if it gets in the DOM or not.
So my guess is somehow we need to make the flag true in test case file, correct me if I'm wrong.
Guys this is very minimal code for you to understand, feel free to comment, if any issues, since am new in the platform