UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

378 lines (377 loc) • 12.8 kB
/** * DevExtreme (cjs/__internal/ui/resizable/utils.test.js) * Version: 24.2.6 * Build date: Mon Mar 17 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; var _globals = require("@jest/globals"); var _utils = require("./utils"); _globals.jest.mock("@js/core/utils/size", (() => ({ getOuterWidth: _ref => { let { outerWidth: outerWidth } = _ref; return outerWidth }, getInnerWidth: _ref2 => { let { innerWidth: innerWidth } = _ref2; return innerWidth }, getOuterHeight: _ref3 => { let { outerHeight: outerHeight } = _ref3; return outerHeight }, getInnerHeight: _ref4 => { let { innerHeight: innerHeight } = _ref4; return innerHeight }, getOffset: _ref5 => { let { top: top, left: left } = _ref5; return { left: left, top: top } } }))); const window = { innerWidth: 101, innerHeight: 102, pageXOffset: 90, pageYOffset: 101 }; window.window = window; (0, _globals.describe)("Utils", (() => { (0, _globals.describe)("getMovingSides", (() => { (0, _globals.it)("should detect top side", (() => { let el = { className: "dx-resizable-handle-corner-top-left" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ top: true }); el = { className: "dx-resizable-handle-top" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ top: true }); el = { className: "dx-resizable-handle-corner-top-right" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ top: true }) })); (0, _globals.it)("should detect bottom side", (() => { let el = { className: "dx-resizable-handle-bottom" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ bottom: true }); el = { className: "dx-resizable-handle-corner-bottom-left" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ bottom: true }); el = { className: "dx-resizable-handle-corner-bottom-right" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ bottom: true }) })); (0, _globals.it)("should detect right side", (() => { let el = { className: "dx-resizable-handle-right" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ right: true }); el = { className: "dx-resizable-handle-corner-top-right" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ right: true }); el = { className: "dx-resizable-handle-corner-bottom-right" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ right: true }) })); (0, _globals.it)("should detect left side", (() => { let el = { className: "dx-resizable-handle-left" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ left: true }); el = { className: "dx-resizable-handle-corner-top-left" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ left: true }); el = { className: "dx-resizable-handle-corner-bottom-left" }; (0, _globals.expect)((0, _utils.getMovingSides)(el)).toMatchObject({ left: true }) })) })); (0, _globals.describe)("Get area", (() => { const emptyEl = { outerWidth: 0, outerHeight: 0, innerWidth: 0, innerHeight: 0, style: { borderLeftWidth: "0", borderTopWidth: "0" } }; (0, _globals.describe)("getAreaFromElement", (() => { (0, _globals.it)("should exclude resizable element's border width", (() => { (0, _globals.expect)((0, _utils.getAreaFromElement)({ innerWidth: 101, innerHeight: 102, left: 10, top: 50, style: { borderLeftWidth: "0", borderTopWidth: "0" } }, { innerWidth: 30, innerHeight: 40, outerWidth: 32, outerHeight: 44, style: { borderLeftWidth: "5", borderTopWidth: "6" } })).toEqual({ width: 39, height: 18, offset: { left: 15, top: 56 } }) })); (0, _globals.it)("should include area element's left/top border width", (() => { (0, _globals.expect)((0, _utils.getAreaFromElement)({ innerWidth: 101, innerHeight: 102, left: 10, top: 50, style: { borderLeftWidth: "5", borderTopWidth: "6" } }, emptyEl)).toEqual({ width: 101, height: 102, offset: { left: 15, top: 56 } }) })); (0, _globals.it)("should ignore offset if element is window", (() => { (0, _globals.expect)((0, _utils.getAreaFromElement)(window, emptyEl)).toEqual({ width: 101, height: 102, offset: { left: 0, top: 0 } }) })); (0, _globals.it)("should get area from element innerWidth/innerHeight/offset ", (() => { (0, _globals.expect)((0, _utils.getAreaFromElement)({ innerWidth: 101, innerHeight: 102, top: 22, left: 33, style: { borderLeftWidth: "0", borderTopWidth: "0" } }, emptyEl)).toEqual({ width: 101, height: 102, offset: { left: 33, top: 22 } }) })) })); (0, _globals.describe)("getAreaFromObject", (() => { (0, _globals.it)("should exclude resizable element's border width", (() => { (0, _globals.expect)((0, _utils.getAreaFromObject)({ left: 30, top: 40, right: 80, bottom: 70 }, { innerWidth: 10, innerHeight: 12, outerWidth: 5, outerHeight: 10, style: { borderLeftWidth: "2", borderTopWidth: "3" } })).toEqual({ width: 35, height: 8, offset: { left: 32, top: 43 } }) })); (0, _globals.it)("should build area from the object", (() => { (0, _globals.expect)((0, _utils.getAreaFromObject)({ left: 3, top: 4, right: 5, bottom: 7 }, emptyEl)).toEqual({ width: 2, height: 3, offset: { left: 3, top: 4 } }) })) })) })); (0, _globals.describe)("getDragOffsets", (() => { (0, _globals.it)("should calculate drag limitations by area size and handle element size", (() => { (0, _globals.expect)((0, _utils.getDragOffsets)({ width: 10, height: 20, offset: { left: 0, top: 0 } }, { outerWidth: 4, outerHeight: 6, left: 30, top: 30 }, {})).toEqual({ maxLeftOffset: 30, maxRightOffset: -24, maxTopOffset: 30, maxBottomOffset: -16 }) })); (0, _globals.it)("should take into account area's offset", (() => { (0, _globals.expect)((0, _utils.getDragOffsets)({ width: 10, height: 20, offset: { left: 5, top: 6 } }, { outerWidth: 4, outerHeight: 6, left: 30, top: 30 }, {})).toEqual({ maxLeftOffset: 25, maxRightOffset: -19, maxTopOffset: 24, maxBottomOffset: -10 }) })); (0, _globals.it)("should take into account window's page offset", (() => { (0, _globals.expect)((0, _utils.getDragOffsets)({ width: 10, height: 20, offset: { left: 0, top: 0 } }, { outerWidth: 4, outerHeight: 6, left: 30, top: 30 }, window)).toEqual({ maxBottomOffset: 85, maxLeftOffset: -60, maxRightOffset: 66, maxTopOffset: -71 }) })) })); (0, _globals.describe)("filterOffsets", (() => { (0, _globals.it)("should exclude horizontal offset if top/bottom handle used", (() => { (0, _globals.expect)((0, _utils.filterOffsets)({ x: 100, y: 200 }, { className: "dx-resizable-handle-right" })).toEqual({ x: 100, y: 0 }) })); (0, _globals.it)("should exclude vertical offset if left/right handle used", (() => { (0, _globals.expect)((0, _utils.filterOffsets)({ x: 100, y: 200 }, { className: "dx-resizable-handle-bottom" })).toEqual({ x: 0, y: 200 }) })); (0, _globals.it)("should include all offsets for corner handle", (() => { (0, _globals.expect)((0, _utils.filterOffsets)({ x: 100, y: 200 }, { className: "dx-resizable-handle-corner-top-right" })).toEqual({ x: 100, y: 200 }) })); (0, _globals.it)("should return zero if handle in not used", (() => { (0, _globals.expect)((0, _utils.filterOffsets)({ x: 100, y: 100 }, { className: "" })).toEqual({ x: 0, y: 0 }) })) })) }));