mardi 11 septembre 2018

testing action creator fails

When testing my action creator I get the following error:

expect(`jest.fn()`).toHaveBeenCalledWith(expected)
Expected mock function to have been called with:
  ["/baselist/list/12345"]
But it was not called.

My test function looks like this:

describe("fetchBases", () => {
    it("should return the bases created by the current user", () => {
        const bases = [];

        global.fetch = jest.fn().mockImplementation(() =>
            Promise.resolve({
                ok: true,
                json() {
                    return bases;
                }
            })
        );
        const dispatch = jest.fn();
        const creatorId = "12345";
        const access_token = "IUDUHADBABwbdpi";

        return fetchBasesByCreatorId(creatorId, access_token)(dispatch).then(() => {
            expect(fetch).toHaveBeenCalledWith(`/baselist/list/${creatorId}`);
            expect(dispatch).toHaveBeenCalledWith(
                fetchBasesByCreatorIdSuccess(bases)
            );
        });
    });
});

I am new to function testing, so I am not sure whether I even need to supply creatorId and access_token, but my actual action creator takes those two parameters. Not sure what other information I should provide to make the issue more clear. The actual function creator looks as follows:

export const fetchBasesByCreatorId = (creatorId, access_token) => dispatch => {
    dispatch(fetchBasesByCreatorIdRequest());
    return fetch(`${API_BASE_URL}/baselist/list/${creatorId}`, {
        method: "GET",
        headers: {
            authorization: `Bearer ${access_token}`
        }
    })
        .then(res => {
            if (!res.ok) {
                return Promise.reject(res.statusText);
            }
            return res.json();
        })
        .then(bases => dispatch(fetchBasesByCreatorIdSuccess(bases)))
        .catch(error => dispatch(fetchBasesByCreatorIdError(error)));
};

I am looking to pass a very shallow test, it does not have to go very in-depth.

Aucun commentaire:

Enregistrer un commentaire