mock javascript function
// ---- comp.js ----
import * as React from 'react';
import * as comp from './comp';
export const remove = () => {
// ...do something
}
export const RemoveButton = (props) => (
<div onClick={() => comp.remove()}>
Remove
</div>
);
// ---- comp.test.js ----
import * as React from 'react';
import { shallow } from 'enzyme';
import * as comp from './comp';
describe('removeButton', () => {
it('should call remove on click', () => {
const mock = jest.spyOn(comp, 'remove');
mock.mockImplementation(() => {});
const component = shallow(<comp.RemoveButton />);
component.find('div').simulate('click');
expect(mock).toHaveBeenCalled();
mock.mockRestore();
});
});