I am learning javascript and testing and I don’t know how to continue with this test.
I have this file api.test.js, I want to test my function getInfo() that have some fetch…
global.fetch = jest.fn();
async function getInfo(url){
try{
const response = await fetch(`https://dwec-tres-en-raya.herokuapp.com` + url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
withCredentials: true,
credentials: 'include',
"authorization": `Bearer token`
}
});
const data = await response.json();
return data;
}catch(error){
console.log(error);
}
}
describe('(your function for doing API requests here)', () => {
it('returns the json data', async () => {
const response = {
status: 200,
json: () => { return {id: 3, username: 'player1', name: 'Player 1'}}
}
fetch.mockResolvedValueOnce(response);
return getInfo('/player/3/')
.then(response =>
{expect(response).toStrictEqual({id: 3, username: 'player1', name: 'Player 1'});
console.log(fetch.mock.calls);
})
}
);
})
I know I have to use fetch.mock.calls but how to use it in this situation? What I am doing wrong? Any advice?
Thank you so much.