Skip to content

Instantly share code, notes, and snippets.

@gwilczynski
Created January 21, 2020 09:11
Show Gist options
  • Save gwilczynski/9104356f1c440aaf100fc419898de598 to your computer and use it in GitHub Desktop.
Save gwilczynski/9104356f1c440aaf100fc419898de598 to your computer and use it in GitHub Desktop.
Expected number of calls: >= 1 Received number of calls: 0
import axios from "axios";
const getSomething = (url, onSucces, onError) => {
axios
.get(url)
.then(result => {
onSucces(result.data.results);
})
.catch(error => {
onError(error);
});
};
jest.mock("axios");
describe("getSomething", () => {
const url = "https://somewhere";
const onSucces = jest.fn();
const onError = jest.fn();
it("calls success", async () => {
axios.get.mockResolvedValue({ data: { results: ["bar"] } });
await getSomething(url, onSucces, onError);
// this works as expected
expect(onSucces).toHaveBeenCalledWith(["bar"]);
});
it("calls error", async () => {
axios.get.mockRejectedValue("Network Error");
await getSomething(url, onSucces, onError);
// this asset failed, onError.mock.call equals 0
expect(onError).toHaveBeenCalled();
expect(onError).toHaveBeenCalledWith("Network Error");
});
});
@gwilczynski
Copy link
Author

const getSomething = (url, onSucces, onError) => {
  return axios
    .get(url)
    .then(result => {
      onSucces(result.data.results);
    })
    .catch(error => {
      onError(error);
    });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment