0

I have a function which consists of saga effect calls i want to coverage the full function with out missing any line of code how can i test if condition here

export function* fetchFromSource() {
const dataTypeName = mapDataTypes(dataType);
      Iif (dataTypeName.length === 0) {
        return;
      }
      yield put(sourceActions.onRdsmSourcePlantRequestStarted());
}

how i test the dataTypeName.length using jest this is my mapDataTypes unit test method

it('should return appropriate dataType when mapDataTypes triggered', () => {
      const expected = 'Items';
      const actionDataType = action.payload.name;
      expect(expected).toEqual(saga.mapDataTypes(actionDataType));
    });

this is my next put test method

it('should return onRdsmSourcePlantRequestStarted action', () => {
      const expectedAction = {
        type: 'rdsm/sourceView/ON_RDSM_SOURCE_PLANT_REQUEST_STARTED',
      };
      const dataTypeName = '';
      const genNext = generator.next(dataTypeName);
      expect(genNext.value).toEqual(put(expectedAction));
    });

here test is passing to verify the put call without entering to if block. my question is how to verify the if block

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
kumar
  • 109
  • 2
  • 13

2 Answers2

0

Probably you should change the implementation of your saga, and make mapDataTypes call declarative: const dataTypeName = yield call(mapDataTypes, dataType). Then you can test it like this:

it('should end saga when there is no dataTypeName', () => {
  const dataTypeName = '';
  expect(generator.next().value).toEqual(call(mapDataTypes, dataType));
  expect(generator.next(dataTypeName).done).toBeTruthy();
});

it('should return onRdsmSourcePlantRequestStarted action', () => {
  const expectedAction = {
    type: 'rdsm/sourceView/ON_RDSM_SOURCE_PLANT_REQUEST_STARTED',
  };
  const dataTypeName = 'something';
  expect(generator.next().value).toEqual(call(mapDataTypes, dataType));
  expect(generator.next(dataTypeName).value).toEqual(put(expectedAction));
});
Osmel Mora
  • 66
  • 3
0

to test else block

it('should return onRdsmSourcePlantRequestStarted action', () => {
      const expectedAction = {
        type: 'rdsm/sourceView/ON_RDSM_SOURCE_PLANT_REQUEST_STARTED',
      };
      const dataTypeName = 'test';
      expect(generator.next(dataTypeName).value).toEqual(put(expectedAction));
    });

test for if block

it('should return undefined ', () => {
          const dataTypeName = '';
         expect(generator.next(dataTypeName).value).toBe(undefined));
        });
kumar
  • 109
  • 2
  • 13