Last active
March 19, 2019 17:13
-
-
Save antoinejaussoin/33388068332d9854143af6756f511260 to your computer and use it in GitHub Desktop.
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 { renderHook, cleanup, act } from "react-hooks-testing-library"; | |
import useLoadActivity from "../useLoadActivity"; | |
import fetchFunctions from "../fetchActivity"; | |
import mockFetchFunction from "../__mocks__/fetchActivity"; | |
describe("Testing a custom hook (without Jooks)", () => { | |
it("should load activities properly", async () => { | |
const { result } = renderHook(() => useLoadActivity()); | |
const { activity, next } = result.current; | |
// Ok so that's ok, it's indeed null at first | |
expect(activity).toBeNull(); | |
// Then I'm going to force fetching the (mock) data | |
act(() => { | |
// This is not asynchronous!! | |
// There's no way to "wait" for that async function inside to complete | |
next(); | |
}); | |
await wait(100); | |
// It should have been calling the effect at this point, but nothing... | |
expect(activity).not.toBeNull(); | |
expect(activity.activity).toBe("Foo"); | |
}); | |
beforeEach(() => { | |
fetchMock.mockImplementation(mockFetchFunction.fetchActivity); | |
}); | |
afterEach(cleanup); | |
}); | |
const fetchMock = jest.spyOn(fetchFunctions, "fetchActivity"); | |
const wait = async wait => | |
new Promise(resolve => setTimeout(resolve, wait || 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment