From 07a78d867ba8caeb20ba89f31374711ec0b2b4c8 Mon Sep 17 00:00:00 2001 From: Marco Braak Date: Sat, 7 Dec 2024 14:16:29 +0100 Subject: [PATCH] Extract mockLayout --- src/test/dragAndDropHandler/index.test.ts | 41 ++++------------------- src/test/support/testUtil.ts | 19 +++++++++++ 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/test/dragAndDropHandler/index.test.ts b/src/test/dragAndDropHandler/index.test.ts index 1308915a..3a0c2eac 100644 --- a/src/test/dragAndDropHandler/index.test.ts +++ b/src/test/dragAndDropHandler/index.test.ts @@ -1,9 +1,8 @@ -import { mockElementBoundingClientRect } from "jsdom-testing-mocks"; - import { DragAndDropHandler } from "../../dragAndDropHandler"; import { Node } from "../../node"; import NodeElement from "../../nodeElement"; import { Position } from "../../position"; +import { mockLayout } from "../support/testUtil"; describe(".refresh", () => { it("generates hit areas if there is a current item", () => { @@ -16,41 +15,15 @@ describe(".refresh", () => { const elementForTree = document.createElement("div"); document.body.append(elementForTree); - jest.spyOn(elementForTree, "clientHeight", "get").mockReturnValue(50); - jest.spyOn(elementForTree, "clientWidth", "get").mockReturnValue(40); + mockLayout(elementForTree, { height: 40, width: 50, x: 0, y: 0 }); const elementForNode1 = document.createElement("div"); elementForTree.append(elementForNode1); const elementForNode2 = document.createElement("div"); elementForTree.append(elementForNode2); - jest.spyOn(elementForNode1, "offsetParent", "get").mockReturnValue( - elementForTree, - ); - jest.spyOn(elementForNode2, "offsetParent", "get").mockReturnValue( - elementForTree, - ); - - mockElementBoundingClientRect(elementForTree, { - height: 40, - width: 50, - x: 0, - y: 0, - }); - - mockElementBoundingClientRect(elementForNode1, { - height: 20, - width: 50, - x: 0, - y: 0, - }); - - mockElementBoundingClientRect(elementForNode2, { - height: 20, - width: 50, - x: 0, - y: 20, - }); + mockLayout(elementForNode1, { height: 20, width: 50, x: 0, y: 0 }); + mockLayout(elementForNode2, { height: 20, width: 50, x: 0, y: 20 }); const tree = new Node({ isRoot: true }); @@ -115,16 +88,16 @@ describe(".refresh", () => { expect(dragAndDropHandler.hitAreas).toMatchObject([ expect.objectContaining({ - bottom: 43, + bottom: 38, node: node2, position: Position.Inside, top: 20, }), expect.objectContaining({ - bottom: 66, + bottom: 56, node: node2, position: Position.After, - top: 43, + top: 38, }), ]); }); diff --git a/src/test/support/testUtil.ts b/src/test/support/testUtil.ts index 6f660195..9440b49e 100644 --- a/src/test/support/testUtil.ts +++ b/src/test/support/testUtil.ts @@ -1,3 +1,12 @@ +import { mockElementBoundingClientRect } from "jsdom-testing-mocks"; + +interface Rect { + height: number; + width: number; + x: number; + y: number; +} + export const singleChild = ($el: JQuery, selector: string): JQuery => { const $result = $el.children(selector); @@ -22,3 +31,13 @@ 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) => { + jest.spyOn(element, "clientHeight", "get").mockReturnValue(rect.height); + jest.spyOn(element, "clientWidth", "get").mockReturnValue(rect.width); + jest.spyOn(element, "offsetParent", "get").mockReturnValue( + element.parentElement, + ); + + mockElementBoundingClientRect(element, rect); +};