diff --git a/src/test/scrollHandler/documentScrollParent.test.ts b/src/test/scrollHandler/documentScrollParent.test.ts new file mode 100644 index 00000000..42c82353 --- /dev/null +++ b/src/test/scrollHandler/documentScrollParent.test.ts @@ -0,0 +1,60 @@ +import DocumentScrollParent from "../../scrollHandler/documentScrollParent"; + +afterEach(() => { + jest.useRealTimers(); +}); + +describe("checkHorizontalScrolling", () => { + it("scrolls to the left when pageX is near the left edge", () => { + jest.useFakeTimers(); + const scrollBy = jest.fn(); + window.scrollBy = scrollBy; + + const refreshHitAreas = jest.fn(); + const treeElement = document.createElement("div"); + + const documentScrollParent = new DocumentScrollParent({ + refreshHitAreas, + treeElement, + }); + + documentScrollParent.checkHorizontalScrolling(10); + + expect(scrollBy).not.toHaveBeenCalled(); + jest.advanceTimersByTime(50); + expect(scrollBy).toHaveBeenCalledWith({ + behavior: "instant", + left: -20, + top: 0, + }); + }); + + it("stops scrolling when pageX is moved from the left edge", () => { + jest.useFakeTimers(); + const scrollBy = jest.fn(); + window.scrollBy = scrollBy; + + const refreshHitAreas = jest.fn(); + const treeElement = document.createElement("div"); + + const documentScrollParent = new DocumentScrollParent({ + refreshHitAreas, + treeElement, + }); + + documentScrollParent.checkHorizontalScrolling(10); + + expect(scrollBy).not.toHaveBeenCalled(); + jest.advanceTimersByTime(50); + expect(scrollBy).toHaveBeenCalledWith({ + behavior: "instant", + left: -20, + top: 0, + }); + + documentScrollParent.checkHorizontalScrolling(100); + jest.advanceTimersByTime(50); + + expect(scrollBy).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/test/support/testUtil.ts b/src/test/support/testUtil.ts index 7d17d4ca..7410ecf7 100644 --- a/src/test/support/testUtil.ts +++ b/src/test/support/testUtil.ts @@ -34,7 +34,7 @@ export const togglerLink = (liNode: HTMLElement | JQuery): JQuery => const nodeElement = (liNode: HTMLElement | JQuery): JQuery => singleChild(jQuery(liNode), "div.jqtree-element "); -export const mockLayout = (element: HTMLElement, rect: Rect) => { +const mockLayout = (element: HTMLElement, rect: Rect) => { jest.spyOn(element, "clientHeight", "get").mockReturnValue(rect.height); jest.spyOn(element, "clientWidth", "get").mockReturnValue(rect.width); jest.spyOn(element, "offsetParent", "get").mockReturnValue(