import "jest"; import { compose } from "../../compose"; import { encodeToPlainSingleLine, PlainSingleLine } from "../encodeToPlainSingleLine"; const { doc, p, b } = compose; describe(encodeToPlainSingleLine.name, () => { it("ignores bold mark", () => { const { node } = doc(p("Hel", b("lo wo"), "rld")); expect(encodeToPlainSingleLine(node)).toMatchObject({ text: "Hello world", textAfter: "", textBefore: "" }); }); it("renders entire node to text when range unspecified", () => { const { node } = doc(p("Hello world")); expect(encodeToPlainSingleLine(node)).toMatchObject({ text: "Hello world", textAfter: "", textBefore: "" }); }); it("renders only the range when specified", () => { const { node, refMap } = doc(p("Hel{from}lo wo{to}rld")); expect(encodeToPlainSingleLine(node, { range: [refMap.get("from")!, refMap.get("to")!] })).toMatchObject({ text: "lo wo", textAfter: "rld", textBefore: "Hel" }); }); it("inserts spaces when range aligns with a block", () => { const { node, refMap } = doc(p("Hello"), p("{from}good{to}"), p("world")); expect(encodeToPlainSingleLine(node, { range: [refMap.get("from")!, refMap.get("to")!] })).toMatchObject({ text: "good", textAfter: " world", textBefore: "Hello " }); }); it("enforces constraints", () => { const { node, refMap } = doc(p("Hel{from}lo wo{to}rld")); expect( encodeToPlainSingleLine(node, { range: [refMap.get("from")!, refMap.get("to")!], constraints: { textBefore: 1, text: 10, textAfter: 2 } }) ).toMatchObject({ text: "lo wo", textAfter: "r…", textBefore: "…" }); }); it("enforces constraints using text length rather than node length", () => { const { node, refMap } = doc(p("Hel{from}lo"), p("wo{to}rld")); expect( encodeToPlainSingleLine(node, { range: [refMap.get("from")!, refMap.get("to")!], constraints: { textBefore: 1, text: 5, textAfter: 2 } }) ).toMatchObject({ text: "lo wo", textAfter: "r…", textBefore: "…" }); }); it("enforces constraints using text length rather than node length #2", () => { const { node, refMap } = doc(p("Hel{from}lo"), p("good"), p("wo{to}rld")); expect( encodeToPlainSingleLine(node, { range: [refMap.get("from")!, refMap.get("to")!], constraints: { textBefore: 1, text: 10, textAfter: 2 } }) ).toMatchObject({ text: "lo good wo", textAfter: "r…", textBefore: "…" }); }); }); describe(PlainSingleLine.name, () => { describe(`#${PlainSingleLine.prototype.eq.name}`, () => { it("detects identical", () => { expect( new PlainSingleLine("textBefore", "text", "textAfter").eq(new PlainSingleLine("textBefore", "text", "textAfter")) ).toBe(true); }); it("detects different", () => { expect(new PlainSingleLine("textBefore", "text", "textAfter").eq(new PlainSingleLine("xxx", "text", "textAfter"))).toBe( false ); expect( new PlainSingleLine("textBefore", "text", "textAfter").eq(new PlainSingleLine("textBefore", "xxx", "textAfter")) ).toBe(false); expect(new PlainSingleLine("textBefore", "text", "textAfter").eq(new PlainSingleLine("textBefore", "text", "xxx"))).toBe( false ); }); }); });