0

My action is like this:-

export function requestModel(param) {
  return (dispatch) => {
    dispatch({
      type: 'REQUEST',
      loadModel: true,
     });
    dispatch(getModel(param)).then(response => response.json())
      .then(model=>
        dispatch(receiveModel(model)))
      .catch(() => {
          dispatch({
          type: MODEL_FAILURE,
          loadModel:false,
        });
      });
  };
}

I wrote the test case to cover catch block as :-

it('requestModel() handle sxception...............', (done) => {
      const response = { json: () => data };
    // call the actual function with stubbed function
    try {
      dispatchStub.withArgs(getDeviceApiStub).returns(Promise.reject(response));
      dispatchStub.withArgs(getDeviceActionStub).returns(Promise.resolve(response));
      const returnFunction = modelAction.requestModel(actionParam);
      returnFunction(dispatchStub);
      done();
      sinon.assert.calledWithMatch(dispatchStub, {
       type: MODEL_FAILURE,
      loadModel:false,
      });
      done();
    } catch (err) {
      done(err);
    }
  });

but the issue is that before catch block of method it is calling the sinon.assert as i wrote above. How to deal with this, i used async await also but same issue is coming, Is there any why so that I can write test cases for the catch block of my action in reactjs?

Gorakh Nath
  • 9,140
  • 15
  • 46
  • 68
  • The test code needs to be able to inject test substitutes for `getModel` and `receiveModel` in order to be able to control the execution. You don't show any code that allows this, which leaves two options. Either use a link seam or use dependency injection. You can read up on using link seams here: http://sinonjs.org/how-to/link-seams-commonjs/ – oligofren Jun 11 '17 at 12:02
  • You might also get something out of this: https://stackoverflow.com/questions/44397639/how-to-test-an-es6-class-that-needs-jquery/44482001#44482001 – oligofren Jun 11 '17 at 12:05
  • Please provide the code of `getModel` and `receiveModel`. – Lin Du Dec 26 '20 at 07:24

0 Answers0