I have this saga effects that calls to an API and dispatched an action if it is successful:
export function* getThemEffect() {
try {
yield put(requestActoin());
const data: AxiosResponse<ServerResponseSchema> = yield call(getStuff);
yield put(successAction(data.data.data));
} catch (err: any) {
yield put(failureAction(err?.response?.data || null));
}
}
This is the helper function:
export function getStuff() {
const config: AxiosRequestConfig = {
method: "GET",
url: "https://somewhere.com/api/get"
};
return axios(config);
}
The test suit for this saga looks like this:
import * as api from "../api";
const getStuffSpy = jest.spyOn(api, "getStuff");
describe("search saga", () => {
let gen: Generator, response: any, getStuffMock: jest.Mock;
beforeEach(() => {
getStuffSpy.mockClear();
gen = getThemEffect();
getStuffMock = jest.fn();
getStuffSpy.mockImplementation(getStuffMock);
});
describe("server success response", () => {
beforeEach(() => {
response = { data: { data: ["1", "2", "3"] } };
});
it("should create correct success flow", () => {
expect(gen.next()).toEqual({
value: put(requestAction()),
done: false
});
expect(gen.next()).toEqual({
value: call(api.getStuff),
done: false
});
expect(getStuffMock).toHaveBeenCalled(); // <=== this fails
expect(gen.next(response)).toEqual({
value: put(successAction(["1", "2", "3"])),
done: false
});
expect(gen.next()).toEqual({
value: undefined,
done: true
});
});
});
}
However the test that expects the getStuffMock function to have been called fails. How can I fix this? I am using jest with testing-library