Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu-dev committed Feb 29, 2024
1 parent 8aed197 commit 75a56ad
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/hooks/src/__tests__/useSetContainerHeight.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 75a56ad

Please sign in to comment.