test: tighten shared singleton and sample coverage

This commit is contained in:
Peter Steinberger
2026-03-13 21:25:20 +00:00
parent 42ccee658d
commit 0c79c86b40
2 changed files with 40 additions and 0 deletions

View File

@@ -27,6 +27,15 @@ describe("resolveGlobalSingleton", () => {
expect(resolveGlobalSingleton(TEST_KEY, create)).toBeUndefined();
expect(create).toHaveBeenCalledTimes(1);
});
it("reuses a prepopulated global value without calling the factory", () => {
const existing = { value: 7 };
const create = vi.fn(() => ({ value: 1 }));
(globalThis as Record<PropertyKey, unknown>)[TEST_KEY] = existing;
expect(resolveGlobalSingleton(TEST_KEY, create)).toBe(existing);
expect(create).not.toHaveBeenCalled();
});
});
describe("resolveGlobalMap", () => {
@@ -36,4 +45,11 @@ describe("resolveGlobalMap", () => {
expect(first).toBe(second);
});
it("preserves existing map contents across repeated resolution", () => {
const map = resolveGlobalMap<string, number>(TEST_MAP_KEY);
map.set("a", 1);
expect(resolveGlobalMap<string, number>(TEST_MAP_KEY).get("a")).toBe(1);
});
});

View File

@@ -4,6 +4,7 @@ import { summarizeStringEntries } from "./string-sample.js";
describe("summarizeStringEntries", () => {
it("returns emptyText for empty lists", () => {
expect(summarizeStringEntries({ entries: [], emptyText: "any" })).toBe("any");
expect(summarizeStringEntries({ entries: null })).toBe("");
});
it("joins short lists without a suffix", () => {
@@ -18,4 +19,27 @@ describe("summarizeStringEntries", () => {
}),
).toBe("a, b, c, d (+1)");
});
it("uses a floored limit and clamps non-positive values to one entry", () => {
expect(
summarizeStringEntries({
entries: ["a", "b", "c"],
limit: 2.8,
}),
).toBe("a, b (+1)");
expect(
summarizeStringEntries({
entries: ["a", "b", "c"],
limit: 0,
}),
).toBe("a (+2)");
});
it("uses the default limit when none is provided", () => {
expect(
summarizeStringEntries({
entries: ["a", "b", "c", "d", "e", "f", "g"],
}),
).toBe("a, b, c, d, e, f (+1)");
});
});