import { elementNotVisible } from "./elementNotVisible"; describe("elementNotVisible", () => { let getBoundingClientRect: jest.Mock; let originalScrollHeight: number; let originalScrollWidth: number; beforeAll(() => { originalScrollHeight = document.documentElement.scrollHeight; originalScrollWidth = document.documentElement.scrollWidth; Object.defineProperty(document.documentElement, "scrollHeight", { value: 1000, writable: true, }); Object.defineProperty(document.documentElement, "scrollWidth", { value: 1000, writable: true, }); getBoundingClientRect = jest.fn(); document.querySelector = jest.fn(); }); afterAll(() => { Object.defineProperty(document.documentElement, "scrollHeight", { value: originalScrollHeight, writable: true, }); Object.defineProperty(document.documentElement, "scrollWidth", { value: originalScrollWidth, writable: true, }); jest.clearAllMocks(); }); it("should return true if the container is not found", () => { (document.querySelector as jest.Mock).mockReturnValue(null); expect(elementNotVisible(".nonexistent")).toBe(true); }); it("should return true if the container is out of view", () => { (document.querySelector as jest.Mock).mockReturnValue({ getBoundingClientRect, }); getBoundingClientRect.mockReturnValue({ bottom: -1, left: 0, right: 0, top: 0, }); expect(elementNotVisible(".out-of-view")).toBe(true); getBoundingClientRect.mockReturnValue({ bottom: 0, left: 1001, right: 1002, top: 0, }); expect(elementNotVisible(".out-of-view")).toBe(true); }); it("should return false if the container is within view", () => { (document.querySelector as jest.Mock).mockReturnValue({ getBoundingClientRect, }); getBoundingClientRect.mockReturnValue({ bottom: 500, left: 100, right: 200, top: 100, }); expect(elementNotVisible(".in-view")).toBe(false); }); });