File size: 1,886 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import { describe, expect, it } from "vitest";
import { concatOptionalTextSegments, joinPresentTextSegments } from "./join-segments.js";
describe("concatOptionalTextSegments", () => {
it("concatenates left and right with default separator", () => {
expect(concatOptionalTextSegments({ left: "A", right: "B" })).toBe("A\n\nB");
});
it("keeps explicit empty-string right value", () => {
expect(concatOptionalTextSegments({ left: "A", right: "" })).toBe("");
});
it("falls back to whichever side is present and honors custom separators", () => {
expect(concatOptionalTextSegments({ left: "A" })).toBe("A");
expect(concatOptionalTextSegments({ right: "B" })).toBe("B");
expect(concatOptionalTextSegments({ left: "", right: "B" })).toBe("B");
expect(concatOptionalTextSegments({ left: "" })).toBe("");
expect(concatOptionalTextSegments({ left: "A", right: "B", separator: " | " })).toBe("A | B");
});
});
describe("joinPresentTextSegments", () => {
it("joins non-empty segments", () => {
expect(joinPresentTextSegments(["A", undefined, "B"])).toBe("A\n\nB");
});
it("returns undefined when all segments are empty", () => {
expect(joinPresentTextSegments(["", undefined, null])).toBeUndefined();
});
it("trims segments when requested", () => {
expect(joinPresentTextSegments([" A ", " B "], { trim: true })).toBe("A\n\nB");
});
it("keeps whitespace-only segments unless trim is enabled and supports custom separators", () => {
expect(joinPresentTextSegments(["A", " ", "B"], { separator: " | " })).toBe("A | | B");
expect(joinPresentTextSegments(["A", " ", "B"], { trim: true, separator: " | " })).toBe(
"A | B",
);
});
it("preserves segment whitespace when trim is disabled", () => {
expect(joinPresentTextSegments(["A", " B "], { separator: "|" })).toBe("A| B ");
});
});
|