jeudi 27 juillet 2017

Cannot simulate click on jest test

I'm learning some react + testing with jest, I have a component which has three numeric inputs and a button. It's a simple calculator with the multiply operation only.

Don't worry about the rest of the code, I'm just trying to simulate the click that performs the operation (in this case it will call a mocked function as shown in the test)

This is the test

function setup() {
    const component = shallow(<Multiplier />);

    return {
       component: component,
       button: component.find('#multiplyButton')
    }
}

describe('Given a Multiplier instantiated', () => {
   it('The button perform operation should invoke the click method', () => {
       const { button } = setup();
       const handleClick = jest.fn();
       button.onclick = handleClick;
       button.simulate('onClick');
       expect(handleClick).toBeCalled();
   });
});

I think the button.onclick assignment is wrong, I tried with prototype and ecma6 Object.assign but it was the same result, the test fails.

I mocked the onclick button with jest.fn(), once the button is pressed it should call this method, but it's always telling me:

Given a Multiplier instantiated › The button perform operation should invoke the click method expect(jest.fn()).toBeCalled()

Any ideas on how to solve this?

Aucun commentaire:

Enregistrer un commentaire