score:1

Accepted answer

you pass your user component a mock function (jest.fn()) through its handlefollowclick and handlestarclick props, then simulate whatever is supposed to trigger the parent action (a click event on the <button /> or a change event on the <input />) and test whether the corresponding mock function was called.

i personally always use enzyme for this sort of thing, but here's how i'd assume it works using react-test-renderer based on this answer:

const mockfollowclick = jest.fn();
const mockstarclick = jest.fn();

const tree = renderer.create(<user
    {...users[0]}
    handlefollowclick={mockfollowclick}
    handlestarclick={mockstarclick}
/>)

const button = tree.root.findbytype('button');
const input = tree.root.findbytype('input');

button.props.onclick();
expect(mockfollowclick).tohavebeencalled();

input.props.onchange();
expect(mockstarclick).tohavebeencalled();

you can even check if it was called with the correct user id:

button.props.onclick();
expect(mockfollowclick).tohavebeencalledwith("5d552d0058f193f2795fc814");

Related Query

More Query from same tag