import { BorderCell, BorderSide, borderWidthOf, resolveCollapsedBorders, } from "./collapsed-borders"; const cell = ( id: string, row: number, col: number, width: number, span: { rowSpan?: number; colSpan?: number } = {} ): BorderCell => ({ id, row, col, rowSpan: span.rowSpan ?? 1, colSpan: span.colSpan ?? 1, widths: { top: width, right: width, bottom: width, left: width }, }); const hiddenOf = (result: Map>) => Object.fromEntries( Array.from(result, ([id, sides]) => [id, Array.from(sides).sort()]) ); describe("resolveCollapsedBorders", () => { test("keeps the top/left border where two equal ones meet", () => { const result = resolveCollapsedBorders( [cell("a", 0, 0, 1), cell("b", 0, 1, 1), cell("c", 1, 0, 1), cell("d", 1, 1, 1)], 2, 2 ); expect(hiddenOf(result)).toEqual({ b: ["left"], c: ["top"], d: ["left", "top"], }); }); test("keeps the wider border, from either side", () => { // A highlighted 3px column next to 1px cells. const result = resolveCollapsedBorders( [cell("a", 0, 0, 1), cell("b", 0, 1, 3), cell("c", 0, 2, 1)], 1, 3 ); expect(hiddenOf(result)).toEqual({ a: ["right"], c: ["left"] }); }); test("a cell with no border on a side leaves its neighbour's", () => { const a = cell("a", 0, 0, 1); a.widths.right = 0; const result = resolveCollapsedBorders([a, cell("b", 0, 1, 1)], 1, 2); expect(hiddenOf(result)).toEqual({}); }); test("smaller cells give way to a merged cell's whole edge", () => { // a spans both rows; b and c sit to its right. const result = resolveCollapsedBorders( [ cell("a", 0, 0, 1, { rowSpan: 2 }), cell("b", 0, 1, 5), cell("c", 1, 1, 1), ], 2, 2 ); expect(hiddenOf(result)).toEqual({ b: ["left"], c: ["left", "top"], }); }); test("a merged cell's edge wins over the smaller cells before it", () => { // c spans both rows to the right of a and b. const result = resolveCollapsedBorders( [ cell("a", 0, 0, 1), cell("b", 1, 0, 1), cell("c", 0, 1, 1, { rowSpan: 2 }), ], 2, 2 ); expect(hiddenOf(result)).toEqual({ a: ["right"], b: ["right", "top"], }); }); test("edge cells give way to the table's own border", () => { const result = resolveCollapsedBorders( [cell("a", 0, 0, 1), cell("b", 0, 1, 1)], 1, 2, { top: 2, right: 0, bottom: 2, left: 2 } ); expect(hiddenOf(result)).toEqual({ a: ["bottom", "left", "top"], b: ["bottom", "left", "top"], }); }); }); describe("borderWidthOf", () => { test.each([ ["2px solid #000", 2], ["solid 1.5px rgb(0, 0, 0)", 1.5], ["0.5em dashed red", 8], ["thick double blue", 5], ["solid #000", 3], ["none", 0], ["", 0], ["0 solid #000", 0], ])("%s is %d", (border, width) => { expect(borderWidthOf(border)).toBe(width); }); });