import "jest"; import { buildNode } from "../../__specs__/buildEditorState"; import { Bias, CartesianAxis, TableNode, TablePosOfCell } from "../../types"; import { TableMap } from "../TableMap"; describe("TableMap", () => { it("finds the right shape for a simple table", () => { const table = buildNode(({ table, tr, c11 }) => table(tr(c11, c11, c11), tr(c11, c11, c11), tr(c11, c11, c11), tr(c11, c11, c11)) ).node as TableNode; expect(TableMap.get(table).map).toMatchObject([1, 6, 11, 18, 23, 28, 35, 40, 45, 52, 57, 62]); }); it("finds the right shape for colspans", () => { const table = buildNode(({ table, tr, c11, c }) => table(tr(c11, c(2, 1)), tr(c(2, 1), c11), tr(c11, c11, c11))) .node as TableNode; expect(TableMap.get(table).map).toMatchObject([1, 6, 6, 13, 13, 18, 25, 30, 35]); }); it("finds the right shape for rowspans", () => { const table = buildNode(({ table, tr, c11, c }) => table(tr(c(1, 2), c11, c(1, 2)), tr(c11))).node as TableNode; expect(TableMap.get(table).map).toMatchObject([1, 6, 11, 1, 18, 11]); }); it("finds the right shape for deep rowspans", () => { const table = buildNode(({ table, tr, c }) => table(tr(c(1, 4), c(2, 1)), tr(c(1, 2), c(1, 2)), tr())).node as TableNode; expect(TableMap.get(table).map).toMatchObject([1, 6, 6, 1, 13, 18, 1, 13, 18]); }); it("finds the right shape for larger rectangles", () => { const table = buildNode(({ table, tr, c11, c }) => table(tr(c11, c(4, 4)), tr(c11), tr(c11), tr(c11))).node as TableNode; expect(TableMap.get(table).map).toMatchObject([1, 6, 6, 6, 6, 13, 6, 6, 6, 6, 20, 6, 6, 6, 6, 27, 6, 6, 6, 6]); }); const table = buildNode(({ table, tr, c11, c }) => table(tr(c(2, 3), c11, c(1, 2)), tr(c11), tr(c(2, 1)))).node as TableNode; const map = TableMap.get(table); // 1 1 6 11 // 1 1 18 11 // 1 1 25 25 it("can accurately find cell sizes", () => { expect(map.width).toBe(4); expect(map.height).toBe(3); expect(map.findCell(1 as TablePosOfCell)).toMatchObject({ left: 0, right: 2, top: 0, bottom: 3 }); expect(map.findCell(6 as TablePosOfCell)).toMatchObject({ left: 2, right: 3, top: 0, bottom: 1 }); expect(map.findCell(11 as TablePosOfCell)).toMatchObject({ left: 3, right: 4, top: 0, bottom: 2 }); expect(map.findCell(18 as TablePosOfCell)).toMatchObject({ left: 2, right: 3, top: 1, bottom: 2 }); expect(map.findCell(25 as TablePosOfCell)).toMatchObject({ left: 2, right: 4, top: 2, bottom: 3 }); }); it("can find the rectangle between two cells", () => { expect(map.cellsInRect(map.rectBetween(1 as TablePosOfCell, 6 as TablePosOfCell))).toMatchObject([1, 6, 18, 25]); expect(map.cellsInRect(map.rectBetween(1 as TablePosOfCell, 25 as TablePosOfCell))).toMatchObject([1, 6, 11, 18, 25]); expect(map.cellsInRect(map.rectBetween(1 as TablePosOfCell, 1 as TablePosOfCell))).toMatchObject([1]); expect(map.cellsInRect(map.rectBetween(6 as TablePosOfCell, 25 as TablePosOfCell))).toMatchObject([6, 11, 18, 25]); expect(map.cellsInRect(map.rectBetween(6 as TablePosOfCell, 11 as TablePosOfCell))).toMatchObject([6, 11, 18]); expect(map.cellsInRect(map.rectBetween(11 as TablePosOfCell, 6 as TablePosOfCell))).toMatchObject([6, 11, 18]); expect(map.cellsInRect(map.rectBetween(18 as TablePosOfCell, 25 as TablePosOfCell))).toMatchObject([18, 25]); expect(map.cellsInRect(map.rectBetween(6 as TablePosOfCell, 18 as TablePosOfCell))).toMatchObject([6, 18]); }); it("can find adjacent cells", () => { expect(map.nextCell(1 as TablePosOfCell, CartesianAxis.HORIZONTAL, Bias.FORWARD)).toBe(6); expect(map.nextCell(1 as TablePosOfCell, CartesianAxis.HORIZONTAL, Bias.BACKWARD)).toBe(null); expect(map.nextCell(1 as TablePosOfCell, CartesianAxis.VERTICAL, Bias.FORWARD)).toBe(null); expect(map.nextCell(1 as TablePosOfCell, CartesianAxis.VERTICAL, Bias.BACKWARD)).toBe(null); expect(map.nextCell(18 as TablePosOfCell, CartesianAxis.HORIZONTAL, Bias.FORWARD)).toBe(11); expect(map.nextCell(18 as TablePosOfCell, CartesianAxis.HORIZONTAL, Bias.BACKWARD)).toBe(1); expect(map.nextCell(18 as TablePosOfCell, CartesianAxis.VERTICAL, Bias.FORWARD)).toBe(25); expect(map.nextCell(18 as TablePosOfCell, CartesianAxis.VERTICAL, Bias.BACKWARD)).toBe(6); expect(map.nextCell(25 as TablePosOfCell, CartesianAxis.VERTICAL, Bias.FORWARD)).toBe(null); expect(map.nextCell(25 as TablePosOfCell, CartesianAxis.VERTICAL, Bias.BACKWARD)).toBe(18); expect(map.nextCell(25 as TablePosOfCell, CartesianAxis.HORIZONTAL, Bias.FORWARD)).toBe(null); expect(map.nextCell(25 as TablePosOfCell, CartesianAxis.HORIZONTAL, Bias.BACKWARD)).toBe(1); }); });