import "jest"; import { buildEditorState, buildNode, buildSlice, DovetailNodeFactory } from "../../__specs__/buildEditorState"; import "../../__specs__/expect.toMatchFragment"; import "../../__specs__/expect.toMatchSlice"; import { CellSelection } from "../CellSelection"; const t = buildNode(({ doc, table, tr, cEmpty }) => doc( table( /* 1 */ tr(/* 2 */ cEmpty, /* 6 */ cEmpty, /* 10 */ cEmpty), /* 15 */ tr(/* 16 */ cEmpty, /* 20 */ cEmpty, /* 24 */ cEmpty), /* 29 */ tr(/* 30 */ cEmpty, /* 34 */ cEmpty, /* 38 */ cEmpty) ) ) ).node; describe(".colSelection()", () => { it("will put $anchorCell at the top and $headCell at the bottom of a cell's column", () => { const s = CellSelection.colSelection(t.resolve(2)); expect(s.$anchorCell.pos).toBe(2); expect(s.$headCell.pos).toBe(30); }); it("will push $anchorCell to the top and $headCell to the bottom when anchor is closer to the top than head", () => { const s = CellSelection.colSelection(t.resolve(16), t.resolve(30)); expect(s.$anchorCell.pos).toBe(2); expect(s.$headCell.pos).toBe(30); }); it("will push $anchorCell to the bottom and $headCell to the top when anchor is closer to the bottom than head", () => { const s = CellSelection.colSelection(t.resolve(30), t.resolve(16)); expect(s.$anchorCell.pos).toBe(30); expect(s.$headCell.pos).toBe(2); }); }); describe(".rowSelection()", () => { it("will put $anchorCell at the left and $headCell at the right of a cell's row", () => { const s = CellSelection.rowSelection(t.resolve(2)); expect(s.$anchorCell.pos).toBe(2); expect(s.$headCell.pos).toBe(10); }); it("will push $anchorCell to the left and $headCell to the right when anchor is closer to the left than head", () => { const s = CellSelection.rowSelection(t.resolve(2), t.resolve(6)); expect(s.$anchorCell.pos).toBe(2); expect(s.$headCell.pos).toBe(10); }); it("will push $anchorCell to the right and $headCell to the left when anchor is closer to the right than head", () => { const s = CellSelection.rowSelection(t.resolve(6), t.resolve(2)); expect(s.$anchorCell.pos).toBe(10); expect(s.$headCell.pos).toBe(2); }); }); describe(".create()", () => { it("will put its head/anchor around the head cell", () => { let s = CellSelection.create(t, 2, 24); expect(s.anchor).toBe(25); expect(s.head).toBe(27); s = CellSelection.create(t, 24, 2); expect(s.anchor).toBe(3); expect(s.head).toBe(5); s = CellSelection.create(t, 10, 30); expect(s.anchor).toBe(31); expect(s.head).toBe(33); s = CellSelection.create(t, 30, 10); expect(s.anchor).toBe(11); expect(s.head).toBe(13); }); }); describe("#content()", () => { function selectionFor(build: DovetailNodeFactory): CellSelection { return buildEditorState(build).selection as CellSelection; } it("contains only the selected cells", () => expect( selectionFor(({ table, tr, c11, cAnchor, cEmpty, cHead }) => table(tr(c11, cAnchor, cEmpty), tr(c11, cEmpty, cHead), tr(c11, c11, c11)) ).content() ).toMatchSlice(buildSlice(({ tr, c11, cEmpty }) => [tr(c11, cEmpty), tr(cEmpty, c11)], 1, 1))); it("understands spanning cells", () => expect( selectionFor(({ table, tr, c11, cAnchor, c, cHead }) => table(tr(cAnchor, c(2, 2), c11, c11), tr(c11, cHead, c11, c11)) ).content() ).toMatchSlice(buildSlice(({ tr, c11, c }) => [tr(c11, c(2, 2), c11), tr(c11, c11)], 1, 1))); it("cuts off cells sticking out horizontally", () => expect( selectionFor(({ table, tr, c11, cAnchor, c, cHead }) => table(tr(c11, cAnchor, c(2, 1)), tr(c(4, 1)), tr(c(2, 1), cHead, c11)) ).content() ).toMatchSlice( buildSlice(({ tr, c11, td_, p, cEmpty }) => [tr(c11, c11), tr(td_({ colspan: 2 })(p())), tr(cEmpty, c11)], 1, 1) )); it("cuts off cells sticking out vertically", () => expect( selectionFor(({ table, tr, c11, cAnchor, c, cHead }) => table(tr(c11, c(1, 4), c(1, 2)), tr(cAnchor), tr(c(1, 2), cHead), tr(c11)) ).content() ).toMatchSlice( buildSlice(({ tr, c11, td_, p, cEmpty }) => [tr(c11, td_({ rowspan: 2 })(p()), cEmpty), tr(c11, c11)], 1, 1) )); it("preserves column widths", () => expect( selectionFor(({ table, tr, c11, cAnchor, td_, p, cHead }) => table(tr(c11, cAnchor, c11), tr(td_({ colspan: 3, colwidth: [100, 200, 300] })(p("x"))), tr(c11, cHead, c11)) ).content() ).toMatchSlice(buildSlice(({ tr, c11, td_, p }) => [tr(c11), tr(td_({ colwidth: [200] })(p())), tr(c11)], 1, 1))); });