I have a React Component which calls a method from another module.
The module just returns 1 method:
export default (url, method, authorization, body) => {
const headers = { 'Content-Type': 'application/json' }
if (authorization) headers.Authorization = localStorage.getItem('id_token');
return fetch(url, {
method,
headers,
body: JSON.stringify(body)
}).then(res => res.json());
}
I'd like to mock this method/module, so the real code doesn't get called when testing the Components using it.
I'm a novice at React/Jest/Enzyme so I could be missing something easy.
In my test file:
jest.mock('../../utils/fetch.js', () => () => {
Promise.resolve({_id: 1});
});
import fetch from '../../utils/fetch';
Where the method is used:
return fetch('/api/items', 'post', true, newItem).then(res => {
this.props.history.push(`/items/${res._id}`);
}).catch(console.log);