From 75a56ad77bd7358a0059aff4ca04fe3ae1ef6f8d Mon Sep 17 00:00:00 2001 From: LiuLiu Date: Thu, 29 Feb 2024 12:44:41 -0800 Subject: [PATCH] test --- .../__tests__/useSetContainerHeight.test.ts | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 packages/hooks/src/__tests__/useSetContainerHeight.test.ts diff --git a/packages/hooks/src/__tests__/useSetContainerHeight.test.ts b/packages/hooks/src/__tests__/useSetContainerHeight.test.ts new file mode 100644 index 00000000..6c168275 --- /dev/null +++ b/packages/hooks/src/__tests__/useSetContainerHeight.test.ts @@ -0,0 +1,45 @@ +import { renderHook } from "@testing-library/react-hooks"; +import useSetContainerHeight from "../useSetContainerHeight"; +import { useReactiveHeight } from "../useReactiveSize"; + + +describe("useSetContainerHeight", () => { + const componentID = 'testComponent'; + + beforeAll(() => { + const mockElement = document.createElement('div'); + mockElement.id = componentID; + + mockElement.getBoundingClientRect = jest.fn().mockReturnValue({ top: 100 }); + + document.body.appendChild(mockElement); + + global.document.getElementById = jest.fn((id) => { + if (id === componentID) { + return mockElement; + } + return null; + }); + }); + + afterEach(() => { + jest.clearAllMocks(); + const mockElement = document.getElementById(componentID); + if (mockElement) { + document.body.removeChild(mockElement); + } + }); + + it("calculates container height correctly based on window height", () => { + const { result: windowHeightResult } = renderHook(() => + useReactiveHeight(), + ); + const windowHeight = windowHeightResult.current; + + const { result } = renderHook(() => useSetContainerHeight(componentID)); + + const expectedHeight = windowHeight - 100; + + expect(result.current.containerHeight).toBe(expectedHeight); + }); +});