I am trying to test that my API service has been called using jest mocks, Most of the examples I have seen all use jest.mock('axios') I don't want to rely on the Axios library but be independent of any of this. Currently, my output assertion is 0 instead of 1. Can anyone see where I might be going wrong?
service.ts
import api from './api';
const postRequest = async (id: string, name: string) => {
const response = await api.post(`contests/${id}/joinedteams/${teamId}`)
.then((res) => res.status)
.catch((error) => Promise.reject(error));
return response;
};
export default postRequest;
Form.ts
const onSubmit = async (formData: any) => {
const nameId = formData.name;
await postJoinContest(id, nameId);
handleModalClose();
};
test.ts
const MockService = postRequest: jest.fn((_id: string, teamId: string) => console.log('called'))
jest.mock('../../services/', () => ({
__esModule: true,
default: {
postRequest: (id: string, name: string) => MockService
}
}));
it('submit a valid form', async () => {
render(component);
fireEvent.click(screen.getByRole('button'));
await expect(MockService).toHaveBeenCalled();
expect(mockService.postJoinContest).toHaveBeenCalledWith('6fafae7c-495e-4e18-9709-3c08b66b3b3d', 'paul');
});