Skip to content

Commit

Permalink
Refactor useITwinFavorites tests to use act for state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
arome committed Nov 22, 2024
1 parent d9e7e02 commit c909c2f
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { renderHook } from "@testing-library/react-hooks";
import { act } from "react";

import { useITwinFavorites } from "./useITwinFavorites";

Expand Down Expand Up @@ -62,18 +63,18 @@ describe("useITwinFavorites", () => {
});

test("should add and remove an iTwin from favorites", async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useITwinFavorites(accessToken)
);
const { result } = renderHook(() => useITwinFavorites(accessToken));
const iTwinId = "test-itwin-id";
window.fetch = mockFetch({});

await result.current.addITwinToFavorites(iTwinId);
await waitForNextUpdate();
await act(async () => {
window.fetch = mockFetch({});
await result.current.addITwinToFavorites(iTwinId);
});
expect(result.current.iTwinFavorites.has(iTwinId)).toBe(true);

await result.current.removeITwinFromFavorites(iTwinId);
await waitForNextUpdate();
await act(async () => {
window.fetch = mockFetch({});
await result.current.removeITwinFromFavorites(iTwinId);
});
expect(result.current.iTwinFavorites.has(iTwinId)).toBe(false);
});
});

0 comments on commit c909c2f

Please sign in to comment.