Files
heroui-vue/tests/collection-foundation.test.ts
T

181 lines
6.2 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { flushPromises, mount } from "@vue/test-utils";
import { defineComponent, h } from "vue";
import Foundation from "../packages/vue/tests/fixtures/CollectionFoundation.vue";
import SelectFoundation from "../packages/vue/tests/fixtures/SelectFoundation.vue";
const wrappers: ReturnType<typeof mount>[] = [];
const originalScroll = Object.getOwnPropertyDescriptor(
HTMLElement.prototype,
"scrollIntoView",
);
beforeEach(() => {
// Only focus/selection, no geometry claims.
Object.defineProperty(HTMLElement.prototype, "scrollIntoView", {
configurable: true,
value: vi.fn(),
});
vi.stubGlobal(
"ResizeObserver",
class {
observe() {}
unobserve() {}
disconnect() {}
},
);
});
afterEach(() => {
wrappers.splice(0).forEach((wrapper) => wrapper.unmount());
document.body.innerHTML = "";
vi.restoreAllMocks();
vi.unstubAllGlobals();
if (originalScroll)
Object.defineProperty(
HTMLElement.prototype,
"scrollIntoView",
originalScroll,
);
else Reflect.deleteProperty(HTMLElement.prototype, "scrollIntoView");
});
describe("installed Reka Select audit — not public component acceptance", () => {
function form(props: Record<string, unknown>) {
const wrapper = mount(
defineComponent({
render: () => h("form", {}, [h(SelectFoundation, props)]),
}),
{ attachTo: document.body },
);
wrappers.push(wrapper);
return wrapper;
}
it("records empty multiple FormData despite two selected trigger labels", async () => {
const wrapper = form({
multiple: true,
defaultValue: [0, "0"],
name: "choice",
});
await flushPromises();
expect([...new FormData(wrapper.get("form").element)]).toEqual([]);
const options = wrapper.get("select").findAll("option");
expect(options).toHaveLength(3);
expect(
options.slice(0, 2).map((option) => option.attributes("value")),
).toEqual(["0", "0"]);
expect(wrapper.get("select").element.selectedOptions).toHaveLength(0);
expect(wrapper.get('[role="combobox"]').text()).toBe(
"Numeric zero, String zero",
);
expect(wrapper.get('[role="combobox"]').attributes("aria-expanded")).toBe(
"false",
);
});
it("omits disabled whole-field values from the native submission layer", async () => {
const wrapper = form({ defaultValue: 0, disabled: true, name: "choice" });
await flushPromises();
expect([...new FormData(wrapper.get("form").element)]).toEqual([]);
expect(
wrapper.get('[role="combobox"]').attributes("disabled"),
).toBeDefined();
});
it("keeps selected content in the closed trigger after mounting", async () => {
const wrapper = form({ defaultValue: 0, name: "choice" });
await flushPromises();
expect(wrapper.get('[role="combobox"]').text()).toBe("Numeric zero");
expect(new FormData(wrapper.get("form").element).get("choice")).toBe("0");
});
});
function fixture(props: Record<string, unknown> = {}) {
const wrapper = mount(Foundation, { props, attachTo: document.body });
wrappers.push(wrapper);
return wrapper;
}
describe("installed Reka Listbox audit — not public component acceptance", () => {
it("distinguishes numeric and string zero and toggles multiple values", async () => {
const wrapper = fixture({ multiple: true, defaultValue: [0] });
await flushPromises();
await wrapper.findAll('[role="option"]')[1].trigger("click");
expect(
wrapper
.findAll('[role="option"]')
.map((item) => item.attributes("aria-selected")),
).toEqual(["true", "true", "false"]);
expect(wrapper.emitted("update:modelValue")).toEqual([[[0, "0"]]]);
});
it("keeps controlled rejection visible and emits repeated proposals", async () => {
const wrapper = fixture({ multiple: true, modelValue: [0] });
await flushPromises();
const item = wrapper.findAll('[role="option"]')[1];
await item.trigger("click");
await item.trigger("click");
expect(item.attributes("aria-selected")).toBe("false");
expect(wrapper.emitted("update:modelValue")).toEqual([
[[0, "0"]],
[[0, "0"]],
]);
});
it("skips disabled items for navigation and select-all", async () => {
const wrapper = fixture({ multiple: true });
await flushPromises();
const listbox = wrapper.get('[role="listbox"]');
await listbox.trigger("keydown", { key: "End" });
expect(document.activeElement).toBe(
wrapper.findAll('[role="option"]')[1].element,
);
await listbox.trigger("keydown", { key: "a", ctrlKey: true });
expect(wrapper.emitted("update:modelValue")).toEqual([[[0, "0"]]]);
expect(
wrapper.findAll('[role="option"]')[2].attributes("aria-selected"),
).toBe("false");
});
it("serializes indexed names and does not restore selection on native reset", async () => {
const wrapper = mount(
defineComponent({
render: () =>
h("form", {}, [
h(Foundation, {
multiple: true,
defaultValue: [0],
name: "choices",
}),
]),
}),
{ attachTo: document.body },
);
wrappers.push(wrapper);
await flushPromises();
await wrapper.findAll('[role="option"]')[1].trigger("click");
const form = wrapper.get("form").element;
expect([...new FormData(form)]).toEqual([
["choices[0]", "0"],
["choices[1]", "0"],
]);
form.reset();
await flushPromises();
expect(
wrapper.findAll('[role="option"]')[1].attributes("aria-selected"),
).toBe("true");
expect([...new FormData(form)]).toHaveLength(2);
});
it("allows cancelling item select before a value change", async () => {
// Raw item event is tested in the fixture through the actual DOM CustomEvent.
const wrapper = fixture({ multiple: true });
await flushPromises();
const option = wrapper.findAll('[role="option"]')[0];
option.element.addEventListener("listbox.select", (event) =>
event.preventDefault(),
);
await option.trigger("click");
expect(option.attributes("aria-selected")).toBe("false");
expect(wrapper.emitted("update:modelValue")).toBeUndefined();
});
});