import "jest"; import { buildNode, DovetailNodeFactory } from "../../__specs__/buildEditorState"; import "../../__specs__/expect.toMatchFragment"; import "../../__specs__/expect.toMatchSlice"; import { Bias } from "../../types"; import { WedgeSelection } from "../WedgeSelection"; describe("#findFrom", () => { function test(nodeFactory: DovetailNodeFactory, bias: Bias, fromRef = "x", toRef = "y") { const { node: doc, refMap } = buildNode(nodeFactory); const sel = WedgeSelection.findFrom(doc.resolve(refMap.get(fromRef)!), bias == Bias.FORWARD ? 1 : -1); expect(sel).not.toBe(null); expect(sel!.$anchor.pos).toBe(refMap.get(toRef)!); } it("finds position backwards between table and start of doc", () => test(({ doc, table, tr, td, p }) => doc("{y}", table(tr(td(p("{x}"))))), Bias.BACKWARD)); it("finds position forwards between table and end of doc", () => test(({ doc, table, tr, td, p }) => doc(table(tr(td(p("{x}")))), "{y}"), Bias.FORWARD)); it("finds position forwards between attachment and end of cell", () => test(({ doc, table, tr, td, at }) => doc(table(tr(td(at({ id: "000" }), "{x}{y}")))), Bias.FORWARD)); it("finds position forwards between attachment and end of cell (with p trailing table)", () => test(({ doc, table, tr, td, p, at }) => doc(table(tr(td(at({ id: "000" }), "{x}{y}"))), p()), Bias.FORWARD)); it("finds position backwards between two tables", () => test(({ doc, table, tr, td, p }) => doc(table(tr(td(p()))), "{y}", table(tr(td(p("{x}"))))), Bias.BACKWARD)); it("finds position forwards between two tables", () => test(({ doc, table, tr, td, p }) => doc(table(tr(td(p("{x}")))), "{y}", table(tr(td(p())))), Bias.FORWARD)); }); describe("#valid", () => { function test(nodeFactory: DovetailNodeFactory, expected = true, ref = "x") { const { node: doc, refMap } = buildNode(nodeFactory); expect(WedgeSelection.valid(doc.resolve(refMap.get(ref)!))).toBe(expected); } it("between attachment and end of cell", () => test(({ doc, table, tr, td, at }) => doc(table(tr(td(at({ id: "000" }), "{x}")))))); it("between attachment and end of cell (with p trailing table)", () => test(({ doc, table, tr, td, p, at }) => doc(table(tr(td(at({ id: "000" }), "{x}"))), p()))); });