Created
January 21, 2020 09:11
-
-
Save gwilczynski/9104356f1c440aaf100fc419898de598 to your computer and use it in GitHub Desktop.
Expected number of calls: >= 1 Received number of calls: 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}); | |
}); |
Author
gwilczynski
commented
Jan 21, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment