I have a function that I want to test using mocha and chai's expect method. The function uses "fetch" and "dispatch" as functions. "fetch" is responsible for making AJAX calls to the server.
Here is the function:
function fetchMetadata() {
return (dispatch) => {
fetch('/json/metadata').then(
(result) => {
if (result.status === 200) {
return result.json();
}
throw "request failed";
}
).then(
(jsonResult) => {
dispatch(jsonResult);
}
);
}
}
I'm having trouble testing it because the error is being swallowed by the promise. To address this, I am using "fetchMock," a function that mocks the "fetch" function.
Here is my test code:
describe('Should work', () => {
before(() => {
fetchMock.mock('^/json/metadata', {body1:"testing"})
});
it.only('should fetch metadata when fetchMetadata is called ', function(){
let returnedFetchMetadataFn = fetchMetadata();
let mockDispatch = (argument) => {
expect(argument).toBe('SET_METADATA1');
};
returnedFetchMetadataFn(mockDispatch);
});
});
I can't seem to get the test to fail. What am I missing or doing wrong?