Skip to content

Commit

Permalink
Assert refetch() called expected number of times.
Browse files Browse the repository at this point in the history
  • Loading branch information
jg210 committed Mar 9, 2024
1 parent 056b722 commit bdd4f56
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/__tests__/useRefresh.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@ import { useRefresh } from "../useRefresh";

describe("useRefresh", () => {
it("refetch resolves immediately", async () => {
const refetch = async () => {};
const refetch = jest.fn(async () => {});
const { result } = renderHook(() => useRefresh(refetch));
expect(refetch).toHaveBeenCalledTimes(0);
expect(result.current.refreshing).toEqual(false);
await act(async () => {
await result.current.onRefresh();
expect(result.current.refreshing).toEqual(true);
});
waitFor(() => expect(result.current.refreshing).toEqual(false));
expect(refetch).toHaveBeenCalledTimes(1);
});

it("refetch resolves after 100ms", async () => {
const refetch = () => {
const refetch = jest.fn(() => {
return new Promise<void>((resolve) => setTimeout(() => resolve(), 100));
};
});
const { result } = renderHook(() => useRefresh(refetch));
expect(refetch).toHaveBeenCalledTimes(0);
expect(result.current.refreshing).toEqual(false);
await act(async () => {
await result.current.onRefresh();
expect(result.current.refreshing).toEqual(true);
});
waitFor(() => expect(result.current.refreshing).toEqual(false));
expect(refetch).toHaveBeenCalledTimes(1);
});

// it("multiple onRefresh calls before refetch resolves", async () => {
Expand Down

0 comments on commit bdd4f56

Please sign in to comment.