What I am trying to test in MyComponent
is the value of checked
props of the Switch
component which is a props of FormControlLabel
component:
class MyComponent extends Component {
(...)
render() {
return (
<FormControlLabel
name={`formControl`}
control={
<Switch
name={`switch`}
data
checked={this.state.isChecked}
onClick={this.handleChange}
value={checked}
/>
}
/>
);
}
}
I can access the FormControlLabel
component like this:
const wrapper = shallow(<MyComponent />);
wrapper.find('[name="formControl"]');
I tried to access the Switch
component like this but it's not working:
wrapper.find('[name="switch"]');
How can I access the checked
props of Switch
component ?
The child component uses the props argument to know what to render. Now, we can pass a function to the props object. You see we passed a function () => [ "nnamdi", "chidume" ] to ChildComponent via name ,then it can access it by referencing it as key in the props argument: this.props.name .
You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop. Copied!
Well, I just found a solution:
expect(wrapper.find('[name="formControl"]').prop('control').props.checked).toEqual(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With