Search code examples
javascriptunit-testingreact-reduxsinonredux-saga

How to use sinon unit test redux saga spawn function


I'm stuck on my redux-saga call with spawn call unit test, need someone extra help, big thanks!

My saga

export function* sampleTest() {
    try {
        yield put(someData1);
        yield spawn(someData2);
        yield spawn(someData3);
    } catch (error) {
        console.log('error', error);
    }
}
}

someData2 code is an API call this one returns an array with one object.

My test

describe('For my spawn test', () => {
    it('Initialize saga', () => {
        const someData2Spy = sinon.spy();
        const gen = sampleTest({});
        expect(gen.next().value).to.deep.equal(put(someData1));
      expect(gen.next(someData2Spy).value).to.be.deep.equal(spawn(someData2));  <--- Does not work
        // expect(gen.next()).to.be.deep.equal(spawn(someData3));  <--- Does not work
        // expect(gen.next().done).to.equal(true);
    });
});

I will get an error for

Attempted to wrap undefined property undefined as function

or

Tried expect(gen.next()).to.be.deep.equal(spawn(someData2)); get this error: 
expected { Object (@@redux-saga/IO, combinator, ...) } to equal { Object (@@redux-saga/IO, combinator, ...) }

And I tried this one does not work for me. Thank you


Solution

  • Find out this work for me now:

    describe('For my spawn test', () => {
      it('Initialize saga', () => {
        expect(gen.next({}).value).to.deep.equal(put(someData1));
        expect(gen.next({}).value).to.be.deep.equal(spawn(someData2));
        expect(gen.next({}).value).to.be.deep.equal(spawn(someData3));
     });
    });
    

    Thanks for the help!