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 jquery), but it didn't seem to trigger anything:
import { mount } from 'enzyme';
page = <MyCoolPage />;
pageMounted = mount(page);
const button = pageMounted.find('#some_button');
expect(button.length).toBe(1); // it finds it alright
button.simulate('click'); // nothing happens
You may use something like this to call the handler written on click:
#1 Using Jest
This is how I use the jest mock callback function to test the click event
I am also using a module called enzyme Enzyme is a testing utility that makes it easier to assert and select your React Components
#2 Using Sinon
Also you can use another module called sinon which is a standalone test spies, stubs and mocks for JavaScript. This is how does it look
#3 Using Your own Spy
Finally you can make your own naive spy
Using jest you can do it like this: