Files
EvoScientist-WebUI/src/lib/workspaceSelection.test.ts
T

98 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
check,
emptySelection,
isChecked,
isIndeterminate,
toParams,
uncheck,
} from "./workspaceSelection";
describe("workspaceSelection", () => {
it("checks a single file", () => {
const sel = check(emptySelection(), "a.txt");
expect(isChecked(sel, "a.txt")).toBe(true);
expect(isChecked(sel, "b.txt")).toBe(false);
});
it("collapses descendants when a folder is checked", () => {
let sel = check(emptySelection(), "dir/a.txt");
sel = check(sel, "dir/b.txt");
sel = check(sel, "dir");
expect([...sel.checked]).toEqual(["dir"]);
expect(isChecked(sel, "dir/a.txt")).toBe(true);
expect(isChecked(sel, "dir/deep/c.txt")).toBe(true);
expect(isIndeterminate(sel, "dir")).toBe(false);
});
it("records an exclusion when a child of a checked folder is unchecked", () => {
let sel = check(emptySelection(), "dir");
sel = uncheck(sel, "dir/a.txt");
expect(isChecked(sel, "dir")).toBe(false);
expect(isChecked(sel, "dir/a.txt")).toBe(false);
expect(isChecked(sel, "dir/b.txt")).toBe(true);
expect(isIndeterminate(sel, "dir")).toBe(true);
});
it("excludes a whole subfolder and everything beneath it", () => {
let sel = check(emptySelection(), "dir");
sel = uncheck(sel, "dir/sub");
expect(isChecked(sel, "dir/sub")).toBe(false);
expect(isChecked(sel, "dir/sub/c.txt")).toBe(false);
expect(isChecked(sel, "dir/a.txt")).toBe(true);
});
it("re-checking an excluded child clears the exclusion", () => {
let sel = check(emptySelection(), "dir");
sel = uncheck(sel, "dir/a.txt");
sel = check(sel, "dir/a.txt");
expect([...sel.excluded]).toEqual([]);
expect(isChecked(sel, "dir")).toBe(true);
expect(isIndeterminate(sel, "dir")).toBe(false);
});
it("unchecking a top-level folder drops its exclusions too", () => {
let sel = check(emptySelection(), "dir");
sel = uncheck(sel, "dir/a.txt");
sel = uncheck(sel, "dir");
expect([...sel.checked]).toEqual([]);
expect([...sel.excluded]).toEqual([]);
expect(isChecked(sel, "dir/a.txt")).toBe(false);
});
it("checking a child of a checked-but-excluded path keeps the collapse", () => {
let sel = check(emptySelection(), "dir");
sel = uncheck(sel, "dir/sub");
sel = check(sel, "dir/sub/c.txt");
expect([...sel.checked].sort()).toEqual(["dir"]);
expect([...sel.excluded]).toEqual(["dir/sub"]);
expect(isChecked(sel, "dir/sub/c.txt")).toBe(false);
});
it("is a no-op when unchecking something not selected", () => {
const sel = check(emptySelection(), "dir");
expect(uncheck(sel, "other.txt")).toBe(sel);
});
it("re-checking an indeterminate folder clears all its exclusions", () => {
let sel = check(emptySelection(), "dir");
sel = uncheck(sel, "dir/a.txt");
sel = uncheck(sel, "dir/sub");
sel = check(sel, "dir");
expect(isChecked(sel, "dir")).toBe(true);
expect(isIndeterminate(sel, "dir")).toBe(false);
expect([...sel.excluded]).toEqual([]);
});
it("produces sorted include/exclude params", () => {
let sel = check(emptySelection(), "z.txt");
sel = check(sel, "dir");
sel = uncheck(sel, "dir/b.txt");
sel = uncheck(sel, "dir/a.txt");
expect(toParams(sel)).toEqual({
include: ["dir", "z.txt"],
exclude: ["dir/a.txt", "dir/b.txt"],
});
});
});