Skip to content

Instantly share code, notes, and snippets.

@Dromediansk
Created June 11, 2020 05:10
Show Gist options
  • Save Dromediansk/e90971c6fe8f835a2ac687c14cbf83a4 to your computer and use it in GitHub Desktop.
Save Dromediansk/e90971c6fe8f835a2ac687c14cbf83a4 to your computer and use it in GitHub Desktop.
Testing success of fetching data
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