Last active
June 27, 2019 18:51
-
-
Save andrecalvo/bb7920a4421ffba9797ade4d2b51dcd0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 React from "react"; | |
import { useDataApi } from "path/to/hoo/useDataApi.jsx"; | |
import "whatwg-fetch"; | |
import { renderHook } from "@testing-library/react-hooks"; | |
import fetchMock from "fetch-mock"; | |
import { act } from "react-test-renderer"; | |
describe("useDataApi", () => { | |
beforeAll(() => { | |
global.fetch = fetch; | |
}); | |
afterAll(() => { | |
fetchMock.restore(); | |
}); | |
it("should return data with a successful api request", async () => { | |
const { result } = renderHook(() => useDataApi()); | |
// Tell the test that any request to 'test.com' should return 'returnedData: "foo"' | |
fetchMock.mock("test.com", { | |
returnedData: "foo" | |
}); | |
// Calling the api via the `callApi` function | |
await act(async () => { | |
result.current.callApi("test.com"); | |
}); | |
// Check the 'data' state variable has the same mocked data from earlier | |
expect(result.current.data).toBe({ | |
returnedData: "foo" | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment