I have, I think, a simple case. I am using a function in my component that is imported from a different file. I've mocked the function as shown below. I've also tried adding a mock implementation, but that didn't work either. If I put a print statement in my original function, that appears when I run the test. If I understand correctly, a mock should replace the original.
I've tried to make this as straightforward as possible, so I'm not sure what I'm missing.
test file:
import AddStoryModal from './AddStoryModal';
import addStory from '../api/add_story';
jest.mock('../api/add_story');
describe('Story tests', () => {
it('makes an API call to add a story', () => {
render(<AddStoryModal />);
const submitButton = screen.getByTestId('add-submit');
fireEvent.click(submitButton);
expect(addStory).toHaveBeenCalled();
});
});
result:
expect(jest.fn()).toBeCalled()
Expected number of calls: >= 1
Received number of calls: 0
component being tested:
...
import addStory from '../api/add_story';
function AddStoryModal(){
const onFinish = (values) => {
addStory();
};
return (
<>
<Button type="primary" data-testid="add-story" onClick={showModal}>
Create New +
</Button>
<Modal title="Add New Story ..." visible={visible} data-testid='add-story-modal' onCancel={handleCancel} footer={null}>
<Form form={form} layout="vertical" onFinish={onFinish}>
...
<Form.Item>
<Button type="primary" data-testid="add-submit" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
</Modal>
</>
);
}