fix: preserve ColorSwatch asChild ids and merged styles

This commit is contained in:
maoyingjie
2026-10-26 13:38:15 +08:00
parent 270dea5cff
commit b18949c071
6 changed files with 167 additions and 35 deletions
@@ -0,0 +1,51 @@
import { afterEach, expect, it } from "vitest";
import { mount } from "@vue/test-utils";
import { defineComponent, h, ref, nextTick } from "vue";
import { ColorSwatch } from "../../../packages/vue/src";
const wrappers: ReturnType<typeof mount>[] = [];
afterEach(() => {
wrappers.splice(0).forEach((w) => w.unmount());
document.body.innerHTML = "";
});
it("keeps asChild self-labelledby attached to the actual root ID", async () => {
const id = ref("child-id");
const w = mount(
defineComponent({
setup: () => () =>
h("div", [
h("span", { id: "caption" }, "Brand"),
h(
ColorSwatch,
{ asChild: true, color: "#0485F7", "aria-labelledby": "caption" },
{
default: () => h("div", { id: id.value }),
},
),
]),
}),
{ attachTo: document.body },
);
wrappers.push(w);
const root = w.get("[data-slot=color-swatch]");
expect(root.attributes("aria-labelledby")).toBe(
`${root.attributes("id")} caption`,
);
id.value = "next-child-id";
await nextTick();
expect(root.attributes("aria-labelledby")).toBe(
`${root.attributes("id")} caption`,
);
});
it("retains ordinary template styles together with styleFn", () => {
const w = mount(
defineComponent({
components: { ColorSwatch },
template: `<ColorSwatch color="#0485F7" :style="{ outline: '2px solid red' }" :style-fn="() => ({ opacity: 0.5 })" />`,
}),
);
wrappers.push(w);
const style = (w.element as HTMLElement).style;
expect(style.outline).toBe("2px solid red");
expect(style.opacity).toBe("0.5");
});