I'm testing a simple redux saga function using redux test plan, the get posts test is returning a failure
SagaTestError:
put expectation unmet:
Expected
--------
{ '@@redux-saga/IO': true,
combinator: false,
type: 'PUT',
payload: { channel: undefined, action: { type: 'GET_POSTS_INIT' } } }
I'm testing to check if the posts array is empty being that the server is not running, is this the right approach to testing this function ?
post saga test
it(" fetchs post failure ", () => { // passes
const error = new Error("Whoops");
return expectSaga(getPosts)
.provide([[call(api.post.getPosts), throwError(error)]])
.put({ type: types.GET_POSTS_FAILURE, error: error })
.run();
});
it("should test fetches posts", () => { // this test doesn't pass
const posts = { posts: [] }; // is this the right approach ?
return expectSaga(watchPosts)
.provide([[call(api.post.getPosts), posts]])
.put({ type: types.GET_POSTS_INIT })
.dispatch({ type: types.GET_POSTS_SUCCESS, payload: posts })
.silentRun();
});
post saga
export function* getPosts() {
try {
const posts = yield call(api.post.getPosts); // call api from axios express back end
yield put(actionTypes.getPostsSuccess(posts));
} catch (error) {
yield put(actionTypes.getPostsFailure(error));
}
}
export function* watchPosts() {
yield takeLatest(types.GET_POSTS_INIT, getPosts);
}
You need to dispatch
types.GET_POSTS_INITaction towatchPostssaga and puttypes.GET_POSTS_SUCCESSaction with payloadposts.E.g.
index.ts:api.ts:types.ts:index.test.ts:unit test result with coverage report:
package versions: