-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* do not throw error when status is 204 * Fix bug in ITwin favorites functionality * Remove 204 status check in ITwin favorites fetch logic * Handle AbortError in ITwin favorites fetching logic * Add label to favorites button in ITwinTile component * Add favorites functionality to ITwinGrid table view * Fix bug in ITwin favorites and add favorites to ITwinGrid table view * Add unit tests for useITwinFavorites hook
- Loading branch information
Showing
7 changed files
with
227 additions
and
38 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/@itwin/imodel-browser-react/fix-itwin-favorites_2024-11-22-02-01.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@itwin/imodel-browser-react", | ||
"comment": "Fix bug in itwin favorites and add favorites to iTwinGrid table view", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@itwin/imodel-browser-react" | ||
} |
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
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
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
80 changes: 80 additions & 0 deletions
80
packages/modules/imodel-browser/src/containers/ITwinGrid/useITwinFavorites.test.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { act } from "react"; | ||
|
||
import { useITwinFavorites } from "./useITwinFavorites"; | ||
|
||
export function mockFetch(data: any, status = 200) { | ||
return jest.fn().mockImplementationOnce(async () => | ||
Promise.resolve({ | ||
status, | ||
ok: true, | ||
json: () => data, | ||
}) | ||
); | ||
} | ||
|
||
const accessToken = "test-access-token"; | ||
|
||
describe("useITwinFavorites", () => { | ||
// Clear mocks before each test | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should initialize with empty Set", () => { | ||
const { result } = renderHook(() => useITwinFavorites(accessToken)); | ||
window.fetch = mockFetch({ iTwins: [] }); | ||
|
||
expect(result.current.iTwinFavorites).toBeInstanceOf(Set); | ||
expect(result.current.iTwinFavorites.size).toBe(0); | ||
}); | ||
|
||
test("should fetch favorites on mount", async () => { | ||
const mockFavorites = [ | ||
{ id: "1", name: "iTwin1" }, | ||
{ id: "2", name: "iTwin2" }, | ||
]; | ||
|
||
window.fetch = mockFetch({ iTwins: mockFavorites }); | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useITwinFavorites(accessToken) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
expect(result.current.iTwinFavorites.has("1")).toBe(true); | ||
expect(result.current.iTwinFavorites.has("2")).toBe(true); | ||
expect(result.current.iTwinFavorites.size).toBe(2); | ||
}); | ||
|
||
test("should handle empty response from API", async () => { | ||
window.fetch = mockFetch({ iTwins: [] }); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useITwinFavorites(accessToken) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
expect(result.current.iTwinFavorites.size).toBe(0); | ||
}); | ||
|
||
test("should add and remove an iTwin from favorites", async () => { | ||
const { result } = renderHook(() => useITwinFavorites(accessToken)); | ||
const iTwinId = "test-itwin-id"; | ||
await act(async () => { | ||
window.fetch = mockFetch({}); | ||
await result.current.addITwinToFavorites(iTwinId); | ||
}); | ||
expect(result.current.iTwinFavorites.has(iTwinId)).toBe(true); | ||
|
||
await act(async () => { | ||
window.fetch = mockFetch({}); | ||
await result.current.removeITwinFromFavorites(iTwinId); | ||
}); | ||
expect(result.current.iTwinFavorites.has(iTwinId)).toBe(false); | ||
}); | ||
}); |
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
Oops, something went wrong.