im newbie in jest/enzyme testing with react and im trying to test react Button Component by passing props and im getting this error Cannot read property 'onIncrement' of undefined.
describe("Name of the group", () => {
const counter = 'pc';
const onIncrement = jest.fn();
const props = {
onIncrement,
counter
};
it("should be clicked ", () => {
const button = shallow(<Button {...{props}}>Increment</Button>);
button.find(".increment").simulate("click");
expect(button).toHaveBeenCalledWith('pc');
});
});
import React from "react";
export const Button = ( props ) => {
return (
<button
id="inc"
className="increment"
onClick={() => props.onIncrement(props.counter)}
>
Increment
</button>
);
};
({ props }). It's(props). Alsojest.fn((e) => (onIncrement = e))doesn't make sense, why would you reassign spy function, not to mention that it's a constant? - Estus Flask<Button {...{props}}>is an another mistake. It will pass props object with props key. It should be<Button {...props}>as it was before. The answer mentions it because<Button {...{props}}>is a way that could make({ props })work, but it shouldn't be like that any way. - Estus Flask