I've got a simple ponent wrapped around a Material UI Checkbox. I've stripped it down to the bare minimum here.
//@flow
import React from "react";
import { Checkbox } from "@material-ui/core";
function MyCheckboxComponent() {
const [checkedState, setCheckedState] = React.useState(true);
const handleChange = event => {
setCheckedState(event.target.checked);
};
return <Checkbox checked={checkedState} onChange={handleChange} />;
}
export default MyCheckboxComponent;
I simply want to test this ponent and toggle the Checkbox value and check it. I cannot get my simple test passing. I'm at a loss as to why.
import React from "react";
import Enzyme, { mount } from "enzyme";
import { Checkbox } from "@material-ui/core";
import Adapter from "enzyme-adapter-react-16";
import MyCheckboxComponent from "./MyCheckboxComponent";
Enzyme.configure({ adapter: new Adapter() });
/** Interaction tests testing user interaction with PilzButton */
test("Toggle Checkbox test", () => {
const wrapper = mount(<MyCheckboxComponent />);
const checkBox = wrapper.find(Checkbox);
expect(checkBox).toHaveLength(1);
checkBox.simulate('change', { target: { checked: true } });
expect(checkBox.props().checked).toBe(true);
});
Should
checkBox.simulate('change', { target: { checked: true } });
work and toggle the value of the Checkbox
??
What I have so far is here on CodeSandbox ...
I've got a simple ponent wrapped around a Material UI Checkbox. I've stripped it down to the bare minimum here.
//@flow
import React from "react";
import { Checkbox } from "@material-ui/core";
function MyCheckboxComponent() {
const [checkedState, setCheckedState] = React.useState(true);
const handleChange = event => {
setCheckedState(event.target.checked);
};
return <Checkbox checked={checkedState} onChange={handleChange} />;
}
export default MyCheckboxComponent;
I simply want to test this ponent and toggle the Checkbox value and check it. I cannot get my simple test passing. I'm at a loss as to why.
import React from "react";
import Enzyme, { mount } from "enzyme";
import { Checkbox } from "@material-ui/core";
import Adapter from "enzyme-adapter-react-16";
import MyCheckboxComponent from "./MyCheckboxComponent";
Enzyme.configure({ adapter: new Adapter() });
/** Interaction tests testing user interaction with PilzButton */
test("Toggle Checkbox test", () => {
const wrapper = mount(<MyCheckboxComponent />);
const checkBox = wrapper.find(Checkbox);
expect(checkBox).toHaveLength(1);
checkBox.simulate('change', { target: { checked: true } });
expect(checkBox.props().checked).toBe(true);
});
Should
checkBox.simulate('change', { target: { checked: true } });
work and toggle the value of the Checkbox
??
What I have so far is here on CodeSandbox ...
Share Improve this question asked Apr 30, 2020 at 16:02 Simon LongSimon Long 1,4505 gold badges25 silver badges44 bronze badges2 Answers
Reset to default 6Newest versions of enzyme cache the results returned from find
and other methods.
You need to re-find and also use .update()
to force the refresh of the state to re-render.
const checkBox = wrapper.find(Checkbox);
expect(checkBox).toHaveLength(1);
checkBox.simulate('change', { target: { checked: true } });
wrapper.update();
expect(wrapper.find(Checkbox).props().checked).toBe(true);
Also, this may just be because you wanted to produce a minimal reproducible question but your test is poor at the moment because your default value is true and you are passing true in dynamically.
You should do this instead:
const checkBox = wrapper.find(Checkbox);
expect(checkBox).toHaveLength(1);
expect(checkBox.props().checked).toBe(true);
checkBox.simulate('change', { target: { checked: false } });
wrapper.update();
expect(wrapper.find(Checkbox).props().checked).toBe(false);
This test is actually proving that the onChange
works properly now.
const checkBox = wrapper.find(Checkbox);
expect(checkBox).toHaveLength(1);
expect(checkBox.props().checked).toBe(false);
checkBox.prop("onChange")({ currentTarget: { checked: true } });
wrapper.update();
expect(wrapper.find(Checkbox).props().checked).toBe(true);
Just a minor update to the answer above. The checkbox state doesn't change with "simulate" and instead works with "prop" for me.