Skip to content

Commit

Permalink
Test documentScrollParent
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Feb 3, 2025
1 parent 7c6159f commit 7d4167e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/test/scrollHandler/documentScrollParent.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion src/test/support/testUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7d4167e

Please sign in to comment.