Testing isomorphic-fetch used in ReactJs

Viewed 1740

I have the following function and i want to test it using mocha and chai, expect.

Here "fetch" and "dispatch" is a function. "fetch" is used to do ajax calls to the server

function fetchMetadata() {
   return (dispatch) => {
       fetch('/json/metadata').then(
           (result) => {
               if (result.status === 200) {
                   return result.json();
               }
               throw "request failed";
           }
       ).then(
           (jsonResult) => {
               dispatch(jsonResult);
           }
       );
   }
}

I am trying to test is as following but i cant get the test to fail as the error is swallowed by the promise!

"fetchMock" is a function which mocks "fetch" function so that we dont actually make a call to server to get the data back.

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);

    });
});

What am i doing wrong or what am i missing??

1 Answers
Related