1122 lines
37 KiB
TypeScript
1122 lines
37 KiB
TypeScript
import { describe, it, expect, afterEach, vi } from "vitest";
|
||
import { mount } from "@vue/test-utils";
|
||
import { defineComponent, h, nextTick, ref } from "vue";
|
||
import {
|
||
Button,
|
||
Accordion,
|
||
AccordionBody,
|
||
AccordionHeading,
|
||
AccordionIndicator,
|
||
AccordionItem,
|
||
AccordionPanel,
|
||
AccordionTrigger,
|
||
Disclosure,
|
||
DisclosureBody,
|
||
DisclosureContent,
|
||
DisclosureGroup,
|
||
DisclosureHeading,
|
||
DisclosureIndicator,
|
||
DisclosureTrigger,
|
||
useDisclosureGroupNavigation,
|
||
} from "../packages/vue/src";
|
||
|
||
afterEach(() => {
|
||
document.body.innerHTML = "";
|
||
});
|
||
|
||
describe("Disclosure", () => {
|
||
it("toggles expanded state with real ARIA and content visibility", async () => {
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
Disclosure,
|
||
DisclosureHeading,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
DisclosureBody,
|
||
DisclosureIndicator,
|
||
},
|
||
template: `
|
||
<Disclosure>
|
||
<DisclosureHeading>
|
||
<DisclosureTrigger>
|
||
标题
|
||
<DisclosureIndicator />
|
||
</DisclosureTrigger>
|
||
</DisclosureHeading>
|
||
<DisclosureContent>
|
||
<DisclosureBody>内容</DisclosureBody>
|
||
</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
const trigger = w.get("[data-slot=disclosure-trigger]");
|
||
const content = w.get("[data-slot=disclosure-content]");
|
||
expect(trigger.attributes("type")).toBe("button");
|
||
expect(trigger.attributes("aria-expanded")).toBe("false");
|
||
expect(content.attributes("aria-hidden")).toBe("true");
|
||
|
||
await trigger.trigger("click");
|
||
expect(trigger.attributes("aria-expanded")).toBe("true");
|
||
expect(content.attributes("aria-hidden")).toBe("false");
|
||
expect(content.attributes("data-expanded")).toBe("true");
|
||
expect(
|
||
w.get("[data-slot=disclosure-indicator]").attributes("data-expanded"),
|
||
).toBe("true");
|
||
|
||
await trigger.trigger("click");
|
||
expect(trigger.attributes("aria-expanded")).toBe("false");
|
||
expect(content.attributes("aria-hidden")).toBe("true");
|
||
});
|
||
|
||
it("supports v-model:expanded and defaultExpanded", async () => {
|
||
const expanded = ref(false);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
setup: () => ({ expanded }),
|
||
template: `
|
||
<Disclosure v-model:expanded="expanded">
|
||
<DisclosureTrigger data-testid="t">x</DisclosureTrigger>
|
||
<DisclosureContent>body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
expect(expanded.value).toBe(false);
|
||
await w.get("[data-testid=t]").trigger("click");
|
||
expect(expanded.value).toBe(true);
|
||
expect(
|
||
w.get("[data-slot=disclosure-trigger]").attributes("aria-expanded"),
|
||
).toBe("true");
|
||
|
||
const uncontrolled = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
template: `
|
||
<Disclosure default-expanded>
|
||
<DisclosureTrigger data-testid="t">x</DisclosureTrigger>
|
||
<DisclosureContent>body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
expect(
|
||
uncontrolled
|
||
.get("[data-slot=disclosure-trigger]")
|
||
.attributes("aria-expanded"),
|
||
).toBe("true");
|
||
});
|
||
|
||
it("blocks toggle when disabled and allows preventDefault cancel", async () => {
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
template: `
|
||
<Disclosure is-disabled>
|
||
<DisclosureTrigger data-testid="t">x</DisclosureTrigger>
|
||
<DisclosureContent>body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=t]").trigger("click");
|
||
expect(
|
||
w.get("[data-slot=disclosure-trigger]").attributes("aria-expanded"),
|
||
).toBe("false");
|
||
|
||
const click = vi.fn((e: MouseEvent) => e.preventDefault());
|
||
const cancelable = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
template: `
|
||
<Disclosure>
|
||
<DisclosureTrigger data-testid="t" @click="onClick">x</DisclosureTrigger>
|
||
<DisclosureContent>body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
methods: { onClick: click },
|
||
}),
|
||
);
|
||
await cancelable.get("[data-testid=t]").trigger("click");
|
||
expect(click).toHaveBeenCalled();
|
||
expect(
|
||
cancelable
|
||
.get("[data-slot=disclosure-trigger]")
|
||
.attributes("aria-expanded"),
|
||
).toBe("false");
|
||
});
|
||
|
||
it("associates trigger and panel ids uniquely and resolves ARIA", () => {
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
template: `
|
||
<div>
|
||
<Disclosure id="a">
|
||
<DisclosureTrigger data-testid="t1">x</DisclosureTrigger>
|
||
<DisclosureContent>c1</DisclosureContent>
|
||
</Disclosure>
|
||
<Disclosure id="b">
|
||
<DisclosureTrigger data-testid="t2">y</DisclosureTrigger>
|
||
<DisclosureContent>c2</DisclosureContent>
|
||
</Disclosure>
|
||
</div>
|
||
`,
|
||
}),
|
||
);
|
||
const t1 = w.get("[data-testid=t1]");
|
||
const t2 = w.get("[data-testid=t2]");
|
||
const c1 = w.get("#a-content");
|
||
const c2 = w.get("#b-content");
|
||
expect(t1.attributes("aria-controls")).toBe("a-content");
|
||
expect(t2.attributes("aria-controls")).toBe("b-content");
|
||
expect(w.find("#a-content").exists()).toBe(true);
|
||
expect(w.find("#b-content").exists()).toBe(true);
|
||
expect(t1.attributes("id")).not.toBe(t2.attributes("id"));
|
||
});
|
||
|
||
it("keeps explicit ARIA and dynamic attrs without dropping keys", async () => {
|
||
const label = ref<string | undefined>(undefined);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger },
|
||
setup: () => ({ label }),
|
||
template: `
|
||
<Disclosure :aria-label="label" data-custom="keep">
|
||
<DisclosureTrigger data-testid="t">x</DisclosureTrigger>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
const root = w.get("[data-slot=disclosure]");
|
||
expect(root.attributes("data-custom")).toBe("keep");
|
||
expect(root.attributes("aria-label")).toBeUndefined();
|
||
label.value = "外部名称";
|
||
await nextTick();
|
||
expect(root.attributes("aria-label")).toBe("外部名称");
|
||
label.value = undefined;
|
||
await nextTick();
|
||
expect(root.attributes("aria-label")).toBeUndefined();
|
||
});
|
||
});
|
||
|
||
describe("DisclosureGroup", () => {
|
||
it("single mode replaces keys and supports empty selection", async () => {
|
||
const keys = ref<Array<string | number>>(["a"]);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
DisclosureGroup,
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
},
|
||
setup: () => ({ keys }),
|
||
template: `
|
||
<DisclosureGroup v-model:expanded-keys="keys">
|
||
<Disclosure id="a"><DisclosureTrigger data-testid="ta">A</DisclosureTrigger><DisclosureContent>ca</DisclosureContent></Disclosure>
|
||
<Disclosure id="b"><DisclosureTrigger data-testid="tb">B</DisclosureTrigger><DisclosureContent>cb</DisclosureContent></Disclosure>
|
||
</DisclosureGroup>
|
||
`,
|
||
}),
|
||
);
|
||
expect(w.get("[data-testid=ta]").attributes("aria-expanded")).toBe("true");
|
||
await w.get("[data-testid=tb]").trigger("click");
|
||
expect(keys.value).toEqual(["b"]);
|
||
await w.get("[data-testid=tb]").trigger("click");
|
||
expect(keys.value).toEqual([]);
|
||
});
|
||
|
||
it("multiple mode adds and removes without mutating caller array", async () => {
|
||
const initial = ["a"];
|
||
const keys = ref<Array<string | number>>([...initial]);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
DisclosureGroup,
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
},
|
||
setup: () => ({ keys }),
|
||
template: `
|
||
<DisclosureGroup v-model:expanded-keys="keys" allows-multiple-expanded>
|
||
<Disclosure id="a"><DisclosureTrigger data-testid="ta">A</DisclosureTrigger><DisclosureContent>ca</DisclosureContent></Disclosure>
|
||
<Disclosure id="b"><DisclosureTrigger data-testid="tb">B</DisclosureTrigger><DisclosureContent>cb</DisclosureContent></Disclosure>
|
||
</DisclosureGroup>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=tb]").trigger("click");
|
||
expect(keys.value).toEqual(["a", "b"]);
|
||
await w.get("[data-testid=ta]").trigger("click");
|
||
expect(keys.value).toEqual(["b"]);
|
||
expect(initial).toEqual(["a"]);
|
||
});
|
||
|
||
it("keeps numeric and string keys distinct", async () => {
|
||
const keys = ref<Array<string | number>>([1]);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
DisclosureGroup,
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
},
|
||
setup: () => ({ keys }),
|
||
template: `
|
||
<DisclosureGroup v-model:expanded-keys="keys" allows-multiple-expanded>
|
||
<Disclosure :id="1"><DisclosureTrigger data-testid="n1">n</DisclosureTrigger><DisclosureContent>1</DisclosureContent></Disclosure>
|
||
<Disclosure id="1"><DisclosureTrigger data-testid="s1">s</DisclosureTrigger><DisclosureContent>s</DisclosureContent></Disclosure>
|
||
</DisclosureGroup>
|
||
`,
|
||
}),
|
||
);
|
||
expect(w.get("[data-testid=n1]").attributes("aria-expanded")).toBe("true");
|
||
expect(w.get("[data-testid=s1]").attributes("aria-expanded")).toBe("false");
|
||
});
|
||
|
||
it("group disable blocks items and can be inherited from Fieldset-like scope", async () => {
|
||
const disabled = ref(true);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
DisclosureGroup,
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
},
|
||
setup: () => ({ disabled }),
|
||
template: `
|
||
<DisclosureGroup :is-disabled="disabled">
|
||
<Disclosure id="a"><DisclosureTrigger data-testid="ta">A</DisclosureTrigger><DisclosureContent>ca</DisclosureContent></Disclosure>
|
||
</DisclosureGroup>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=ta]").trigger("click");
|
||
expect(w.get("[data-testid=ta]").attributes("aria-expanded")).toBe("false");
|
||
disabled.value = false;
|
||
await nextTick();
|
||
await w.get("[data-testid=ta]").trigger("click");
|
||
expect(w.get("[data-testid=ta]").attributes("aria-expanded")).toBe("true");
|
||
});
|
||
});
|
||
|
||
describe("useDisclosureGroupNavigation", () => {
|
||
it("handles empty list, boundaries, single replace and multiple add", () => {
|
||
const empty = useDisclosureGroupNavigation({
|
||
expandedKeys: [],
|
||
itemIds: [],
|
||
onExpandedChange: () => {},
|
||
});
|
||
expect(empty.currentIndex).toBe(-1);
|
||
expect(empty.isPrevDisabled).toBe(true);
|
||
expect(empty.isNextDisabled).toBe(true);
|
||
empty.onPrevious();
|
||
empty.onNext();
|
||
|
||
const keys = ref<Array<string | number>>(["a"]);
|
||
const commits: Array<string | number>[] = [];
|
||
const nav = useDisclosureGroupNavigation({
|
||
expandedKeys: keys,
|
||
itemIds: ["a", "b", "c"],
|
||
onExpandedChange: (next) => {
|
||
commits.push(next);
|
||
keys.value = next;
|
||
},
|
||
});
|
||
expect(nav.currentIndex).toBe(0);
|
||
expect(nav.isPrevDisabled).toBe(true);
|
||
expect(nav.isNextDisabled).toBe(false);
|
||
nav.onPrevious();
|
||
expect(commits).toHaveLength(0);
|
||
nav.onNext();
|
||
expect(commits.at(-1)).toEqual(["b"]);
|
||
expect(nav.currentIndex).toBe(1);
|
||
nav.onNext();
|
||
expect(commits.at(-1)).toEqual(["c"]);
|
||
expect(nav.isNextDisabled).toBe(true);
|
||
|
||
const multiKeys = ref<Array<string | number>>(["a"]);
|
||
const multiCommits: Array<string | number>[] = [];
|
||
const multi = useDisclosureGroupNavigation({
|
||
expandedKeys: multiKeys,
|
||
itemIds: ["a", "b", "c"],
|
||
allowsMultipleExpanded: true,
|
||
onExpandedChange: (next) => {
|
||
multiCommits.push(next);
|
||
multiKeys.value = next;
|
||
},
|
||
});
|
||
multi.onNext();
|
||
expect(multiCommits.at(-1)).toEqual(["a", "b"]);
|
||
multi.onPrevious();
|
||
// previous from index 1 goes to index 0 which is already expanded — adds only if missing
|
||
expect(multiCommits.at(-1)).toEqual(["a", "b"]);
|
||
});
|
||
});
|
||
|
||
describe("Accordion", () => {
|
||
it("renders items with separators, hideSeparator and surface variant", async () => {
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
Accordion,
|
||
AccordionItem,
|
||
AccordionHeading,
|
||
AccordionTrigger,
|
||
AccordionIndicator,
|
||
AccordionPanel,
|
||
AccordionBody,
|
||
},
|
||
template: `
|
||
<Accordion hide-separator variant="surface">
|
||
<AccordionItem id="1">
|
||
<AccordionHeading>
|
||
<AccordionTrigger>一<AccordionIndicator /></AccordionTrigger>
|
||
</AccordionHeading>
|
||
<AccordionPanel><AccordionBody>内容一</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
</Accordion>
|
||
`,
|
||
}),
|
||
);
|
||
const root = w.get("[data-slot=accordion]");
|
||
expect(root.classes()).toContain("hv-accordion--surface");
|
||
const item = w.get("[data-slot=accordion-item]");
|
||
expect(item.attributes("data-hide-separator")).toBe("true");
|
||
expect(w.get("[data-slot=accordion-trigger]").attributes("type")).toBe(
|
||
"button",
|
||
);
|
||
expect(w.find("[data-slot=accordion-panel]").exists()).toBe(true);
|
||
});
|
||
|
||
it("supports multiple expansion and controlled keys", async () => {
|
||
const keys = ref<Array<string | number>>([]);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
Accordion,
|
||
AccordionItem,
|
||
AccordionHeading,
|
||
AccordionTrigger,
|
||
AccordionIndicator,
|
||
AccordionPanel,
|
||
AccordionBody,
|
||
},
|
||
setup: () => ({ keys }),
|
||
template: `
|
||
<Accordion v-model:expanded-keys="keys" allows-multiple-expanded>
|
||
<AccordionItem id="a">
|
||
<AccordionHeading><AccordionTrigger data-testid="ta">A<AccordionIndicator /></AccordionTrigger></AccordionHeading>
|
||
<AccordionPanel><AccordionBody>ca</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
<AccordionItem id="b">
|
||
<AccordionHeading><AccordionTrigger data-testid="tb">B<AccordionIndicator /></AccordionTrigger></AccordionHeading>
|
||
<AccordionPanel><AccordionBody>cb</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
</Accordion>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=ta]").trigger("click");
|
||
await w.get("[data-testid=tb]").trigger("click");
|
||
expect(keys.value).toEqual(["a", "b"]);
|
||
expect(w.get("[data-testid=ta]").attributes("aria-expanded")).toBe("true");
|
||
expect(w.get("[data-testid=tb]").attributes("aria-expanded")).toBe("true");
|
||
});
|
||
|
||
it("disables whole accordion and individual items", async () => {
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
Accordion,
|
||
AccordionItem,
|
||
AccordionHeading,
|
||
AccordionTrigger,
|
||
AccordionIndicator,
|
||
AccordionPanel,
|
||
AccordionBody,
|
||
},
|
||
template: `
|
||
<div>
|
||
<Accordion is-disabled>
|
||
<AccordionItem id="a">
|
||
<AccordionHeading><AccordionTrigger data-testid="ga">A</AccordionTrigger></AccordionHeading>
|
||
<AccordionPanel><AccordionBody>x</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
</Accordion>
|
||
<Accordion>
|
||
<AccordionItem id="b" is-disabled>
|
||
<AccordionHeading><AccordionTrigger data-testid="ib">B</AccordionTrigger></AccordionHeading>
|
||
<AccordionPanel><AccordionBody>y</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
</Accordion>
|
||
</div>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=ga]").trigger("click");
|
||
await w.get("[data-testid=ib]").trigger("click");
|
||
expect(w.get("[data-testid=ga]").attributes("aria-expanded")).toBe("false");
|
||
expect(w.get("[data-testid=ib]").attributes("aria-expanded")).toBe("false");
|
||
expect(w.get("[data-testid=ib]").attributes("disabled")).toBeDefined();
|
||
});
|
||
});
|
||
|
||
describe("M21–M23 demos", () => {
|
||
const demoModules = import.meta.glob<{ default: unknown }>(
|
||
"../apps/docs/demos/{disclosure,disclosure-group,accordion}-*.vue",
|
||
{ eager: true },
|
||
);
|
||
|
||
it("mounts all 16 baseline demos without console errors", () => {
|
||
const names = Object.keys(demoModules)
|
||
.map((n) => n.split("/").pop())
|
||
.sort();
|
||
expect(names).toHaveLength(16);
|
||
expect(names).toEqual([
|
||
"accordion-basic.vue",
|
||
"accordion-controlled.vue",
|
||
"accordion-custom-indicator.vue",
|
||
"accordion-custom-styles.vue",
|
||
"accordion-disabled.vue",
|
||
"accordion-faq.vue",
|
||
"accordion-multiple.vue",
|
||
"accordion-render-function.vue",
|
||
"accordion-surface.vue",
|
||
"accordion-without-separator.vue",
|
||
"disclosure-basic.vue",
|
||
"disclosure-custom-styles.vue",
|
||
"disclosure-group-basic.vue",
|
||
"disclosure-group-controlled.vue",
|
||
"disclosure-group-custom-styles.vue",
|
||
"disclosure-render-function.vue",
|
||
]);
|
||
|
||
const errors: unknown[] = [];
|
||
const warn = console.warn;
|
||
const error = console.error;
|
||
console.warn = (...args: unknown[]) => {
|
||
errors.push(args);
|
||
warn(...args);
|
||
};
|
||
console.error = (...args: unknown[]) => {
|
||
errors.push(args);
|
||
error(...args);
|
||
};
|
||
try {
|
||
for (const mod of Object.values(demoModules)) {
|
||
const w = mount(mod.default as never, { attachTo: document.body });
|
||
expect(w.html().length).toBeGreaterThan(20);
|
||
w.unmount();
|
||
document.body.innerHTML = "";
|
||
}
|
||
} finally {
|
||
console.warn = warn;
|
||
console.error = error;
|
||
}
|
||
expect(errors).toEqual([]);
|
||
});
|
||
});
|
||
|
||
describe("M21–M23 review regressions", () => {
|
||
it("enforces single-expand and group disable for members without id", async () => {
|
||
const keys = ref<Array<string | number>>([]);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
Accordion,
|
||
AccordionItem,
|
||
AccordionHeading,
|
||
AccordionTrigger,
|
||
AccordionPanel,
|
||
AccordionBody,
|
||
AccordionIndicator,
|
||
},
|
||
setup: () => ({ keys }),
|
||
template: `
|
||
<Accordion v-model:expanded-keys="keys">
|
||
<AccordionItem>
|
||
<AccordionHeading><AccordionTrigger data-testid="t1">一<AccordionIndicator /></AccordionTrigger></AccordionHeading>
|
||
<AccordionPanel><AccordionBody>a</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
<AccordionItem>
|
||
<AccordionHeading><AccordionTrigger data-testid="t2">二<AccordionIndicator /></AccordionTrigger></AccordionHeading>
|
||
<AccordionPanel><AccordionBody>b</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
</Accordion>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=t1]").trigger("click");
|
||
await w.get("[data-testid=t2]").trigger("click");
|
||
const a = w.get("[data-testid=t1]").attributes("aria-expanded");
|
||
const b = w.get("[data-testid=t2]").attributes("aria-expanded");
|
||
expect(a).toBe("false");
|
||
expect(b).toBe("true");
|
||
|
||
const disabled = mount(
|
||
defineComponent({
|
||
components: {
|
||
Accordion,
|
||
AccordionItem,
|
||
AccordionHeading,
|
||
AccordionTrigger,
|
||
AccordionPanel,
|
||
AccordionBody,
|
||
AccordionIndicator,
|
||
},
|
||
template: `
|
||
<Accordion is-disabled>
|
||
<AccordionItem>
|
||
<AccordionHeading><AccordionTrigger data-testid="d1">x<AccordionIndicator /></AccordionTrigger></AccordionHeading>
|
||
<AccordionPanel><AccordionBody>x</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
</Accordion>
|
||
`,
|
||
}),
|
||
);
|
||
await disabled.get("[data-testid=d1]").trigger("click");
|
||
expect(disabled.get("[data-testid=d1]").attributes("aria-expanded")).toBe(
|
||
"false",
|
||
);
|
||
expect(
|
||
disabled.get("[data-testid=d1]").attributes("disabled"),
|
||
).toBeDefined();
|
||
});
|
||
|
||
it("keeps DOM ids unique for number 1 and string '1'", async () => {
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
DisclosureGroup,
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
},
|
||
template: `
|
||
<DisclosureGroup allows-multiple-expanded>
|
||
<Disclosure :id="1"><DisclosureTrigger data-testid="n">n</DisclosureTrigger><DisclosureContent>c1</DisclosureContent></Disclosure>
|
||
<Disclosure id="1"><DisclosureTrigger data-testid="s">s</DisclosureTrigger><DisclosureContent>c2</DisclosureContent></Disclosure>
|
||
</DisclosureGroup>
|
||
`,
|
||
}),
|
||
);
|
||
const n = w.get("[data-testid=n]");
|
||
const s = w.get("[data-testid=s]");
|
||
expect(n.attributes("id")).toBeTruthy();
|
||
expect(s.attributes("id")).toBeTruthy();
|
||
expect(n.attributes("id")).not.toBe(s.attributes("id"));
|
||
expect(n.attributes("aria-controls")).not.toBe(
|
||
s.attributes("aria-controls"),
|
||
);
|
||
const nPanel = n.attributes("aria-controls")!;
|
||
const sPanel = s.attributes("aria-controls")!;
|
||
expect(w.find(`[id="${nPanel}"]`).exists()).toBe(true);
|
||
expect(w.find(`[id="${sPanel}"]`).exists()).toBe(true);
|
||
expect(nPanel).not.toBe(sPanel);
|
||
});
|
||
|
||
it("emits requested next expanded state in controlled groups", async () => {
|
||
const events: boolean[] = [];
|
||
const keys = ref<Array<string | number>>([]);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
DisclosureGroup,
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
},
|
||
setup: () => ({ keys, events }),
|
||
template: `
|
||
<DisclosureGroup :expanded-keys="keys" @update:expanded-keys="keys = $event">
|
||
<Disclosure id="a" @expanded-change="events.push($event)">
|
||
<DisclosureTrigger data-testid="ta">A</DisclosureTrigger>
|
||
<DisclosureContent>ca</DisclosureContent>
|
||
</Disclosure>
|
||
</DisclosureGroup>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=ta]").trigger("click");
|
||
expect(events.at(-1)).toBe(true);
|
||
expect(keys.value).toEqual(["a"]);
|
||
await w.get("[data-testid=ta]").trigger("click");
|
||
expect(events.at(-1)).toBe(false);
|
||
expect(keys.value).toEqual([]);
|
||
});
|
||
|
||
it("does not run asChild child click when disabled", async () => {
|
||
const childClick = vi.fn();
|
||
const emitClick = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
Button,
|
||
},
|
||
setup: () => ({ childClick, emitClick }),
|
||
template: `
|
||
<Disclosure is-disabled>
|
||
<DisclosureTrigger as-child @click="emitClick">
|
||
<Button data-testid="btn" @click="childClick">点我</Button>
|
||
</DisclosureTrigger>
|
||
<DisclosureContent>body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=btn]").trigger("click");
|
||
expect(childClick).not.toHaveBeenCalled();
|
||
expect(emitClick).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("ignores stale close-animation callbacks after reopen", async () => {
|
||
let resolveClose!: () => void;
|
||
const closeFinished = new Promise<void>((resolve) => {
|
||
resolveClose = resolve;
|
||
});
|
||
// Stub getAnimations so close waits on our promise.
|
||
const proto = HTMLElement.prototype as HTMLElement & {
|
||
getAnimations?: () => Animation[];
|
||
};
|
||
const original = proto.getAnimations;
|
||
let mode: "close" | "open" = "close";
|
||
proto.getAnimations = function getAnimations() {
|
||
const finished =
|
||
mode === "close"
|
||
? (closeFinished as Promise<unknown>)
|
||
: Promise.resolve();
|
||
return [{ finished } as unknown as Animation];
|
||
};
|
||
|
||
try {
|
||
const expanded = ref(true);
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
setup: () => ({ expanded }),
|
||
template: `
|
||
<Disclosure v-model:expanded="expanded">
|
||
<DisclosureTrigger data-testid="t">x</DisclosureTrigger>
|
||
<DisclosureContent data-testid="panel">body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
{ attachTo: document.body },
|
||
);
|
||
await nextTick();
|
||
// close
|
||
mode = "close";
|
||
await w.get("[data-testid=t]").trigger("click");
|
||
await nextTick();
|
||
// reopen before close animation settles
|
||
mode = "open";
|
||
await w.get("[data-testid=t]").trigger("click");
|
||
await nextTick();
|
||
// stale close promise rejects/settles
|
||
resolveClose();
|
||
await Promise.resolve();
|
||
await Promise.resolve();
|
||
await nextTick();
|
||
const panel = w.get("[data-testid=panel]");
|
||
expect(panel.attributes("aria-hidden")).toBe("false");
|
||
expect(panel.attributes("hidden")).toBeUndefined();
|
||
} finally {
|
||
if (original) proto.getAnimations = original;
|
||
else {
|
||
(proto as { getAnimations?: unknown }).getAnimations = undefined;
|
||
}
|
||
}
|
||
});
|
||
|
||
it("renders DisclosureContent and AccordionPanel/Body with as", () => {
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureContent },
|
||
template: `
|
||
<Disclosure default-expanded>
|
||
<DisclosureContent as="section" data-custom="panel">body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
const panel = w.get("[data-slot=disclosure-content]");
|
||
expect(panel.element.tagName).toBe("SECTION");
|
||
expect(panel.attributes("data-custom")).toBe("panel");
|
||
|
||
const acc = mount(
|
||
defineComponent({
|
||
components: {
|
||
Accordion,
|
||
AccordionItem,
|
||
AccordionPanel,
|
||
AccordionBody,
|
||
},
|
||
template: `
|
||
<Accordion>
|
||
<AccordionItem default-expanded>
|
||
<AccordionPanel as="section">
|
||
<AccordionBody as="article">text</AccordionBody>
|
||
</AccordionPanel>
|
||
</AccordionItem>
|
||
</Accordion>
|
||
`,
|
||
}),
|
||
);
|
||
expect(acc.get("[data-slot=accordion-panel]").element.tagName).toBe(
|
||
"SECTION",
|
||
);
|
||
expect(acc.get("[data-slot=accordion-body]").element.tagName).toBe(
|
||
"ARTICLE",
|
||
);
|
||
});
|
||
});
|
||
|
||
describe("M21–M23 asChild trigger regressions", () => {
|
||
it("runs child click exactly once per activation (Disclosure + Accordion)", async () => {
|
||
for (const family of ["disclosure", "accordion"] as const) {
|
||
const childClick = vi.fn();
|
||
const rootClick = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
Accordion,
|
||
AccordionItem,
|
||
AccordionTrigger,
|
||
AccordionPanel,
|
||
AccordionBody,
|
||
Button,
|
||
},
|
||
setup: () => ({ childClick, rootClick }),
|
||
template:
|
||
family === "disclosure"
|
||
? `
|
||
<Disclosure>
|
||
<DisclosureTrigger as-child @click="rootClick">
|
||
<Button data-testid="btn" @click="childClick">open</Button>
|
||
</DisclosureTrigger>
|
||
<DisclosureContent>body</DisclosureContent>
|
||
</Disclosure>
|
||
`
|
||
: `
|
||
<Accordion>
|
||
<AccordionItem>
|
||
<AccordionTrigger as-child @click="rootClick">
|
||
<Button data-testid="btn" @click="childClick">open</Button>
|
||
</AccordionTrigger>
|
||
<AccordionPanel><AccordionBody>body</AccordionBody></AccordionPanel>
|
||
</AccordionItem>
|
||
</Accordion>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=btn]").trigger("click");
|
||
expect(childClick, family).toHaveBeenCalledTimes(1);
|
||
expect(rootClick, family).toHaveBeenCalledTimes(1);
|
||
w.unmount();
|
||
}
|
||
});
|
||
|
||
it("does not run child click.capture when disabled", async () => {
|
||
const capture = vi.fn();
|
||
const click = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
setup: () => ({ capture, click }),
|
||
template: `
|
||
<Disclosure is-disabled>
|
||
<DisclosureTrigger as-child @click="click">
|
||
<span data-testid="btn" @click.capture="capture" @click="() => {}">open</span>
|
||
</DisclosureTrigger>
|
||
<DisclosureContent>body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
await w.get("[data-testid=btn]").trigger("click");
|
||
expect(capture).not.toHaveBeenCalled();
|
||
expect(click).not.toHaveBeenCalled();
|
||
expect(
|
||
w
|
||
.get("[data-slot=disclosure-trigger], [data-testid=btn]")
|
||
.attributes("aria-expanded"),
|
||
).toBe("false");
|
||
});
|
||
|
||
it("keeps the effective button when the slot starts with v-if comments", async () => {
|
||
const click = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: {
|
||
Disclosure,
|
||
DisclosureTrigger,
|
||
DisclosureContent,
|
||
Button,
|
||
},
|
||
setup: () => ({ click }),
|
||
template: `
|
||
<Disclosure>
|
||
<DisclosureTrigger as-child @click="click">
|
||
<span v-if="false">hidden</span>
|
||
<Button data-testid="btn">open</Button>
|
||
</DisclosureTrigger>
|
||
<DisclosureContent>body</DisclosureContent>
|
||
</Disclosure>
|
||
`,
|
||
}),
|
||
);
|
||
const btn = w.find("[data-testid=btn]");
|
||
expect(btn.exists()).toBe(true);
|
||
expect(btn.element.tagName).toBe("BUTTON");
|
||
expect(btn.attributes("aria-expanded")).toBe("false");
|
||
await btn.trigger("click");
|
||
expect(click).toHaveBeenCalledTimes(1);
|
||
expect(btn.attributes("aria-expanded")).toBe("true");
|
||
});
|
||
});
|
||
|
||
describe("M21–M23 event adaptation regressions", () => {
|
||
it("keeps same-node listeners and expand after child stopPropagation", async () => {
|
||
const second = vi.fn();
|
||
const triggerClick = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
setup: () => ({ second, triggerClick }),
|
||
render() {
|
||
return h("div", [
|
||
h(
|
||
Disclosure,
|
||
{},
|
||
{
|
||
default: () => [
|
||
h(
|
||
DisclosureTrigger,
|
||
{
|
||
asChild: true,
|
||
onClick: triggerClick,
|
||
},
|
||
{
|
||
default: () =>
|
||
h(
|
||
"button",
|
||
{
|
||
"data-testid": "btn",
|
||
type: "button",
|
||
onClick: [
|
||
(e: MouseEvent) => e.stopPropagation(),
|
||
second,
|
||
],
|
||
},
|
||
"open",
|
||
),
|
||
},
|
||
),
|
||
h(DisclosureContent, {}, { default: () => "body" }),
|
||
],
|
||
},
|
||
),
|
||
]);
|
||
},
|
||
}),
|
||
);
|
||
await w.get("[data-testid=btn]").trigger("click");
|
||
expect(second).toHaveBeenCalledTimes(1);
|
||
expect(triggerClick).toHaveBeenCalledTimes(1);
|
||
expect(w.get("[data-testid=btn]").attributes("aria-expanded")).toBe("true");
|
||
});
|
||
|
||
it("stopImmediatePropagation skips later listeners and expand", async () => {
|
||
const second = vi.fn();
|
||
const triggerClick = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
setup: () => ({ second, triggerClick }),
|
||
render() {
|
||
return h(
|
||
Disclosure,
|
||
{},
|
||
{
|
||
default: () => [
|
||
h(
|
||
DisclosureTrigger,
|
||
{ asChild: true, onClick: triggerClick },
|
||
{
|
||
default: () =>
|
||
h(
|
||
"button",
|
||
{
|
||
"data-testid": "btn",
|
||
type: "button",
|
||
onClick: [
|
||
(e: MouseEvent) => e.stopImmediatePropagation(),
|
||
second,
|
||
],
|
||
},
|
||
"open",
|
||
),
|
||
},
|
||
),
|
||
h(DisclosureContent, {}, { default: () => "body" }),
|
||
],
|
||
},
|
||
);
|
||
},
|
||
}),
|
||
);
|
||
await w.get("[data-testid=btn]").trigger("click");
|
||
expect(second).not.toHaveBeenCalled();
|
||
expect(triggerClick).not.toHaveBeenCalled();
|
||
expect(w.get("[data-testid=btn]").attributes("aria-expanded")).toBe(
|
||
"false",
|
||
);
|
||
});
|
||
|
||
it("preventDefault cancels expand but still runs same-node listeners", async () => {
|
||
const second = vi.fn();
|
||
const triggerClick = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
setup: () => ({ second, triggerClick }),
|
||
render() {
|
||
return h(
|
||
Disclosure,
|
||
{},
|
||
{
|
||
default: () => [
|
||
h(
|
||
DisclosureTrigger,
|
||
{ asChild: true, onClick: triggerClick },
|
||
{
|
||
default: () =>
|
||
h(
|
||
"button",
|
||
{
|
||
"data-testid": "btn",
|
||
type: "button",
|
||
onClick: [
|
||
(e: MouseEvent) => e.preventDefault(),
|
||
second,
|
||
],
|
||
},
|
||
"open",
|
||
),
|
||
},
|
||
),
|
||
h(DisclosureContent, {}, { default: () => "body" }),
|
||
],
|
||
},
|
||
);
|
||
},
|
||
}),
|
||
);
|
||
await w.get("[data-testid=btn]").trigger("click");
|
||
expect(second).toHaveBeenCalledTimes(1);
|
||
expect(triggerClick).toHaveBeenCalledTimes(1);
|
||
expect(w.get("[data-testid=btn]").attributes("aria-expanded")).toBe(
|
||
"false",
|
||
);
|
||
});
|
||
|
||
it("runs Trigger @click.capture with asChild alongside child capture", async () => {
|
||
const triggerCapture = vi.fn();
|
||
const childCapture = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
setup: () => ({ triggerCapture, childCapture }),
|
||
render() {
|
||
return h(
|
||
Disclosure,
|
||
{},
|
||
{
|
||
default: () => [
|
||
h(
|
||
DisclosureTrigger,
|
||
{
|
||
asChild: true,
|
||
onClickCapture: triggerCapture,
|
||
},
|
||
{
|
||
default: () =>
|
||
h(
|
||
"button",
|
||
{
|
||
"data-testid": "btn",
|
||
type: "button",
|
||
onClickCapture: childCapture,
|
||
},
|
||
"open",
|
||
),
|
||
},
|
||
),
|
||
h(DisclosureContent, {}, { default: () => "body" }),
|
||
],
|
||
},
|
||
);
|
||
},
|
||
}),
|
||
);
|
||
await w.get("[data-testid=btn]").trigger("click");
|
||
expect(triggerCapture).toHaveBeenCalledTimes(1);
|
||
expect(childCapture).toHaveBeenCalledTimes(1);
|
||
expect(w.get("[data-testid=btn]").attributes("aria-expanded")).toBe("true");
|
||
});
|
||
|
||
it("disabled asChild blocks Trigger and child capture listeners", async () => {
|
||
const triggerCapture = vi.fn();
|
||
const childCapture = vi.fn();
|
||
const w = mount(
|
||
defineComponent({
|
||
components: { Disclosure, DisclosureTrigger, DisclosureContent },
|
||
setup: () => ({ triggerCapture, childCapture }),
|
||
render() {
|
||
return h(
|
||
Disclosure,
|
||
{ isDisabled: true },
|
||
{
|
||
default: () => [
|
||
h(
|
||
DisclosureTrigger,
|
||
{ asChild: true, onClickCapture: triggerCapture },
|
||
{
|
||
default: () =>
|
||
h(
|
||
"button",
|
||
{
|
||
"data-testid": "btn",
|
||
type: "button",
|
||
onClickCapture: childCapture,
|
||
},
|
||
"open",
|
||
),
|
||
},
|
||
),
|
||
h(DisclosureContent, {}, { default: () => "body" }),
|
||
],
|
||
},
|
||
);
|
||
},
|
||
}),
|
||
);
|
||
await w.get("[data-testid=btn]").trigger("click");
|
||
expect(triggerCapture).not.toHaveBeenCalled();
|
||
expect(childCapture).not.toHaveBeenCalled();
|
||
});
|
||
});
|