I have a saga that yields a put. One of the arguments to that put is a function object.
I've copied the saga function only as far as it is relevant:
function* updateTaskTimeCancelledTimeRejectedSuccess(action) {
const {payload} = action.data
const tasks = yield select(getTasksSelector);
const parent = yield call(findExistingTaskParent, tasks, action.data.taskUUID);
if (action.type === taskActions.updateTaskCancelledTimeActions.success) {
const currentValue = parent.taskGroup[action.data.taskUUID].time_cancelled;
if (currentValue === null) {
// only notify if marking rejected for the first time
const restoreActions = yield () => [taskActions.updateTaskCancelledTimeRequest(
action.data.taskUUID,
{time_cancelled: null}
)];
const viewLink = `/task/${encodeUUID(action.data.taskUUID)}`
yield put(displayInfoNotification("Task marked cancelled", restoreActions, viewLink))
This is the test:
it("updates the task state with cancelled time and sends a notification", () => {
const action = {type: taskActions.updateTaskCancelledTimeActions.success, data: {taskUUID: "someUUID", time_cancelled: new Date().toISOString()}};
const restoreActions = () => [taskActions.updateTaskCancelledTimeRequest(
action.data.taskUUID,
{time_cancelled: null}
)];
const viewLink = `/task/${encodeUUID(action.data.taskUUID)}`
return expectSaga(testable.updateTaskTimeCancelledTimeRejectedSuccess, action)
.provide([
[select(getTasksSelector), {
tasksNew: {
1: {
someUUID: {
time_cancelled: null,
parent_id: 1
}
}
}
}]])
.put(displayInfoNotification("Task marked cancelled", restoreActions, viewLink))
.run()
})
})
and this is the result:
SagaTestError:
put expectation unmet:
Expected
--------
{ '@@redux-saga/IO': true,
combinator: false,
type: 'PUT',
payload:
{ channel: undefined,
action:
{ type: 'DISPLAY_INFO_NOTIFICATION',
message: 'Task marked cancelled',
restoreActions: [Function: restoreActions],
viewLink: '/task/0000000000000000000000' } } }
Actual:
------
1. { '@@redux-saga/IO': true,
combinator: false,
type: 'PUT',
payload:
{ channel: undefined,
action:
{ type: 'DISPLAY_INFO_NOTIFICATION',
message: 'Task marked cancelled',
restoreActions: [Function],
viewLink: '/task/0000000000000000000000' } } }
at node_modules/redux-saga-test-plan/lib/expectSaga/expectations.js:48:13
at node_modules/redux-saga-test-plan/lib/expectSaga/index.js:544:7
at Array.forEach (<anonymous>)
at checkExpectations (node_modules/redux-saga-test-plan/lib/expectSaga/index.js:543:18)
It's obvious to me that the equality is failing because of the restoreActions object which is a function of a different reference. I can't figure out what I should do instead to make the test pass. What do I need to do to verify the saga is flowing as I expected? I don't necessarily need to verify the result of the function, just that the displayInfoNotification put took place.
Thank you.
Make a factory function for your restoreActions
lambda and then you can mock it for your test.
e.g.
// somewhere in your module
const actionRestoreFactory = (action) => () => [taskActions.updateTaskCancelledTimeRequest(action.data.taskUUID, {time_cancelled: null})]
//In your saga
yield put(displayInfoNotification("Task marked cancelled", actionRestoreFactory(action), viewLink))
//In your test
import actionRestoreFactory from './your/module'
jest.mock('./your/module')
actionRestoreFactory.mockReturnValue(jest.fn())
//...
return expectSaga(testable.updateTaskTimeCancelledTimeRejectedSuccess, action)
.provide([ ... ])
.put(displayInfoNotification("Task marked cancelled", actionRestoreFactory(action), viewLink))
.run()