Files
heroui-vue/tests/regressions/color/swatch-composition.test.ts
T
2026-10-26 13:43:39 +08:00

60 lines
1.9 KiB
TypeScript

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<string | undefined>("child-id");
const childRef = ref<HTMLElement | null>(null);
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, ref: childRef }),
},
),
]),
}),
{ 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`,
);
id.value = undefined;
await nextTick();
expect(root.attributes("id")).toBeTruthy();
expect(childRef.value).toBe(root.element);
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");
});