Simulating a button click seems like a very easy/standard operation. Yet, I can\'t get it to work in Jest.js tests.
This is what I tried (and also doing it using jQue
I needed to do a little bit of testing myself of a button component. These tests work for me ;-)
import { shallow } from "enzyme";
import * as React from "react";
import Button from "../button.component";
describe("Button Component Tests", () => {
it("Renders correctly in DOM", () => {
shallow(
);
});
it("Expects to find button HTML element in the DOM", () => {
const wrapper = shallow()
expect(wrapper.find('button')).toHaveLength(1);
});
it("Expects to find button HTML element with className test in the DOM", () => {
const wrapper = shallow()
expect(wrapper.find('button.test')).toHaveLength(1);
});
it("Expects to run onClick function when button is pressed in the DOM", () => {
const mockCallBackClick = jest.fn();
const wrapper = shallow();
wrapper.find('button').simulate('click');
expect(mockCallBackClick.mock.calls.length).toEqual(1);
});
});