Created
June 11, 2020 05:10
-
-
Save Dromediansk/e90971c6fe8f835a2ac687c14cbf83a4 to your computer and use it in GitHub Desktop.
Testing success of fetching data
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 { getAllCountries } from "./countries"; | |
const stubbedCountries = [ | |
{ | |
numericCode: 1, | |
name: "Slovakia", | |
capital: "Bratislava", | |
region: "Europe", | |
population: 500, | |
flag: "Slovakia flag", | |
}, | |
]; | |
describe("all countries request", () => { | |
it("should return status code 200 and a defined body as response", async () => { | |
// Mock API | |
jest.spyOn(global, "fetch").mockImplementation(() => | |
Promise.resolve({ | |
json: () => | |
Promise.resolve({ | |
status: 200, | |
data: stubbedCountries, | |
}), | |
}) | |
); | |
const result = await getAllCountries(); | |
expect(result.status).toBe(200); | |
expect(result.data).toBe(stubbedCountries); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment