0

I am getting undefined as response for a mocked api call using jest.spyOn() and mockImplmentation.

Saga.tsx

export function* parseUserData(parsingAction: IParsingAction) {

try {
    const skills = yield call(SkillRepository.getAllSkills);

    console.log('------skils-----', skills);    // undefined

    const industries = yield call(IndustryRepository.getAllIndustry);

    if (skills.skillGroup && industry.industryGroup) 
      {

        yield put(getSkillsSuccess(skills.skillGroups));
        yield put(getIndustriesSuccess(industry.industryGroups));

      } 

    else 
   {
       yield put(bulkUploadError());
   }

    } catch (error) {

    yield put(bulkUploadError());

}

}

Saga.test.tsx

describe('ParseUserData job saga', () => {
const action = {
    type: bulkUploadActionType.GET_API_DATA,
    payload: {
        sheetData: {},
        isAdvisor: true,
    },
};
const generator = parseUserData(action);
it('Should call skill repository', () => {
    const skills = { skillGroups: [] };
    const spy = jest.spyOn(SkillRepository, 'getAllSkills');
    spy.mockImplementation((): any => {
        return (skills);
    });
    expect(generator.next().value).toEqual(call(SkillRepository.getAllSkills));
    spy.mockRestore();
});

});

Any help would be appreciated. Its very simple thing but I am stuck with this.

1 Answer 1

1

When testing sagas as pure generators, the redux saga middleware is not hooked up to drive the generator - you must do that yourself. So SkillRepository.getAllSkills is not actually called, instead you need to supply the expected value to generator.next(). You can see this in the first example of the Testing section where gen.next(chooseColor(color)) is called to drive the generator. In your case you'd do something like:

expect(generator.next().value).toEqual(call(SkillRepository.getAllSkills));
expect(generator.next(skills).done).toBe(true);

Since it's the redux saga middleware that is actually executing the call effect, if you want to test that SkillRepository.getAllSkill is called when your saga is run you'll need to follow the example under Testing the full Saga where runSaga is used to have redux-saga drive the generator, and where you can mock/spy on modules to see if they're called.

So I would re-write your test as:

it('Should call skill repository', () => {
    const skills = { skillGroups: [] };
    const spy = jest.spyOn(SkillRepository, 'getAllSkills');
    spy.mockImplementation((): any => {
        return (skills);
    });
    runSaga(
      {dispatch: () => {}, getState: () => ({})}, // flesh these out if need be
      parseUserData, action
    ).toPromise().then(() => {
      expect(SkillRepository.getAllSkills.mock.calls.length).toBe(1);
      spy.mockRestore();
    )
});

EDIT

For the unit test with the full saga as shared above:

describe('ParseUserData job saga', () => {
const action = {
    type: bulkUploadActionType.GET_API_DATA,
    payload: {
        sheetData: {},
        isAdvisor: true,
    },
};
const generator = parseUserData(action);
it('Should call skill repository', () => {
    const skills = { skillGroups: [] };
    const industries = { industryGroups: [] };

    expect(generator.next().value).toEqual(
      call(SkillRepository.getAllSkills));
    expect(generator.next(skills).value).toEqual(
      call(IndustryRepository.getAllIndustry));
    expect(generator.next(industries).value).toEqual(
      put(getSkillsSuccess(skills.skillGroups)));
    expect(generator.next().value).toEqual(
      put(getIndustriesSuccess(industries.industryGroups))
    );
    expect(generator.next().done).toBe(true);

});
4
  • But I am receiving false when i expect this. expect(generator.next(skills).done).toBe(true); Received false. But yes I am getting the skills on saga now but test case is failing. Nov 19, 2019 at 7:46
  • Is the parseUserData code you posted above the full saga? If there are more effects that you didn't include then the data won't be done yet. I imagine you're doing something with the result of getAllSkills.
    – azundo
    Nov 19, 2019 at 17:10
  • Yes I am using the result. I have updated the saga please see. Nov 21, 2019 at 11:50
  • Updated at the bottom of my post. Looks like your parseUserData generator has some typos though - should be skills.skillGroups and industry.industryGroups in the if statement before putting the successful actions.
    – azundo
    Nov 22, 2019 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.