-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #942 from mittwald/fix/MultipleOverlayControllerUsage
fix(OverlayController): fix overlayController handler management
- Loading branch information
Showing
3 changed files
with
208 additions
and
15 deletions.
There are no files selected for viewing
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
143 changes: 143 additions & 0 deletions
143
packages/components/src/lib/controller/overlay/useOverlayController.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,143 @@ | ||
import { renderHook, act } from "@testing-library/react"; | ||
import { useOverlayController } from "@/lib/controller"; | ||
import { useOverlayContext } from "@/lib/controller/overlay/context"; | ||
import { OverlayController } from "@/lib/controller"; | ||
import type { Mock } from "vitest"; | ||
import { vitest, describe, test, expect, beforeEach } from "vitest"; | ||
|
||
vitest.mock("@/lib/controller/overlay/context", () => ({ | ||
useOverlayContext: vitest.fn(), | ||
})); | ||
|
||
describe("useOverlayController", () => { | ||
let mockOnOpen: Mock; | ||
let mockOnClose: Mock; | ||
let contextController: OverlayController; | ||
|
||
beforeEach(() => { | ||
mockOnOpen = vitest.fn(); | ||
mockOnClose = vitest.fn(); | ||
contextController = new OverlayController(); | ||
|
||
(useOverlayContext as Mock).mockReturnValue({ | ||
Modal: contextController, | ||
}); | ||
}); | ||
|
||
test("should use controller from context when reuseControllerFromContext is true", () => { | ||
const { result } = renderHook(() => | ||
useOverlayController("Modal", { | ||
reuseControllerFromContext: true, | ||
onOpen: mockOnOpen, | ||
onClose: mockOnClose, | ||
}), | ||
); | ||
|
||
expect(result.current).toBe(contextController); | ||
}); | ||
|
||
test("should create new controller when reuseControllerFromContext is false", () => { | ||
const { result } = renderHook(() => | ||
useOverlayController("Modal", { | ||
reuseControllerFromContext: false, | ||
onOpen: mockOnOpen, | ||
onClose: mockOnClose, | ||
}), | ||
); | ||
|
||
expect(result.current).not.toBe(contextController); | ||
}); | ||
|
||
test("should add onOpen handler when controller is closed", () => { | ||
const { result } = renderHook(() => | ||
useOverlayController("Modal", { | ||
onOpen: mockOnOpen, | ||
}), | ||
); | ||
|
||
act(() => { | ||
result.current.open(); | ||
}); | ||
|
||
expect(mockOnOpen).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("should add onClose handler when controller is open", () => { | ||
contextController.open(); | ||
|
||
const { result } = renderHook(() => | ||
useOverlayController("Modal", { | ||
onClose: mockOnClose, | ||
}), | ||
); | ||
|
||
act(() => { | ||
result.current.close(); | ||
}); | ||
|
||
expect(mockOnClose).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("should cleanup handlers on unmount", () => { | ||
const { unmount } = renderHook(() => | ||
useOverlayController("Modal", { | ||
onOpen: mockOnOpen, | ||
onClose: mockOnClose, | ||
}), | ||
); | ||
|
||
unmount(); | ||
|
||
act(() => { | ||
contextController.open(); | ||
contextController.close(); | ||
}); | ||
|
||
expect(mockOnOpen).not.toHaveBeenCalled(); | ||
expect(mockOnClose).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("should update handlers when options change", () => { | ||
const newMockOnOpen = vitest.fn(); | ||
const { rerender } = renderHook( | ||
(props) => useOverlayController("Modal", props), | ||
{ | ||
initialProps: { onOpen: mockOnOpen }, | ||
}, | ||
); | ||
|
||
rerender({ onOpen: newMockOnOpen }); | ||
|
||
act(() => { | ||
contextController.open(); | ||
}); | ||
|
||
expect(mockOnOpen).not.toHaveBeenCalled(); | ||
expect(newMockOnOpen).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("should handle multiple handlers correctly", () => { | ||
const secondOnOpen = vitest.fn(); | ||
|
||
renderHook(() => useOverlayController("Modal", { onOpen: mockOnOpen })); | ||
renderHook(() => useOverlayController("Modal", { onOpen: secondOnOpen })); | ||
|
||
act(() => { | ||
contextController.open(); | ||
}); | ||
|
||
expect(mockOnOpen).toHaveBeenCalledTimes(1); | ||
expect(secondOnOpen).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("should handle undefined handlers gracefully", () => { | ||
const { result } = renderHook(() => useOverlayController("Modal", {})); | ||
|
||
expect(() => { | ||
act(() => { | ||
result.current.open(); | ||
result.current.close(); | ||
}); | ||
}).not.toThrow(); | ||
}); | ||
}); |
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