I am new to redux-saga testing.
I am trying to write tests for the following generator functions, using jest and redux-saga-test-plan:
//saga.ts
export function* chooseCorrectGenerator(action: {type: string, payload: { flag: boolean } }){
if(action.payload.flag) {
yield* behaviour1(action);
}
else {
yield* behaviour2(action);
}
}
export function* behaviour1(action: { type: string, payload: { flag: boolean } }) {
// do work
}
export function* behaviour2(action: { type: string, payload: { flag: boolean } }) {
// do work
}
I want to write a test that checks if the flag is set to true, the behaviour1 function is called. I have separate tests for behaviour1, so I wish to mock its implementation.
This:
// saga.test.ts
import * as sagaFunctions from './saga';
import { expectSaga } from 'redux-saga-test-plan';
test('behaviour test', async () => {
const testSpy = jest.spyOn(sagaFunctions, "behaviour1");
await expectSaga(sagaFunctions.chooseCorrectGenerator, {
type: "TEST_ACTION",
payload: { flag: true}
})
.run();
expect(testSpy).toBeCalled();
});
Results in:
expect(jest.fn()).toBeCalled()
Expected number of calls: >= 1
Received number of calls: 0
Please note this is a simplified example, the real code has some redux-saga side effects (like select, put, etc)
How can I mock behaviour1?
Is there a better way to test chooseCorrectGenerator other than mocking behaviour1?