simulate event enzyme code example
Example: react ezyme simulate click
import React from "react";
class Button extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
this.onClick = this.onClick.bind(this);
}
onClick() {
this.setState({ count: 1 });
}
renderState() {
return this.state.count;
}
render() {
return (
<div>
<button onClick={this.onClick}>{this.count}</button>
</div>
);
}
}
export default Button;
import React from "react";
import { mount } from "enzyme";
import Button from "./Button";
describe("button test", () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<Button />);
});
it("test increment button", () => {
wrapper.find("button").simulate("click");
wrapper.setState({ count: 3 });
expect(wrapper.state("count")).toBe(3);
expect(wrapper.instance().renderState()).toBe(3);
});
});