Skip to content

Instantly share code, notes, and snippets.

@Dromediansk
Last active June 13, 2020 13:53
Show Gist options
  • Save Dromediansk/967f068c0f7824be2ad8b00ab4533f93 to your computer and use it in GitHub Desktop.
Save Dromediansk/967f068c0f7824be2ad8b00ab4533f93 to your computer and use it in GitHub Desktop.
Getting data test in useFetch.test.js
it("should return data after fetch", async () => {
// Mock API
jest.spyOn(global, "fetch").mockImplementation(() =>
Promise.resolve({
json: () => Promise.resolve(stubbedCountries),
})
);
// Execute
const { result, waitForNextUpdate } = renderHook(() =>
useFetch(stubbedFetchUrl, { current: true }, [])
);
await waitForNextUpdate();
// Assert
expect(result.current).toStrictEqual({
loading: false,
data: stubbedCountries,
error: null,
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment