0

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');
    });
skyboyer
  • 22,209
  • 7
  • 57
  • 64
NiseNise
  • 902
  • 4
  • 15
  • 30
  • Does this answer your question? [How do I test axios in jest](https://stackoverflow.com/questions/45016033/how-do-i-test-axios-in-jest) – A Jar of Clay Aug 24 '20 at 09:02
  • I have resolved the issue by doing the mock about the importing the component - which seems to enable me to see if it is called – NiseNise Aug 25 '20 at 11:31

0 Answers0