I'm having trouble testing callbacks that a component provides to its children.
In the past, what I did was this:
const component = ReactDOM.render(<Subject />, node);
const collaborator = ReactTestUtils.findRenderedComponentWithType(component, Collaborator);
collaborator.props.onSubmit(); // now I have a handle on the collaborator so I can call the callbacks it was given to my heart's content and assert what they do
But the issue is that findRenderedComponentWithType
does not work with function components.
i.e. if Collaborator is implemented like this:
class Collaborator extends React.Component {
render() {
const { onSubmit } = this.props;
return <MyForm onSubmit={onSubmit} />;
}
}
findRenderedComponentWithType
will return a handle of the component, but if it is implemented like this:
function Collaborator({ onSubmit }) {
return <MyForm onSubmit={onSubmit} />;
}
findRenderedComponentWithType
won't find it!
Does anyone test like this? I thought this was how I can allow <Collaborator />
to evolve without needing to change the tests for <Subject />
:(
Here is a codesandbox that demonstrates:
https://codesandbox.io/s/qzy39n46wq
What with function components being the new hotness w/ hooks, this is a big problem for my testing approach.
Aucun commentaire:
Enregistrer un commentaire