How to mock axios requests with jest and enzyme?
Hi. I have simple Rails API and i want to test my axios requests.
How to mock post, delete and e.t.c?
My goal is:
-
1.Test response code
-
2.Test response body
-
3.Test that request sent only once
Here's my test:
it('should add have ok response', async () => {
mockAxios.get.mockImplementationOnce(() =>
Promise.resolve({
list: {
board_id: 1,
title: "Sample list"
}
})
);
const response = await postList(1);
expect(response).toEqual({ board_id: 1, title: "Sample list" });
expect(mockAxios.post).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith(
"https://localhost:8080/api/v1/lists",
{
params: {
board_id: 1,
title: "Sample list",
}
}
);
});
And postList
function:
export const postList = async (board_id) => {
try {
const result = await FetchHelpers.post(`${API_ROOT}/lists`, {
list: {
board_id: board_id,
title: "Sample list"
}
});
return result;
} catch (error) {
console.error(error);
}
}
But i get this error in test:
● CreateListButton › should add have ok response
expect(received).toEqual(expected) // deep equality
Expected: {"board_id": 1, "title": "Sample list"}
Received: undefined
What's wrong here and how to fix that?
Aucun commentaire:
Enregistrer un commentaire