279 lines
9.9 KiB
TypeScript
279 lines
9.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { effectScope, reactive, ref } from "vue";
|
|
import {
|
|
copyKeys,
|
|
decodeKey,
|
|
encodeKey,
|
|
type CollectionKey,
|
|
} from "../packages/vue/src/components/collection/keys";
|
|
import { useCollectionSelection } from "../packages/vue/src/components/collection/selection";
|
|
import { useCollectionRegistry } from "../packages/vue/src/components/collection/registry";
|
|
|
|
function selection(overrides = {}) {
|
|
const props = reactive({
|
|
mode: "multiple" as "none" | "single" | "multiple",
|
|
controlled: false,
|
|
value: undefined as CollectionKey[] | undefined,
|
|
disabled: false,
|
|
readOnly: false,
|
|
disabledKeys: [] as CollectionKey[],
|
|
disallowEmptySelection: false,
|
|
...overrides,
|
|
});
|
|
const onChange = vi.fn();
|
|
const state = useCollectionSelection({
|
|
mode: () => props.mode,
|
|
controlled: () => props.controlled,
|
|
value: () => props.value,
|
|
disabled: () => props.disabled,
|
|
readOnly: () => props.readOnly,
|
|
disabledKeys: () => props.disabledKeys,
|
|
disallowEmptySelection: () => props.disallowEmptySelection,
|
|
defaultValue: ["initial"],
|
|
onChange,
|
|
});
|
|
return { state, props, onChange };
|
|
}
|
|
|
|
describe("private collection identity and accepted selection", () => {
|
|
it("round trips typed keys including empty, zero and adapter-like strings", () => {
|
|
const keys = ["", 0, "0", 1, "1", '["number",1]', "__proto__", "中文"];
|
|
expect(new Set(keys.map(encodeKey)).size).toBe(keys.length);
|
|
expect(keys.map(encodeKey).map(decodeKey)).toEqual(keys);
|
|
expect(copyKeys([0, "0", 0, "0"])).toEqual([0, "0"]);
|
|
});
|
|
|
|
it("rejects unsupported identities and malformed adapter values", () => {
|
|
for (const key of [NaN, Infinity, null, undefined, {}, []]) {
|
|
expect(() => encodeKey(key as CollectionKey)).toThrow(TypeError);
|
|
}
|
|
for (const encoded of ['["number","1"]', "{}", "null", '["object",{}]']) {
|
|
expect(() => decodeKey(encoded)).toThrow(TypeError);
|
|
}
|
|
});
|
|
|
|
it("keeps caller arrays and emitted snapshots independent", () => {
|
|
const initial: CollectionKey[] = [1];
|
|
const onChange = vi.fn((value: CollectionKey[]) => value.push("injected"));
|
|
const state = useCollectionSelection({
|
|
mode: () => "multiple",
|
|
controlled: () => false,
|
|
value: () => undefined,
|
|
defaultValue: initial,
|
|
onChange,
|
|
});
|
|
initial.push(2);
|
|
state.toggle("1");
|
|
expect(state.keys.value).toEqual([1, "1"]);
|
|
state.reset();
|
|
expect(state.keys.value).toEqual([1]);
|
|
});
|
|
|
|
it("retries equal rejected proposals from the last accepted value", () => {
|
|
const { state, props, onChange } = selection({
|
|
controlled: true,
|
|
value: [1],
|
|
});
|
|
state.toggle("1");
|
|
state.toggle("1");
|
|
expect(onChange.mock.calls).toEqual([[[1, "1"]], [[1, "1"]]]);
|
|
expect(state.keys.value).toEqual([1]);
|
|
props.value = [1, "1"];
|
|
state.toggle("1");
|
|
expect(onChange).toHaveBeenLastCalledWith([1]);
|
|
expect(state.keys.value).toEqual([1, "1"]);
|
|
});
|
|
|
|
it("treats an explicitly controlled undefined as empty", () => {
|
|
const { state, onChange } = selection({ controlled: true });
|
|
expect(state.keys.value).toEqual([]);
|
|
state.toggle(0);
|
|
state.reset();
|
|
expect(state.keys.value).toEqual([]);
|
|
expect(onChange).toHaveBeenCalledExactlyOnceWith([0]);
|
|
});
|
|
|
|
it("exposes immutable accepted snapshots and treats reordering as the same selection", () => {
|
|
const { state, onChange } = selection({
|
|
controlled: true,
|
|
value: [0, "0"],
|
|
});
|
|
expect(Object.isFrozen(state.keys.value)).toBe(true);
|
|
expect(state.request(["0", 0])).toBe(false);
|
|
expect(state.keys.value).toEqual([0, "0"]);
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("supports single replace, toggle and disallow-empty without duplicate changes", () => {
|
|
const { state, props, onChange } = selection({ mode: "single" });
|
|
expect(state.replace(0)).toBe(true);
|
|
expect(state.replace(0)).toBe(false);
|
|
props.disallowEmptySelection = true;
|
|
expect(state.toggle(0)).toBe(false);
|
|
expect(state.request([])).toBe(false);
|
|
props.disallowEmptySelection = false;
|
|
expect(state.toggle(0)).toBe(true);
|
|
expect(state.keys.value).toEqual([]);
|
|
expect(onChange.mock.calls).toEqual([[[0]], [[]]]);
|
|
expect(() => state.request([1, 2])).toThrow(TypeError);
|
|
});
|
|
|
|
it.each(["none", "disabled", "readOnly"])(
|
|
"blocks selection for %s",
|
|
(kind) => {
|
|
const { state, onChange } = selection(
|
|
kind === "none" ? { mode: "none" } : { [kind]: true },
|
|
);
|
|
expect(state.toggle("x")).toBe(false);
|
|
expect(state.replace("x")).toBe(false);
|
|
expect(state.selectAll(["x"])).toBe(false);
|
|
expect(state.request([])).toBe(false);
|
|
expect(state.keys.value).toEqual(["initial"]);
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
|
|
it("preserves disabled selections and excludes disabled additions in bulk paths", () => {
|
|
const { state, props, onChange } = selection({
|
|
disabledKeys: ["initial", 0],
|
|
});
|
|
expect(state.toggle("initial")).toBe(false);
|
|
expect(state.request(["initial", 0])).toBe(false);
|
|
state.selectAll([0, "0", "other"]);
|
|
expect(state.keys.value).toEqual(["initial", "0", "other"]);
|
|
props.disabledKeys = [];
|
|
state.toggle("initial");
|
|
expect(state.keys.value).toEqual(["0", "other"]);
|
|
expect(onChange).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("can replace a disabled initial selection with an available single option", () => {
|
|
const { state, onChange } = selection({
|
|
mode: "single",
|
|
disabledKeys: ["initial"],
|
|
});
|
|
expect(state.toggle("initial")).toBe(false);
|
|
expect(state.replace(0)).toBe(true);
|
|
expect(state.keys.value).toEqual([0]);
|
|
expect(onChange).toHaveBeenCalledExactlyOnceWith([0]);
|
|
});
|
|
|
|
it("leaves accepted missing keys intact until caller explicitly edits them", () => {
|
|
const { state, props, onChange } = selection({
|
|
controlled: true,
|
|
value: ["missing"],
|
|
});
|
|
expect(state.keys.value).toEqual(["missing"]);
|
|
props.value = ["replacement"];
|
|
expect(state.keys.value).toEqual(["replacement"]);
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("reset restores the initial snapshot silently, including while disabled", () => {
|
|
const { state, props, onChange } = selection();
|
|
state.toggle("added");
|
|
props.disabled = true;
|
|
state.reset();
|
|
expect(state.keys.value).toEqual(["initial"]);
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe("collection membership, DOM order and navigation targets", () => {
|
|
function member(key: CollectionKey, disabled = false) {
|
|
const data = reactive({ key, disabled, text: `Item ${key}` });
|
|
const element = document.createElement("div");
|
|
return {
|
|
data,
|
|
node: element,
|
|
member: {
|
|
key: () => data.key,
|
|
element: () => element,
|
|
textValue: () => data.text,
|
|
disabled: () => data.disabled,
|
|
},
|
|
};
|
|
}
|
|
|
|
it("tracks live DOM reordering, disabled updates and native member removal", () => {
|
|
const registry = useCollectionRegistry();
|
|
const a = member("a"),
|
|
b = member("b"),
|
|
c = member("c", true);
|
|
const parent = document.createElement("div");
|
|
parent.append(a.node, b.node, c.node);
|
|
registry.register(c.member);
|
|
registry.register(a.member);
|
|
const removeB = registry.register(b.member);
|
|
expect(registry.ordered().map((item) => item.key())).toEqual([
|
|
"a",
|
|
"b",
|
|
"c",
|
|
]);
|
|
parent.prepend(c.node);
|
|
expect(registry.available().map((item) => item.key())).toEqual(["a", "b"]);
|
|
c.data.disabled = false;
|
|
expect(registry.adjacent("b", "next", true)?.key()).toBe("c");
|
|
expect(registry.adjacent("b", "next")).toBeUndefined();
|
|
expect(registry.adjacent(undefined, "previous")?.key()).toBe("b");
|
|
expect(registry.adjacent("a", "previous")?.key()).toBe("c");
|
|
removeB();
|
|
removeB();
|
|
expect(registry.ordered().map((item) => item.key())).toEqual(["c", "a"]);
|
|
});
|
|
|
|
it("distinguishes typed keys and rejects initial and dynamic duplicates", () => {
|
|
const registry = useCollectionRegistry();
|
|
const a = member(1),
|
|
b = member("1");
|
|
registry.register(a.member);
|
|
registry.register(b.member);
|
|
expect(() => registry.register(member(1).member)).toThrow("Duplicate");
|
|
b.data.key = 1;
|
|
expect(() => registry.ordered()).toThrow("Duplicate");
|
|
b.data.key = "fixed";
|
|
expect(registry.ordered()).toHaveLength(2);
|
|
});
|
|
|
|
it("uses available DOM order for ranges and handles stale anchors", () => {
|
|
const registry = useCollectionRegistry();
|
|
const items = [member(0), member(1, true), member("1"), member(2)];
|
|
const parent = document.createElement("div");
|
|
items.forEach((item) => {
|
|
parent.append(item.node);
|
|
registry.register(item.member);
|
|
});
|
|
expect(registry.range(2, 0)).toEqual([0, "1", 2]);
|
|
expect(registry.range(1, 2)).toEqual([]);
|
|
expect(registry.range("missing", 2)).toEqual([]);
|
|
expect(registry.adjacent(0, "first")?.key()).toBe(0);
|
|
expect(registry.adjacent(0, "last")?.key()).toBe(2);
|
|
expect(registry.adjacent(0, "previous", true)?.key()).toBe(2);
|
|
});
|
|
|
|
it("isolates instances and disposes scope-owned registrations", () => {
|
|
const outer = useCollectionRegistry(),
|
|
inner = useCollectionRegistry();
|
|
const scope = effectScope();
|
|
scope.run(() => outer.register(member("same").member));
|
|
inner.register(member("same").member);
|
|
expect(outer.ordered()).toHaveLength(1);
|
|
scope.stop();
|
|
expect(outer.ordered()).toEqual([]);
|
|
expect(inner.ordered()).toHaveLength(1);
|
|
});
|
|
|
|
it("works without elements and retains registration order", () => {
|
|
const registry = useCollectionRegistry();
|
|
const element = ref<HTMLElement>();
|
|
registry.register({
|
|
key: () => 0,
|
|
element: () => element.value,
|
|
textValue: () => "Zero",
|
|
disabled: () => false,
|
|
});
|
|
expect(registry.adjacent(undefined, "next")?.key()).toBe(0);
|
|
expect(registry.range(0, 0)).toEqual([0]);
|
|
});
|
|
});
|