I have a createPostSaga which POST
s to my API and I am trying to test it. This is the saga:
export function* createPostSaga(action) {
const token = yield select(selectToken);
const headerParams = {
Authorization: `JWT ${token}`
};
const apiCall = () => {
let formData = new FormData();
formData.append("title", action.payload.title);
formData.append("content", action.payload.content);
formData.append("thumbnail", action.payload.thumbnail);
formData.append("tags", JSON.stringify(action.payload.tags));
formData.append("slug", generateSlug(action.payload.title));
return axios({
method: "post",
url: "/posts/",
data: formData,
headers: headerParams
})
.then(response => response.data)
.catch(err => {
throw err;
});
};
try {
const response = yield call(apiCall);
yield put(push(`/posts/${response.id}/${response.slug}/`));
} catch (error) {
console.log(error);
}
}
and this is the code I have so far that is failing:
describe("createPostSaga", () => {
const action = {
type: types.CREATE_POST,
payload: {
title: "test title",
pub_date: "2018-11-12",
content: "test content",
tags: ["test1", "test2"],
thumbnail: "http://fail",
slug: "test-title"
}
}
const apiCall = () => ({
id: 1,
...action.payload
});
it("calls the api and redirects", () => {
testSaga(actions.createPostSaga, action)
.next()
.select(selectToken)
.next()
.call(apiCall)
.next()
.put(push("/posts/1/test-title/"))
.finish()
.isDone();
})
})
When I test this code, I receive the following error:
SagaTestError:
Assertion 2 failed: call effects do not match
Expected
--------
{ context: null, fn: [Function: apiCall], args: [] }
Actual
------
{ context: null, fn: [Function: apiCall], args: [] }
As you can see the expected and actual results are the same.
I have also tried to directly copy paste the apiCall
function into my test but it fails with the same message and I am stumped.