I want to test a saga that yields a take effect which takes a function as pattern, e.g.
export function* mySaga(myAction: {
type: string;
}): Generator<TakeEffect, boolean, { type: string }> {
const { type: actionType } = (yield take(
(action: any) => action.type === myAction.type,
)) as { type: string };
return actionType === myAction.type;
}
with a test that looks like:
it('Should test mySaga', () => {
testSaga(mySaga, { type: 'myActionType' }).next().take().next().finish();
});
but I get the following error:
SagaTestError:
Assertion 1 failed: take effects do not match
Expected
--------
{ '@@redux-saga/IO': true,
combinator: false,
type: 'TAKE',
payload: { pattern: '*' } }
Actual
------
{ '@@redux-saga/IO': true,
combinator: false,
type: 'TAKE',
payload: { pattern: [Function] } }
I have not been able to find how to assert the a take effect that takes a function pattern instead of a string. Can someone please help me?
Since you pass an anonymous function to
takeeffect creator. We can't get this anonymous function in the test case using the module requiring.You can use inspect general assertions helper.
So that we can get the returned effect of
yield take((action: any) => action.type === myAction.type). Then, assert this effect.E.g. (using jestjs as testing framework)
saga.ts:saga.test.ts:unit test result: