Skip to content

Instantly share code, notes, and snippets.

@ivan-ha
Last active June 1, 2019 13:00
Show Gist options
  • Save ivan-ha/ec7ed5aa1c262cd0f7bce6f8a5471c30 to your computer and use it in GitHub Desktop.
Save ivan-ha/ec7ed5aa1c262cd0f7bce6f8a5471c30 to your computer and use it in GitHub Desktop.
react-hooks-test-custom-hooks
import { renderHook, act } from "react-hooks-testing-library";
jest.mock("axios");
describe("useRandomUsers", () => {
it("call API and return results", async () => {
axios.mockImplementation(() => Promise.resolve({ data: mockResponse }));
const { result, waitForNextUpdate } = renderHook(() => useRandomUsers());
expect(result.current).toStrictEqual([]);
await waitForNextUpdate();
expect(result.current).toStrictEqual(mockResponse.results);
expect(axios).toHaveBeenCalledWith(
"https://randomuser.me/api/?results=10&inc=name,login&nat=us"
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment