Want to test component callback invoked by child component callback in React with Enzyme? Use the below example:
1 2 3 4 5 6 7 8 9 10 11 12 | import { shallow } from 'enzyme'; import { stub } from 'sinon'; describe('<Parent/>', () => { it('should handle a child click', () => { const onParentClick = stub(); const wrapper = shallow(<Parent onParentClick={onParentClick} />); wrapper.find("Child").prop('onChildClick')('foo'); expect(onParentClick.callCount).to.be.equal(1); // You can also check if the 'foo' argument was passed to onParentClick }); }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.