|
| 1 | +import { renderHook, act } from "@testing-library/react"; |
| 2 | +import { createRef, UIEvent } from "react"; |
| 3 | + |
| 4 | +jest.mock("@mendix/widget-plugin-hooks/useOnScreen", () => ({ |
| 5 | + useOnScreen: () => true |
| 6 | +})); |
| 7 | + |
| 8 | +const mockBumpPage = jest.fn(); |
| 9 | +const mockLockGridContainerHeight = jest.fn(); |
| 10 | +const mockGridContainerRef = createRef<HTMLDivElement>(); |
| 11 | + |
| 12 | +jest.mock("../injection-hooks", () => ({ |
| 13 | + useGridSizeStore: () => ({ |
| 14 | + hasVirtualScrolling: true, |
| 15 | + gridContainerRef: mockGridContainerRef, |
| 16 | + bumpPage: mockBumpPage, |
| 17 | + lockGridContainerHeight: mockLockGridContainerHeight |
| 18 | + }) |
| 19 | +})); |
| 20 | + |
| 21 | +import { useInfiniteControl } from "../useInfiniteControl"; |
| 22 | +import { VIRTUAL_SCROLLING_OFFSET } from "../../stores/GridSize.store"; |
| 23 | + |
| 24 | +function makeScrollEvent(overrides: { |
| 25 | + scrollTop: number; |
| 26 | + scrollHeight: number; |
| 27 | + clientHeight: number; |
| 28 | + scrollLeft?: number; |
| 29 | +}): UIEvent<HTMLDivElement> { |
| 30 | + const { scrollTop, scrollHeight, clientHeight, scrollLeft = 0 } = overrides; |
| 31 | + return { |
| 32 | + target: { scrollTop, scrollHeight, clientHeight, scrollLeft } |
| 33 | + } as unknown as UIEvent<HTMLDivElement>; |
| 34 | +} |
| 35 | + |
| 36 | +describe("useInfiniteControl — trackTableScrolling", () => { |
| 37 | + beforeEach(() => { |
| 38 | + jest.useFakeTimers(); |
| 39 | + mockBumpPage.mockClear(); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + jest.useRealTimers(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("does not call bumpPage when scrollTop is 0, even if scroll position satisfies bottom condition", () => { |
| 47 | + const { result } = renderHook(() => useInfiniteControl()); |
| 48 | + const [trackTableScrolling] = result.current; |
| 49 | + |
| 50 | + // scrollTop === 0 → guard blocks bumpPage regardless of height math |
| 51 | + const e = makeScrollEvent({ |
| 52 | + scrollTop: 0, |
| 53 | + scrollHeight: 500, |
| 54 | + clientHeight: 500 |
| 55 | + }); |
| 56 | + |
| 57 | + act(() => { |
| 58 | + trackTableScrolling!(e); |
| 59 | + }); |
| 60 | + |
| 61 | + expect(mockBumpPage).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("calls bumpPage when scrollTop > 0 and scroll position reaches the bottom", () => { |
| 65 | + const { result } = renderHook(() => useInfiniteControl()); |
| 66 | + const [trackTableScrolling] = result.current; |
| 67 | + |
| 68 | + // scrollTop > 0 and Math.floor(scrollHeight - OFFSET - scrollTop) <= clientHeight + 2 |
| 69 | + const scrollHeight = 500; |
| 70 | + const clientHeight = 400; |
| 71 | + const scrollTop = scrollHeight - VIRTUAL_SCROLLING_OFFSET - clientHeight; // exactly at bottom |
| 72 | + |
| 73 | + const e = makeScrollEvent({ scrollTop, scrollHeight, clientHeight }); |
| 74 | + |
| 75 | + act(() => { |
| 76 | + trackTableScrolling!(e); |
| 77 | + }); |
| 78 | + |
| 79 | + expect(mockBumpPage).toHaveBeenCalledTimes(1); |
| 80 | + }); |
| 81 | + |
| 82 | + it("does not call bumpPage when scrolled but not yet at the bottom", () => { |
| 83 | + const { result } = renderHook(() => useInfiniteControl()); |
| 84 | + const [trackTableScrolling] = result.current; |
| 85 | + |
| 86 | + const e = makeScrollEvent({ |
| 87 | + scrollTop: 10, |
| 88 | + scrollHeight: 500, |
| 89 | + clientHeight: 100 |
| 90 | + }); |
| 91 | + |
| 92 | + act(() => { |
| 93 | + trackTableScrolling!(e); |
| 94 | + }); |
| 95 | + |
| 96 | + expect(mockBumpPage).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments