| | |
| | |
| |
|
| | import { |
| | bottom, |
| | collides, |
| | compact, |
| | fastRGLPropsEqual, |
| | moveElement, |
| | sortLayoutItemsByRowCol, |
| | validateLayout, |
| | compactType, |
| | synchronizeLayoutWithChildren |
| | } from "../../lib/utils"; |
| | import * as React from "react"; |
| | import { |
| | calcGridColWidth, |
| | calcGridItemPosition, |
| | calcWH, |
| | calcXY |
| | } from "../../lib/calculateUtils"; |
| | import { deepEqual } from "fast-equals"; |
| | import deepFreeze from "./../util/deepFreeze"; |
| |
|
| | describe("bottom", () => { |
| | it("Handles an empty layout as input", () => { |
| | expect(bottom([])).toEqual(0); |
| | }); |
| |
|
| | it("Returns the bottom coordinate of the layout", () => { |
| | expect( |
| | bottom([ |
| | { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| | { i: "2", x: 1, y: 2, w: 1, h: 1 } |
| | ]) |
| | ).toEqual(3); |
| | }); |
| | }); |
| |
|
| | describe("sortLayoutItemsByRowCol", () => { |
| | it("should sort by top to bottom right", () => { |
| | const layout = [ |
| | { x: 1, y: 1, w: 1, h: 1, i: "2" }, |
| | { x: 1, y: 0, w: 1, h: 1, i: "1" }, |
| | { x: 0, y: 1, w: 2, h: 2, i: "3" } |
| | ]; |
| | expect(sortLayoutItemsByRowCol(layout)).toEqual([ |
| | { x: 1, y: 0, w: 1, h: 1, i: "1" }, |
| | { x: 0, y: 1, w: 2, h: 2, i: "3" }, |
| | { x: 1, y: 1, w: 1, h: 1, i: "2" } |
| | ]); |
| | }); |
| | }); |
| |
|
| | describe("collides", () => { |
| | it("Returns whether the layout items collide", () => { |
| | expect( |
| | collides( |
| | { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| | { i: "2", x: 1, y: 2, w: 1, h: 1 } |
| | ) |
| | ).toEqual(false); |
| | expect( |
| | collides( |
| | { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| | { i: "2", x: 0, y: 1, w: 1, h: 1 } |
| | ) |
| | ).toEqual(true); |
| | }); |
| | }); |
| |
|
| | describe("validateLayout", () => { |
| | it("Validates an empty layout", () => { |
| | validateLayout([]); |
| | }); |
| | it("Validates a populated layout", () => { |
| | validateLayout([ |
| | { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| | { i: "2", x: 1, y: 2, w: 1, h: 1 } |
| | ]); |
| | }); |
| | it("Throws errors on h not as a number", () => { |
| | expect(() => { |
| | validateLayout([ |
| | { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| | |
| | { i: "2", x: 1, y: 2, w: 1 } |
| | ]); |
| | }).toThrowError(/layout\[1]\.h must be a number!/i); |
| | }); |
| | }); |
| |
|
| | describe("moveElement", () => { |
| | function compactAndMove( |
| | layout, |
| | layoutItem, |
| | x, |
| | y, |
| | isUserAction, |
| | preventCollision, |
| | compactType, |
| | cols |
| | ) { |
| | return compact( |
| | moveElement( |
| | layout, |
| | layoutItem, |
| | x, |
| | y, |
| | isUserAction, |
| | preventCollision, |
| | compactType, |
| | cols |
| | ), |
| | compactType, |
| | cols |
| | ); |
| | } |
| |
|
| | it("Does not change layout when colliding on no rearrangement mode", () => { |
| | const layout = [ |
| | { i: "1", x: 0, y: 1, w: 1, h: 1, moved: false }, |
| | { i: "2", x: 1, y: 2, w: 1, h: 1, moved: false } |
| | ]; |
| | const layoutItem = layout[0]; |
| | expect( |
| | moveElement( |
| | layout, |
| | layoutItem, |
| | 1, |
| | 2, |
| | true, |
| | true, |
| | null, |
| | 2 |
| | ) |
| | ).toEqual([ |
| | { i: "1", x: 0, y: 1, w: 1, h: 1, moved: false }, |
| | { i: "2", x: 1, y: 2, w: 1, h: 1, moved: false } |
| | ]); |
| | }); |
| |
|
| | it("Does change layout when colliding in rearrangement mode", () => { |
| | const layout = [ |
| | { i: "1", x: 0, y: 0, w: 1, h: 1, moved: false }, |
| | { i: "2", x: 1, y: 0, w: 1, h: 1, moved: false } |
| | ]; |
| | const layoutItem = layout[0]; |
| | expect( |
| | moveElement( |
| | layout, |
| | layoutItem, |
| | 1, |
| | 0, |
| | true, |
| | false, |
| | "vertical", |
| | 2 |
| | ) |
| | ).toEqual([ |
| | { i: "1", x: 1, y: 0, w: 1, h: 1, moved: true }, |
| | { i: "2", x: 1, y: 1, w: 1, h: 1, moved: true } |
| | ]); |
| | }); |
| |
|
| | it("Moves elements out of the way without causing panel jumps when compaction is vertical", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 1, h: 10, i: "A" }, |
| | { x: 0, y: 10, w: 1, h: 1, i: "B" }, |
| | { x: 0, y: 11, w: 1, h: 1, i: "C" } |
| | ]; |
| | |
| | |
| | const itemA = layout[0]; |
| | expect( |
| | compactAndMove( |
| | layout, |
| | itemA, |
| | 0, |
| | 1, |
| | true, |
| | false, |
| | "vertical", |
| | 10 |
| | ) |
| | ).toEqual([ |
| | expect.objectContaining({ x: 0, y: 1, w: 1, h: 10, i: "A" }), |
| | expect.objectContaining({ x: 0, y: 0, w: 1, h: 1, i: "B" }), |
| | expect.objectContaining({ x: 0, y: 11, w: 1, h: 1, i: "C" }) |
| | ]); |
| | }); |
| |
|
| | it("Calculates the correct collision when moving large object far", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 1, h: 10, i: "A" }, |
| | { x: 0, y: 10, w: 1, h: 1, i: "B" }, |
| | { x: 0, y: 11, w: 1, h: 1, i: "C" } |
| | ]; |
| | |
| | |
| | const itemA = layout[0]; |
| | expect( |
| | moveElement( |
| | layout, |
| | itemA, |
| | 0, |
| | 2, |
| | true, |
| | false, |
| | "vertical", |
| | 10 |
| | ) |
| | ).toEqual([ |
| | expect.objectContaining({ x: 0, y: 2, w: 1, h: 10, i: "A" }), |
| | expect.objectContaining({ x: 0, y: 1, w: 1, h: 1, i: "B" }), |
| | expect.objectContaining({ x: 0, y: 12, w: 1, h: 1, i: "C" }) |
| | ]); |
| | }); |
| |
|
| | it("Moves elements out of the way without causing panel jumps when compaction is vertical (example case 13)", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 1, h: 1, i: "A" }, |
| | { x: 1, y: 0, w: 1, h: 1, i: "B" }, |
| | { x: 0, y: 1, w: 2, h: 2, i: "C" } |
| | ]; |
| | |
| | |
| | const itemA = layout[0]; |
| | expect( |
| | moveElement( |
| | layout, |
| | itemA, |
| | 1, |
| | 0, |
| | true, |
| | false, |
| | "vertical", |
| | 2 |
| | ) |
| | ).toEqual([ |
| | { x: 1, y: 0, w: 1, h: 1, i: "A", moved: true }, |
| | { x: 1, y: 1, w: 1, h: 1, i: "B", moved: true }, |
| | { x: 0, y: 2, w: 2, h: 2, i: "C", moved: true } |
| | ]); |
| | }); |
| |
|
| | it("Moves elements out of the way without causing panel jumps when compaction is horizontal", () => { |
| | const layout = [ |
| | { y: 0, x: 0, h: 1, w: 10, i: "A" }, |
| | { y: 0, x: 11, h: 1, w: 1, i: "B" }, |
| | { y: 0, x: 12, h: 1, w: 1, i: "C" } |
| | ]; |
| | |
| | |
| | const itemA = layout[0]; |
| | expect( |
| | moveElement( |
| | layout, |
| | itemA, |
| | 2, |
| | 0, |
| | true, |
| | false, |
| | "horizontal", |
| | 10 |
| | ) |
| | ).toEqual([ |
| | { y: 0, x: 2, h: 1, w: 10, moved: true, i: "A" }, |
| | { y: 0, x: 1, h: 1, w: 1, moved: true, i: "B" }, |
| | { y: 0, x: 12, h: 1, w: 1, i: "C" } |
| | ]); |
| | }); |
| |
|
| | it("Moves one element to another should cause moving down panels, vert compact, example 1", () => { |
| | |
| | |
| | const layout = [ |
| | { x: 0, y: 0, w: 2, h: 1, i: "A" }, |
| | { x: 2, y: 0, w: 2, h: 1, i: "B" }, |
| | { x: 0, y: 1, w: 1, h: 1, i: "C" }, |
| | { x: 1, y: 1, w: 3, h: 1, i: "D" } |
| | ]; |
| | |
| | |
| | const itemB = layout[1]; |
| | expect( |
| | compactAndMove( |
| | layout, |
| | itemB, |
| | 1, |
| | 0, |
| | true, |
| | false, |
| | "vertical", |
| | 4 |
| | ) |
| | ).toEqual([ |
| | expect.objectContaining({ x: 0, y: 1, w: 2, h: 1, i: "A" }), |
| | expect.objectContaining({ x: 1, y: 0, w: 2, h: 1, i: "B" }), |
| | expect.objectContaining({ x: 0, y: 2, w: 1, h: 1, i: "C" }), |
| | expect.objectContaining({ x: 1, y: 2, w: 3, h: 1, i: "D" }) |
| | ]); |
| | }); |
| |
|
| | it("Moves one element to another should cause moving down panels, vert compact, example 2", () => { |
| | |
| | |
| | |
| | |
| | |
| | const layout = [ |
| | { x: 0, y: 0, w: 2, h: 1, i: "A" }, |
| | { x: 0, y: 1, w: 1, h: 1, i: "B" }, |
| | { x: 1, y: 1, w: 1, h: 2, i: "C" } |
| | ]; |
| | |
| | const itemB = layout[2]; |
| | expect( |
| | compactAndMove( |
| | layout, |
| | itemB, |
| | 1, |
| | 0, |
| | true, |
| | false, |
| | "vertical", |
| | 4 |
| | ) |
| | ).toEqual([ |
| | expect.objectContaining({ x: 0, y: 2, w: 2, h: 1, i: "A" }), |
| | expect.objectContaining({ x: 0, y: 3, w: 1, h: 1, i: "B" }), |
| | expect.objectContaining({ x: 1, y: 0, w: 1, h: 2, i: "C" }) |
| | ]); |
| | }); |
| |
|
| | it("Prevent collision", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 1, h: 10, i: "A" }, |
| | { x: 0, y: 10, w: 1, h: 1, i: "B" }, |
| | { x: 0, y: 11, w: 1, h: 1, i: "C" } |
| | ]; |
| | |
| | |
| | const itemA = layout[0]; |
| | const modifiedLayout = moveElement( |
| | layout, |
| | itemA, |
| | 0, |
| | 2, |
| | true, |
| | true, |
| | null, |
| | 10 |
| | ); |
| | expect(Object.is(layout, modifiedLayout)).toBe(true); |
| |
|
| | expect(layout).toEqual([ |
| | expect.objectContaining({ x: 0, y: 0, w: 1, h: 10, i: "A" }), |
| | expect.objectContaining({ x: 0, y: 10, w: 1, h: 1, i: "B" }), |
| | expect.objectContaining({ x: 0, y: 11, w: 1, h: 1, i: "C" }) |
| | ]); |
| | }); |
| |
|
| | it("Allow overlapping the grid items", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 1, h: 10, i: "A" }, |
| | { x: 0, y: 10, w: 1, h: 1, i: "B" }, |
| | { x: 0, y: 11, w: 1, h: 1, i: "C" } |
| | ]; |
| | |
| | const itemA = layout[0]; |
| | expect( |
| | moveElement( |
| | layout, |
| | itemA, |
| | 0, |
| | 2, |
| | true, |
| | false, |
| | null, |
| | 10, |
| | true |
| | ) |
| | ).toEqual([ |
| | expect.objectContaining({ x: 0, y: 2, w: 1, h: 10, i: "A" }), |
| | expect.objectContaining({ x: 0, y: 10, w: 1, h: 1, i: "B" }), |
| | expect.objectContaining({ x: 0, y: 11, w: 1, h: 1, i: "C" }) |
| | ]); |
| | }); |
| |
|
| | it("Layout is cloned when using allowOverlap (#1606)", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 1, h: 10, i: "A" }, |
| | { x: 0, y: 10, w: 1, h: 1, i: "B" }, |
| | { x: 0, y: 11, w: 1, h: 1, i: "C" } |
| | ]; |
| | |
| | const itemA = layout[0]; |
| | const modifiedLayout = moveElement( |
| | layout, |
| | itemA, |
| | 0, |
| | 2, |
| | true, |
| | false, |
| | null, |
| | 10, |
| | true |
| | ); |
| | expect(Object.is(layout, modifiedLayout)).toBe(false); |
| | }); |
| | }); |
| |
|
| | describe("compact vertical", () => { |
| | it("Removes empty vertical space above item", () => { |
| | const layout = [{ i: "1", x: 0, y: 1, w: 1, h: 1 }]; |
| | expect(compact(layout, "vertical", 10)).toEqual([ |
| | { i: "1", x: 0, y: 0, w: 1, h: 1, moved: false, static: false } |
| | ]); |
| | }); |
| |
|
| | it("Resolve collision by moving item further down in array", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 1, h: 5, i: "1" }, |
| | { x: 0, y: 1, w: 1, h: 1, i: "2" } |
| | ]; |
| | expect(compact(layout, "vertical", 10)).toEqual([ |
| | { x: 0, y: 0, w: 1, h: 5, i: "1", moved: false, static: false }, |
| | { x: 0, y: 5, w: 1, h: 1, i: "2", moved: false, static: false } |
| | ]); |
| | }); |
| |
|
| | it("Handles recursive collision by moving new collisions out of the way before moving item down", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 2, h: 5, i: "1" }, |
| | { x: 0, y: 0, w: 10, h: 1, i: "2" }, |
| | { x: 5, y: 1, w: 1, h: 1, i: "3" }, |
| | { x: 5, y: 2, w: 1, h: 1, i: "4" }, |
| | { x: 5, y: 3, w: 1, h: 1, i: "5", static: true } |
| | ]; |
| |
|
| | expect(compact(layout, "vertical", 10)).toEqual([ |
| | { x: 0, y: 0, w: 2, h: 5, i: "1", moved: false, static: false }, |
| | { x: 0, y: 5, w: 10, h: 1, i: "2", moved: false, static: false }, |
| | { x: 5, y: 6, w: 1, h: 1, i: "3", moved: false, static: false }, |
| | { x: 5, y: 7, w: 1, h: 1, i: "4", moved: false, static: false }, |
| | { x: 5, y: 3, w: 1, h: 1, i: "5", moved: false, static: true } |
| | ]); |
| | }); |
| |
|
| | it("Clones layout items (does not modify input)", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 2, h: 5, i: "1" }, |
| | { x: 0, y: 0, w: 10, h: 1, i: "2" } |
| | ]; |
| | const out = compact(layout, "vertical", 10); |
| | layout.forEach(item => { |
| | expect(out.includes(item)).toEqual(false); |
| | }); |
| | }); |
| | }); |
| |
|
| | describe("compact horizontal", () => { |
| | it("compact horizontal should remove empty horizontal space to left of item", () => { |
| | const layout = [{ x: 5, y: 5, w: 1, h: 1, i: "1" }]; |
| | expect(compact(layout, "horizontal", 10)).toEqual([ |
| | { x: 0, y: 5, w: 1, h: 1, i: "1", moved: false, static: false } |
| | ]); |
| | }); |
| |
|
| | it("Resolve collision by moving item further to the right in array", () => { |
| | const layout = [ |
| | { y: 0, x: 0, h: 1, w: 5, i: "1" }, |
| | { y: 0, x: 1, h: 1, w: 1, i: "2" } |
| | ]; |
| | expect(compact(layout, "horizontal", 10)).toEqual([ |
| | { y: 0, x: 0, h: 1, w: 5, i: "1", moved: false, static: false }, |
| | { y: 0, x: 5, h: 1, w: 1, i: "2", moved: false, static: false } |
| | ]); |
| | }); |
| |
|
| | it("Handles recursive collision by moving new collisions out of the way before moving item to the right", () => { |
| | const layout = [ |
| | { y: 0, x: 0, h: 2, w: 5, i: "1" }, |
| | { y: 1, x: 0, h: 10, w: 1, i: "2" }, |
| | { y: 5, x: 1, h: 1, w: 1, i: "3" }, |
| | { y: 5, x: 2, h: 1, w: 1, i: "4" }, |
| | { y: 5, x: 2, h: 1, w: 1, i: "5", static: true } |
| | ]; |
| | expect(compact(layout, "horizontal", 10)).toEqual([ |
| | { y: 0, x: 0, h: 2, w: 5, i: "1", moved: false, static: false }, |
| | { y: 1, x: 5, h: 10, w: 1, i: "2", moved: false, static: false }, |
| | { y: 5, x: 6, h: 1, w: 1, i: "3", moved: false, static: false }, |
| | { y: 5, x: 7, h: 1, w: 1, i: "4", moved: false, static: false }, |
| | { y: 5, x: 2, h: 1, w: 1, i: "5", moved: false, static: true } |
| | ]); |
| | }); |
| |
|
| | it("Should put overflowing right elements as bottom needed without colliding and as left as possible", () => { |
| | const cols = 6; |
| | const layout = [ |
| | { y: 0, x: 0, h: 2, w: 2, i: "1" }, |
| | { y: 0, x: 2, h: 2, w: 2, i: "2" }, |
| | { y: 0, x: 4, h: 2, w: 2, i: "3" }, |
| | { y: -2, x: -2, h: 2, w: 2, i: "4" } |
| | ]; |
| |
|
| | expect(compact(layout, "horizontal", cols)).toEqual([ |
| | { y: 0, x: 2, h: 2, w: 2, i: "1", moved: false, static: false }, |
| | { y: 0, x: 4, h: 2, w: 2, i: "2", moved: false, static: false }, |
| | { y: 2, x: 0, h: 2, w: 2, i: "3", moved: false, static: false }, |
| | { y: 0, x: 0, h: 2, w: 2, i: "4", moved: false, static: false } |
| | ]); |
| | }); |
| | }); |
| |
|
| | const basePositionParams = { |
| | margin: [0, 0], |
| | containerPadding: [0, 0], |
| | containerWidth: 800, |
| | cols: 8, |
| | rowHeight: 50, |
| | maxRows: 12 |
| | }; |
| | describe("calcGridColWidth", () => { |
| | it("should complete basic calculation", () => { |
| | expect(calcGridColWidth(basePositionParams)).toEqual(100); |
| | }); |
| |
|
| | it("should consider margin", () => { |
| | const positionParams = { |
| | ...basePositionParams, |
| | margin: [10, 10] |
| | }; |
| | |
| | expect(calcGridColWidth(positionParams)).toEqual(91.25); |
| | }); |
| |
|
| | it("should consider container padding", () => { |
| | const positionParams = { |
| | ...basePositionParams, |
| | containerPadding: [100, 0] |
| | }; |
| | |
| | expect(calcGridColWidth(positionParams)).toEqual(75); |
| | }); |
| |
|
| | it("should consider margin and padding", () => { |
| | const positionParams = { |
| | ...basePositionParams, |
| | margin: [10, 0], |
| | containerPadding: [100, 0] |
| | }; |
| | |
| | expect(calcGridColWidth(positionParams)).toEqual(66.25); |
| | }); |
| | }); |
| |
|
| | describe("calcGridItemPosition", () => { |
| | it("should complete basic calculation", () => { |
| | const x = 1; |
| | const y = 1; |
| | const w = 2; |
| | const h = 2; |
| | const resizing = null; |
| | const dragging = null; |
| | const positionParams = { |
| | ...basePositionParams, |
| | margin: [10, 10], |
| | containerPadding: [100, 100] |
| | }; |
| | expect( |
| | calcGridItemPosition(positionParams, x, y, w, h, { resizing, dragging }) |
| | ).toEqual({ |
| | height: 110, |
| | left: 176, |
| | top: 160, |
| | width: 143 |
| | }); |
| | }); |
| | }); |
| |
|
| | describe("fastRGLPropsEqual", () => { |
| | it("should tell us if props are equal, including arrays and objects", () => { |
| | const props1 = { |
| | className: "foo", |
| | margin: [10, 10], |
| | style: { background: "red" } |
| | }; |
| | const props2 = { |
| | className: "foo", |
| | margin: [10, 10], |
| | style: { background: "red" } |
| | }; |
| | expect(fastRGLPropsEqual(props1, props2, deepEqual)).toEqual(true); |
| | }); |
| |
|
| | it("catches changed arrays", () => { |
| | const props1 = { |
| | margin: [10, 10] |
| | }; |
| | const props2 = { |
| | margin: [10, 11] |
| | }; |
| | expect(fastRGLPropsEqual(props1, props2, deepEqual)).toEqual(false); |
| | }); |
| |
|
| | it("ignores children", () => { |
| | const props1 = { |
| | children: ["foo", "bar"] |
| | }; |
| | const props2 = { |
| | children: ["biff", "bar"] |
| | }; |
| | expect(fastRGLPropsEqual(props1, props2, deepEqual)).toEqual(true); |
| | }); |
| |
|
| | it("fails added props", () => { |
| | const props1 = {}; |
| | const props2 = { |
| | droppingItem: { w: 1, h: 2, i: 3 } |
| | }; |
| | expect(fastRGLPropsEqual(props1, props2, deepEqual)).toEqual(false); |
| | }); |
| |
|
| | it("ignores invalid props", () => { |
| | const props1 = {}; |
| | const props2 = { |
| | somethingElse: { w: 1, h: 2, i: 3 } |
| | }; |
| | expect(fastRGLPropsEqual(props1, props2, deepEqual)).toEqual(true); |
| | }); |
| | }); |
| |
|
| | describe("calcWH", () => { |
| | const mockPositionParams = { |
| | margin: [0, 0], |
| | containerPadding: [0, 0], |
| | containerWidth: 400, |
| | cols: 4, |
| | rowHeight: 200, |
| | maxRows: 3 |
| | }; |
| | it("return { w: 1, h: 1 }", () => { |
| | const res = calcWH(mockPositionParams, 100, 200, 1, 1, "e"); |
| | expect(JSON.stringify(res)).toBe(JSON.stringify({ w: 1, h: 1 })); |
| | }); |
| | it("return { w: 2, h: 1 }", () => { |
| | const res = calcWH(mockPositionParams, 200, 200, 1, 1, "e"); |
| | expect(JSON.stringify(res)).toBe(JSON.stringify({ w: 2, h: 1 })); |
| | }); |
| | it("return { w: 1, h: 2 }", () => { |
| | const res = calcWH(mockPositionParams, 100, 400, 1, 1, "se"); |
| | expect(JSON.stringify(res)).toBe(JSON.stringify({ w: 1, h: 2 })); |
| | }); |
| | }); |
| |
|
| | describe("calcXY", () => { |
| | const mockPositionParams = { |
| | margin: [20, 20], |
| | containerPadding: [50, 50], |
| | containerWidth: 560, |
| | cols: 4, |
| | rowHeight: 100, |
| | maxRows: 3 |
| | }; |
| |
|
| | it("return {x:0, y:0}", () => { |
| | |
| | const LEFT = 109; |
| | const TOP = 100; |
| | const W = 1; |
| | const H = 1; |
| | const res = calcXY(mockPositionParams, TOP, LEFT, W, H); |
| | expect(JSON.stringify(res)).toBe(JSON.stringify({ x: 0, y: 0 })); |
| | }); |
| | it("return {x:1, y:0}", () => { |
| | |
| | const LEFT = 111; |
| | const TOP = 0; |
| | const W = 1; |
| | const H = 1; |
| | const res = calcXY(mockPositionParams, TOP, LEFT, W, H); |
| | expect(JSON.stringify(res)).toBe(JSON.stringify({ x: 1, y: 0 })); |
| | }); |
| | it("return {x:0, y:1}", () => { |
| | |
| | const LEFT = 50; |
| | const TOP = 170; |
| | const W = 1; |
| | const H = 1; |
| | const res = calcXY(mockPositionParams, TOP, LEFT, W, H); |
| | expect(JSON.stringify(res)).toBe(JSON.stringify({ x: 0, y: 1 })); |
| | }); |
| | }); |
| |
|
| | describe("compactType", () => { |
| | const mockProps = { |
| | verticalCompact: false, |
| | compactType: "horizontal" |
| | }; |
| | it("returns null when verticalCompact is false", () => { |
| | expect(compactType(mockProps)).toBe(null); |
| | }); |
| | it("returns compactType value when verticalCompact is true", () => { |
| | expect(compactType({ ...mockProps, verticalCompact: true })).toBe( |
| | "horizontal" |
| | ); |
| | }); |
| | }); |
| |
|
| | describe("deepFreeze", () => { |
| | it("smoke test", () => { |
| | const deepFreezeResult = deepFreeze( |
| | { a: "a", b: { b: "c" } }, |
| | { get: true, set: true } |
| | ); |
| | expect(JSON.stringify(deepFreezeResult)).toBe('{"a":"a","b":{"b":"c"}}'); |
| | }); |
| | it("gets nested key value", () => { |
| | const res = deepFreeze( |
| | { one: "a", two: { b: "c" } }, |
| | { set: true, get: true } |
| | ); |
| |
|
| | const val = res.two.b; |
| | expect(val).toBe("c"); |
| | }); |
| | it("defaults option prop to get: true", () => { |
| | const res = deepFreeze({ one: "a", two: { b: "c" } }); |
| |
|
| | expect(res.two.b).toBe("c"); |
| | }); |
| | it("does not pass check `if(options.set)` ", () => { |
| | const res = deepFreeze({ one: "a" }, { set: false, get: false }); |
| | expect(res.one).toBe("a"); |
| | }); |
| |
|
| | it("returns `toJSON`", () => { |
| | const res = deepFreeze({ a: "toJSON" }); |
| | expect(res.a.toString()).toBe(`toJSON`); |
| | }); |
| | describe('throws "unknown prop" error', () => { |
| | it("when setting bad key", () => { |
| | try { |
| | const res = deepFreeze( |
| | { one: "a", two: { b: "c" } }, |
| | { set: true, get: false } |
| | ); |
| | |
| | res.badProp = "dog"; |
| | } catch (e) { |
| | expect(e.message).toBe( |
| | 'Can not set unknown prop "badProp" on frozen object.' |
| | ); |
| | } |
| | }); |
| | it("when getting bad key", () => { |
| | try { |
| | const res = deepFreeze( |
| | { one: "a", two: { b: "c" } }, |
| | { set: true, get: true } |
| | ); |
| | |
| | res.badProp; |
| | } catch (e) { |
| | expect(e.message).toBe( |
| | 'Can not get unknown prop "badProp" on frozen object.' |
| | ); |
| | } |
| | }); |
| | }); |
| | }); |
| |
|
| | describe("synchronizeLayoutWithChildren", () => { |
| | const layout = [ |
| | { x: 0, y: 0, w: 1, h: 10, i: "A" }, |
| | { x: 0, y: 10, w: 1, h: 1, i: "B" }, |
| | { x: 0, y: 11, w: 1, h: 1, i: "C" } |
| | ]; |
| | const cols = 6; |
| | const compactType = "horizontal"; |
| | it("test", () => { |
| | const children = [ |
| | <div key="A" />, |
| | <div key="B" />, |
| | <div key="C" />, |
| | <div key="D" /> |
| | ]; |
| | const output = synchronizeLayoutWithChildren( |
| | layout, |
| | children, |
| | cols, |
| | compactType |
| | ); |
| | expect(output).toEqual([ |
| | expect.objectContaining({ w: 1, h: 10, x: 0, y: 0, i: "A" }), |
| | expect.objectContaining({ w: 1, h: 1, x: 0, y: 10, i: "B" }), |
| | expect.objectContaining({ w: 1, h: 1, x: 0, y: 11, i: "C" }), |
| | expect.objectContaining({ w: 1, h: 1, x: 0, y: 12, i: "D" }) |
| | ]); |
| | }); |
| | it("Prefers data-grid over layout", () => { |
| | const children = [ |
| | <div key="A" />, |
| | <div key="B" />, |
| | <div key="C" data-grid={{ x: 0, y: 11, w: 2, h: 2 }} /> |
| | ]; |
| | const output = synchronizeLayoutWithChildren( |
| | layout, |
| | children, |
| | cols, |
| | compactType |
| | ); |
| | expect(output).toEqual([ |
| | expect.objectContaining({ w: 1, h: 10, x: 0, y: 0, i: "A" }), |
| | expect.objectContaining({ w: 1, h: 1, x: 0, y: 10, i: "B" }), |
| | expect.objectContaining({ w: 2, h: 2, x: 0, y: 11, i: "C" }) |
| | ]); |
| | }); |
| | }); |
| |
|