feat: add ProgressBar ProgressCircle and Meter
This commit is contained in:
@@ -0,0 +1,843 @@
|
||||
import { describe, it, expect, afterEach, vi } from "vitest";
|
||||
import { renderToString } from "@vue/server-renderer";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { createSSRApp, defineComponent, h, nextTick, ref } from "vue";
|
||||
import {
|
||||
Label,
|
||||
TextField,
|
||||
Input,
|
||||
Meter,
|
||||
MeterFill,
|
||||
MeterOutput,
|
||||
MeterTrack,
|
||||
ProgressBar,
|
||||
ProgressBarFill,
|
||||
ProgressBarOutput,
|
||||
ProgressBarTrack,
|
||||
ProgressCircle,
|
||||
ProgressCircleFillCircle,
|
||||
ProgressCircleTrack,
|
||||
ProgressCircleTrackCircle,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
describe("ProgressBar", () => {
|
||||
it("renders with progressbar role and Label accessible name", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
Label,
|
||||
ProgressBar,
|
||||
ProgressBarOutput,
|
||||
ProgressBarTrack,
|
||||
ProgressBarFill,
|
||||
},
|
||||
template: `
|
||||
<ProgressBar :value="60">
|
||||
<Label>Loading</Label>
|
||||
<ProgressBarOutput />
|
||||
<ProgressBarTrack>
|
||||
<ProgressBarFill />
|
||||
</ProgressBarTrack>
|
||||
</ProgressBar>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
const root = w.get("[data-slot=progress-bar]");
|
||||
expect(root.attributes("role")).toBe("progressbar");
|
||||
expect(root.attributes("aria-valuenow")).toBe("60");
|
||||
expect(root.attributes("aria-valuemin")).toBe("0");
|
||||
expect(root.attributes("aria-valuemax")).toBe("100");
|
||||
await nextTick();
|
||||
const label = w.get("[data-slot=label]");
|
||||
expect(root.attributes("aria-labelledby")).toBe(label.attributes("id"));
|
||||
expect(label.attributes("id")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("exposes BEM block, color/size modifiers and data-slots", () => {
|
||||
const w = mount(ProgressBar, {
|
||||
props: { color: "success", size: "lg", value: 50 },
|
||||
attrs: { "aria-label": "Loading", "data-foo": "bar" },
|
||||
slots: {
|
||||
default: () => [
|
||||
h(ProgressBarOutput),
|
||||
h(ProgressBarTrack, null, { default: () => h(ProgressBarFill) }),
|
||||
],
|
||||
},
|
||||
});
|
||||
const root = w.get("[data-slot=progress-bar]");
|
||||
expect(root.attributes("data-foo")).toBe("bar");
|
||||
expect(root.attributes("aria-label")).toBe("Loading");
|
||||
expect(root.classes()).toContain("hv-progress-bar");
|
||||
expect(root.classes()).toContain("hv-progress-bar--success");
|
||||
expect(root.classes()).toContain("hv-progress-bar--lg");
|
||||
expect(w.find("[data-slot=progress-bar-output]").exists()).toBe(true);
|
||||
expect(w.find("[data-slot=progress-bar-track]").exists()).toBe(true);
|
||||
expect(w.find("[data-slot=progress-bar-fill]").exists()).toBe(true);
|
||||
});
|
||||
|
||||
it("omits aria-valuenow and width when indeterminate", () => {
|
||||
const w = mount(ProgressBar, {
|
||||
props: { isIndeterminate: true },
|
||||
attrs: { "aria-label": "Loading" },
|
||||
slots: {
|
||||
default: () =>
|
||||
h(ProgressBarTrack, null, {
|
||||
default: () => h(ProgressBarFill, { "data-testid": "fill" }),
|
||||
}),
|
||||
},
|
||||
});
|
||||
const root = w.get("[data-slot=progress-bar]");
|
||||
expect(root.attributes("aria-valuenow")).toBeUndefined();
|
||||
expect(root.attributes("aria-valuetext")).toBeUndefined();
|
||||
const fill = w.get("[data-slot=progress-bar-fill]");
|
||||
expect((fill.element as HTMLElement).style.width).toBe("");
|
||||
});
|
||||
|
||||
it("formats percent text and custom ranges", async () => {
|
||||
const percent = mount(ProgressBar, {
|
||||
props: { value: 60 },
|
||||
attrs: { "aria-label": "p" },
|
||||
slots: { default: () => h(ProgressBarOutput) },
|
||||
});
|
||||
expect(percent.get("[data-slot=progress-bar-output]").text()).toBe("60%");
|
||||
|
||||
const currency = mount(ProgressBar, {
|
||||
props: {
|
||||
value: 750,
|
||||
minValue: 0,
|
||||
maxValue: 1000,
|
||||
formatOptions: { style: "currency", currency: "USD" },
|
||||
},
|
||||
attrs: { "aria-label": "p" },
|
||||
slots: {
|
||||
default: () => [
|
||||
h(ProgressBarOutput),
|
||||
h(ProgressBarFill, { "data-testid": "fill" }),
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(currency.get("[data-slot=progress-bar-output]").text()).toBe(
|
||||
"$750.00",
|
||||
);
|
||||
expect(
|
||||
(currency.get("[data-slot=progress-bar-fill]").element as HTMLElement)
|
||||
.style.width,
|
||||
).toBe("75%");
|
||||
});
|
||||
|
||||
it("clamps value and supports valueLabel override", () => {
|
||||
const w = mount(ProgressBar, {
|
||||
props: { value: 150, minValue: 0, maxValue: 100, valueLabel: "自定义" },
|
||||
attrs: { "aria-label": "p" },
|
||||
slots: {
|
||||
default: () => [
|
||||
h(ProgressBarOutput),
|
||||
h(ProgressBarFill, { "data-testid": "fill" }),
|
||||
],
|
||||
},
|
||||
});
|
||||
const root = w.get("[data-slot=progress-bar]");
|
||||
expect(root.attributes("aria-valuenow")).toBe("100");
|
||||
expect(root.attributes("aria-valuetext")).toBe("自定义");
|
||||
expect(w.get("[data-slot=progress-bar-output]").text()).toBe("自定义");
|
||||
expect(
|
||||
(w.get("[data-slot=progress-bar-fill]").element as HTMLElement).style
|
||||
.width,
|
||||
).toBe("100%");
|
||||
});
|
||||
|
||||
it("updates DOM when value and indeterminate toggle", async () => {
|
||||
const value = ref(20);
|
||||
const indeterminate = ref(false);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
ProgressBar,
|
||||
ProgressBarOutput,
|
||||
ProgressBarTrack,
|
||||
ProgressBarFill,
|
||||
},
|
||||
setup: () => ({ value, indeterminate }),
|
||||
template: `
|
||||
<ProgressBar
|
||||
:value="value"
|
||||
:is-indeterminate="indeterminate"
|
||||
aria-label="动态"
|
||||
>
|
||||
<ProgressBarOutput />
|
||||
<ProgressBarTrack>
|
||||
<ProgressBarFill data-testid="fill" />
|
||||
</ProgressBarTrack>
|
||||
</ProgressBar>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
const root = w.get("[data-slot=progress-bar]");
|
||||
const fill = w.get("[data-slot=progress-bar-fill]");
|
||||
expect(root.attributes("aria-valuenow")).toBe("20");
|
||||
expect((fill.element as HTMLElement).style.width).toBe("20%");
|
||||
|
||||
value.value = 80;
|
||||
await nextTick();
|
||||
expect(root.attributes("aria-valuenow")).toBe("80");
|
||||
expect((fill.element as HTMLElement).style.width).toBe("80%");
|
||||
|
||||
indeterminate.value = true;
|
||||
await nextTick();
|
||||
expect(root.attributes("aria-valuenow")).toBeUndefined();
|
||||
expect((fill.element as HTMLElement).style.width).toBe("");
|
||||
|
||||
indeterminate.value = false;
|
||||
value.value = 45;
|
||||
await nextTick();
|
||||
expect(root.attributes("aria-valuenow")).toBe("45");
|
||||
expect((fill.element as HTMLElement).style.width).toBe("45%");
|
||||
});
|
||||
|
||||
it("merges caller class arrays/objects and supports scoped default slot", () => {
|
||||
const w = mount(ProgressBar, {
|
||||
props: { value: 30, size: "sm" },
|
||||
attrs: {
|
||||
"aria-label": "p",
|
||||
class: ["custom-a", { "custom-b": true }],
|
||||
},
|
||||
slots: {
|
||||
default: (scope: {
|
||||
percentage: number | undefined;
|
||||
valueText: string | undefined;
|
||||
isIndeterminate: boolean;
|
||||
}) =>
|
||||
h("span", { "data-testid": "scoped" }, [
|
||||
`${scope.percentage}|${scope.valueText}|${scope.isIndeterminate}`,
|
||||
]),
|
||||
},
|
||||
});
|
||||
const root = w.get("[data-slot=progress-bar]");
|
||||
expect(root.classes()).toContain("hv-progress-bar");
|
||||
expect(root.classes()).toContain("hv-progress-bar--sm");
|
||||
expect(root.classes()).toContain("custom-a");
|
||||
expect(root.classes()).toContain("custom-b");
|
||||
expect(w.get("[data-testid=scoped]").text()).toBe("30|30%|false");
|
||||
});
|
||||
});
|
||||
|
||||
describe("ProgressCircle", () => {
|
||||
it("renders circle geometry and indeterminate dash offset", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
ProgressCircle,
|
||||
ProgressCircleTrack,
|
||||
ProgressCircleTrackCircle,
|
||||
ProgressCircleFillCircle,
|
||||
},
|
||||
template: `
|
||||
<ProgressCircle aria-label="加载中" :value="60">
|
||||
<ProgressCircleTrack>
|
||||
<ProgressCircleTrackCircle />
|
||||
<ProgressCircleFillCircle />
|
||||
</ProgressCircleTrack>
|
||||
</ProgressCircle>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
const root = w.get("[data-slot=progress-circle]");
|
||||
expect(root.attributes("role")).toBe("progressbar");
|
||||
expect(root.attributes("aria-valuenow")).toBe("60");
|
||||
const track = w.get("svg[data-slot=progress-circle-track]");
|
||||
expect(track.attributes("viewBox")).toBe("0 0 36 36");
|
||||
expect(track.attributes("fill")).toBe("none");
|
||||
const trackCircle = w.get("[data-slot=progress-circle-track-circle]");
|
||||
expect(trackCircle.attributes("cx")).toBe("18");
|
||||
expect(trackCircle.attributes("cy")).toBe("18");
|
||||
expect(trackCircle.attributes("r")).toBe("16");
|
||||
expect(trackCircle.attributes("stroke-width")).toBe("4");
|
||||
const fillCircle = w.get("[data-slot=progress-circle-fill-circle]");
|
||||
const circumference = 2 * Math.PI * 16;
|
||||
expect(Number(fillCircle.attributes("stroke-dasharray"))).toBeCloseTo(
|
||||
circumference,
|
||||
5,
|
||||
);
|
||||
// 60% → offset = C * 0.4
|
||||
expect(Number(fillCircle.attributes("stroke-dashoffset"))).toBeCloseTo(
|
||||
circumference * 0.4,
|
||||
5,
|
||||
);
|
||||
expect(fillCircle.attributes("stroke-linecap")).toBe("round");
|
||||
expect(fillCircle.attributes("transform")).toBe("rotate(-90 18 18)");
|
||||
});
|
||||
|
||||
it("supports custom SVG geometry overrides", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
ProgressCircle,
|
||||
ProgressCircleTrack,
|
||||
ProgressCircleTrackCircle,
|
||||
ProgressCircleFillCircle,
|
||||
},
|
||||
template: `
|
||||
<ProgressCircle aria-label="细" :value="60">
|
||||
<ProgressCircleTrack stroke-width="2" view-box="0 0 36 36">
|
||||
<ProgressCircleTrackCircle :cx="18" :cy="18" :r="17" :stroke-width="2" />
|
||||
<ProgressCircleFillCircle :cx="18" :cy="18" :r="17" :stroke-width="2" />
|
||||
</ProgressCircleTrack>
|
||||
</ProgressCircle>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
w.get("[data-slot=progress-circle-track-circle]").attributes("r"),
|
||||
).toBe("17");
|
||||
expect(
|
||||
w
|
||||
.get("[data-slot=progress-circle-track-circle]")
|
||||
.attributes("stroke-width"),
|
||||
).toBe("2");
|
||||
});
|
||||
|
||||
it("omits aria-valuenow when indeterminate and spins track via CSS hook", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
ProgressCircle,
|
||||
ProgressCircleTrack,
|
||||
ProgressCircleTrackCircle,
|
||||
ProgressCircleFillCircle,
|
||||
},
|
||||
template: `
|
||||
<ProgressCircle is-indeterminate aria-label="加载中">
|
||||
<ProgressCircleTrack>
|
||||
<ProgressCircleTrackCircle />
|
||||
<ProgressCircleFillCircle />
|
||||
</ProgressCircleTrack>
|
||||
</ProgressCircle>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
w.get("[data-slot=progress-circle]").attributes("aria-valuenow"),
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Meter", () => {
|
||||
it("renders meter role with Label name and percent output", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
Label,
|
||||
Meter,
|
||||
MeterOutput,
|
||||
MeterTrack,
|
||||
MeterFill,
|
||||
},
|
||||
template: `
|
||||
<Meter :value="60">
|
||||
<Label>Storage</Label>
|
||||
<MeterOutput />
|
||||
<MeterTrack>
|
||||
<MeterFill />
|
||||
</MeterTrack>
|
||||
</Meter>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
const root = w.get("[data-slot=meter]");
|
||||
expect(root.attributes("role")).toBe("meter progressbar");
|
||||
expect(root.attributes("aria-valuenow")).toBe("60");
|
||||
expect(root.attributes("aria-valuetext")).toBe("60%");
|
||||
await nextTick();
|
||||
const label = w.get("[data-slot=label]");
|
||||
expect(root.attributes("aria-labelledby")).toBe(label.attributes("id"));
|
||||
expect(w.get("[data-slot=meter-output]").text()).toBe("60%");
|
||||
});
|
||||
|
||||
it("always reports a value (no indeterminate) and uses BEM color", () => {
|
||||
const w = mount(Meter, {
|
||||
props: { value: 50, color: "danger" },
|
||||
attrs: { "aria-label": "Storage", "data-foo": "bar" },
|
||||
slots: {
|
||||
default: () => [
|
||||
h(MeterOutput),
|
||||
h(MeterTrack, null, { default: () => h(MeterFill) }),
|
||||
],
|
||||
},
|
||||
});
|
||||
const root = w.get("[data-slot=meter]");
|
||||
expect(root.attributes("data-foo")).toBe("bar");
|
||||
expect(root.attributes("aria-valuenow")).toBe("50");
|
||||
expect(root.classes()).toContain("hv-meter--danger");
|
||||
expect(root.attributes("role")).toBe("meter progressbar");
|
||||
});
|
||||
|
||||
it("formats custom value ranges", () => {
|
||||
const w = mount(Meter, {
|
||||
props: {
|
||||
value: 750,
|
||||
maxValue: 1000,
|
||||
minValue: 0,
|
||||
formatOptions: { style: "currency", currency: "USD" },
|
||||
},
|
||||
attrs: { "aria-label": "Revenue" },
|
||||
slots: {
|
||||
default: () => [h(MeterOutput), h(MeterFill)],
|
||||
},
|
||||
});
|
||||
expect(w.get("[data-slot=meter-output]").text()).toBe("$750.00");
|
||||
expect(
|
||||
(w.get("[data-slot=meter-fill]").element as HTMLElement).style.width,
|
||||
).toBe("75%");
|
||||
expect(w.get("[data-slot=meter]").attributes("aria-valuenow")).toBe("750");
|
||||
expect(w.get("[data-slot=meter]").attributes("aria-valuemin")).toBe("0");
|
||||
expect(w.get("[data-slot=meter]").attributes("aria-valuemax")).toBe("1000");
|
||||
});
|
||||
|
||||
it("prefers explicit aria-label over Label association", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: { Label, Meter },
|
||||
template: `
|
||||
<Meter :value="40" aria-label="显式名称">
|
||||
<Label>内部标签</Label>
|
||||
</Meter>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
const root = w.get("[data-slot=meter]");
|
||||
expect(root.attributes("aria-label")).toBe("显式名称");
|
||||
expect(root.attributes("aria-labelledby")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("M18–M20 demos", () => {
|
||||
const demoModules = import.meta.glob<{ default: unknown }>(
|
||||
"../apps/docs/demos/{progress-bar,progress-circle,meter}-*.vue",
|
||||
{ eager: true },
|
||||
);
|
||||
|
||||
it("mounts all 20 baseline demos without console errors", async () => {
|
||||
const names = Object.keys(demoModules).sort();
|
||||
expect(names).toHaveLength(20);
|
||||
expect(names.map((n) => n.split("/").pop())).toEqual([
|
||||
"meter-basic.vue",
|
||||
"meter-colors.vue",
|
||||
"meter-custom-styles.vue",
|
||||
"meter-custom-value.vue",
|
||||
"meter-sizes.vue",
|
||||
"meter-without-label.vue",
|
||||
"progress-bar-basic.vue",
|
||||
"progress-bar-colors.vue",
|
||||
"progress-bar-custom-styles.vue",
|
||||
"progress-bar-custom-value.vue",
|
||||
"progress-bar-indeterminate.vue",
|
||||
"progress-bar-sizes.vue",
|
||||
"progress-bar-without-label.vue",
|
||||
"progress-circle-basic.vue",
|
||||
"progress-circle-colors.vue",
|
||||
"progress-circle-custom-styles.vue",
|
||||
"progress-circle-custom-svg.vue",
|
||||
"progress-circle-indeterminate.vue",
|
||||
"progress-circle-sizes.vue",
|
||||
"progress-circle-with-label.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 [path, mod] of Object.entries(demoModules)) {
|
||||
const w = mount(mod.default as never, { attachTo: document.body });
|
||||
expect(w.html().length).toBeGreaterThan(20);
|
||||
w.unmount();
|
||||
document.body.innerHTML = "";
|
||||
void path;
|
||||
}
|
||||
} finally {
|
||||
console.warn = warn;
|
||||
console.error = error;
|
||||
}
|
||||
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it("extracts, compiles and mounts documentation body SFC examples", async () => {
|
||||
const docs = [
|
||||
"apps/docs/components/progress-bar.md",
|
||||
"apps/docs/components/progress-circle.md",
|
||||
"apps/docs/components/meter.md",
|
||||
];
|
||||
const fixturePaths = [
|
||||
"tests/fixtures/m18m20-progress-bar-doc-0.vue",
|
||||
"tests/fixtures/m18m20-progress-bar-doc-1.vue",
|
||||
"tests/fixtures/m18m20-progress-circle-doc-0.vue",
|
||||
"tests/fixtures/m18m20-progress-circle-doc-1.vue",
|
||||
"tests/fixtures/m18m20-meter-doc-0.vue",
|
||||
"tests/fixtures/m18m20-meter-doc-1.vue",
|
||||
];
|
||||
const fixtures = await Promise.all(
|
||||
fixturePaths.map(async (path) => ({
|
||||
path,
|
||||
mod: (await import("./" + path.replace("tests/", ""))).default,
|
||||
})),
|
||||
);
|
||||
|
||||
let index = 0;
|
||||
for (const file of docs) {
|
||||
const source = readFileSync(file, "utf8");
|
||||
const blocks = [...source.matchAll(/```vue\n([\s\S]*?)```/g)].map(
|
||||
(m) => m[1],
|
||||
);
|
||||
expect(blocks.length).toBeGreaterThanOrEqual(2);
|
||||
for (const block of blocks) {
|
||||
const fixture = fixtures[index];
|
||||
expect(fixture).toBeTruthy();
|
||||
expect(readFileSync(fixture.path, "utf8")).toBe(block);
|
||||
const w = mount(fixture.mod as never, { attachTo: document.body });
|
||||
expect(w.html().length).toBeGreaterThan(20);
|
||||
w.unmount();
|
||||
document.body.innerHTML = "";
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
expect(index).toBe(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe("M18–M20 review regressions", () => {
|
||||
it("keeps explicit aria-labelledby on all three roots", () => {
|
||||
const cases = [
|
||||
{ Comp: ProgressBar, slot: "progress-bar" },
|
||||
{ Comp: ProgressCircle, slot: "progress-circle" },
|
||||
{ Comp: Meter, slot: "meter" },
|
||||
] as const;
|
||||
for (const { Comp, slot } of cases) {
|
||||
const w = mount(Comp, {
|
||||
props: { value: 40 },
|
||||
attrs: { "aria-labelledby": "external-label" },
|
||||
});
|
||||
expect(w.get(`[data-slot=${slot}]`).attributes("aria-labelledby")).toBe(
|
||||
"external-label",
|
||||
);
|
||||
w.unmount();
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps dynamic aria-labelledby updates and removals", async () => {
|
||||
const labelledby = ref<string | undefined>("external-label");
|
||||
const w = mount(ProgressBar, {
|
||||
props: { value: 10 },
|
||||
attrs: {
|
||||
get "aria-labelledby"() {
|
||||
return labelledby.value;
|
||||
},
|
||||
},
|
||||
// re-render when ref changes
|
||||
});
|
||||
// attrs object getters need a reactive wrapper component instead
|
||||
w.unmount();
|
||||
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
components: { ProgressBar },
|
||||
setup: () => ({ labelledby }),
|
||||
template: `
|
||||
<ProgressBar
|
||||
:value="10"
|
||||
:aria-labelledby="labelledby"
|
||||
data-testid="root"
|
||||
/>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
const root = wrapper.get("[data-testid=root]");
|
||||
expect(root.attributes("aria-labelledby")).toBe("external-label");
|
||||
|
||||
labelledby.value = "other-label";
|
||||
await nextTick();
|
||||
expect(root.attributes("aria-labelledby")).toBe("other-label");
|
||||
|
||||
labelledby.value = undefined;
|
||||
await nextTick();
|
||||
expect(root.attributes("aria-labelledby")).toBeUndefined();
|
||||
|
||||
labelledby.value = "external-label";
|
||||
await nextTick();
|
||||
expect(root.attributes("aria-labelledby")).toBe("external-label");
|
||||
});
|
||||
|
||||
it("preserves explicit Label id and unique auto ids for multiple labels", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: { Label, ProgressBar },
|
||||
template: `
|
||||
<ProgressBar :value="50">
|
||||
<Label id="my-label">自定义</Label>
|
||||
<Label>自动甲</Label>
|
||||
<Label>自动乙</Label>
|
||||
</ProgressBar>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
await nextTick();
|
||||
const labels = w.findAll("[data-slot=label]");
|
||||
expect(labels).toHaveLength(3);
|
||||
const ids = labels.map((l) => l.attributes("id"));
|
||||
expect(ids[0]).toBe("my-label");
|
||||
expect(ids[1]).toBeTruthy();
|
||||
expect(ids[2]).toBeTruthy();
|
||||
expect(new Set(ids).size).toBe(3);
|
||||
|
||||
const root = w.get("[data-slot=progress-bar]");
|
||||
const labelledby = root.attributes("aria-labelledby")!.split(/\s+/);
|
||||
expect(labelledby).toContain("my-label");
|
||||
expect(labelledby).toContain(ids[1]);
|
||||
expect(labelledby).toContain(ids[2]);
|
||||
expect(labelledby).toHaveLength(3);
|
||||
// Every referenced id exists in the mounted tree
|
||||
for (const id of labelledby) {
|
||||
expect(w.find(`[id="${id}"]`).exists()).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("restores native label when for is added dynamically and removes it again", async () => {
|
||||
const forAttr = ref<string | undefined>(undefined);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: { Label, ProgressBar },
|
||||
setup: () => ({ forAttr }),
|
||||
template: `
|
||||
<ProgressBar :value="50">
|
||||
<Label :for="forAttr">动态标签</Label>
|
||||
</ProgressBar>
|
||||
`,
|
||||
}),
|
||||
);
|
||||
await nextTick();
|
||||
expect(w.get("[data-slot=label]").element.tagName).toBe("SPAN");
|
||||
|
||||
forAttr.value = "target-input";
|
||||
await nextTick();
|
||||
const asLabel = w.get("[data-slot=label]");
|
||||
expect(asLabel.element.tagName).toBe("LABEL");
|
||||
expect(asLabel.attributes("for")).toBe("target-input");
|
||||
|
||||
forAttr.value = "other-input";
|
||||
await nextTick();
|
||||
expect(w.get("[data-slot=label]").attributes("for")).toBe("other-input");
|
||||
|
||||
forAttr.value = undefined;
|
||||
await nextTick();
|
||||
expect(w.get("[data-slot=label]").element.tagName).toBe("SPAN");
|
||||
|
||||
forAttr.value = "target-input";
|
||||
await nextTick();
|
||||
expect(w.get("[data-slot=label]").element.tagName).toBe("LABEL");
|
||||
});
|
||||
|
||||
it("keeps field labels working when for is present without indicator span override", async () => {
|
||||
// No indicator context: default remains native label without for
|
||||
const w = mount(Label, { slots: { default: "独立标签" } });
|
||||
expect(w.get("[data-slot=label]").element.tagName).toBe("LABEL");
|
||||
|
||||
const withFor = mount(Label, {
|
||||
attrs: { for: "x" },
|
||||
slots: { default: "关联标签" },
|
||||
});
|
||||
expect(withFor.get("[data-slot=label]").element.tagName).toBe("LABEL");
|
||||
expect(withFor.get("[data-slot=label]").attributes("for")).toBe("x");
|
||||
});
|
||||
});
|
||||
|
||||
describe("M18–M20 docs navigation", () => {
|
||||
it("registers the three pages in Layout sidebar groups", () => {
|
||||
const source = readFileSync(
|
||||
"apps/docs/.vitepress/theme/Layout.vue",
|
||||
"utf8",
|
||||
);
|
||||
for (const url of [
|
||||
"/components/meter",
|
||||
"/components/progress-bar",
|
||||
"/components/progress-circle",
|
||||
]) {
|
||||
expect(source).toContain(`url: "${url}"`);
|
||||
}
|
||||
expect(source).toContain("Meter 计量条");
|
||||
expect(source).toContain("ProgressBar 进度条");
|
||||
expect(source).toContain("ProgressCircle 环形进度条");
|
||||
// Order relative to neighbors used by prev/next
|
||||
const link = source.indexOf('url: "/components/link"');
|
||||
const meter = source.indexOf('url: "/components/meter"');
|
||||
const bar = source.indexOf('url: "/components/progress-bar"');
|
||||
const circle = source.indexOf('url: "/components/progress-circle"');
|
||||
const radio = source.indexOf('url: "/components/radio-group"');
|
||||
expect(link).toBeGreaterThan(-1);
|
||||
expect(meter).toBeGreaterThan(link);
|
||||
expect(bar).toBeGreaterThan(meter);
|
||||
expect(circle).toBeGreaterThan(bar);
|
||||
expect(radio).toBeGreaterThan(circle);
|
||||
});
|
||||
});
|
||||
|
||||
it("removes stale label IDs and keeps associations correct after id removal and reordering", async () => {
|
||||
const rows = ref(["a", "b"]);
|
||||
const ids = ref<Record<string, string>>({ a: "first-label" });
|
||||
const w = mount({
|
||||
render: () =>
|
||||
h(ProgressBar, {}, () =>
|
||||
rows.value.map((key) =>
|
||||
h(
|
||||
Label,
|
||||
{ key, ...(ids.value[key] ? { id: ids.value[key] } : {}) },
|
||||
() => key,
|
||||
),
|
||||
),
|
||||
),
|
||||
});
|
||||
for (const step of [
|
||||
() => {
|
||||
ids.value = { a: "new-first" };
|
||||
},
|
||||
() => {
|
||||
ids.value = {};
|
||||
},
|
||||
() => {
|
||||
rows.value = ["b", "a"];
|
||||
},
|
||||
() => {
|
||||
rows.value = ["a"];
|
||||
},
|
||||
() => {
|
||||
rows.value = [];
|
||||
},
|
||||
() => {
|
||||
rows.value = ["b"];
|
||||
},
|
||||
]) {
|
||||
step();
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
const labelIds = w
|
||||
.findAll("[data-slot=label]")
|
||||
.map((n) => n.attributes("id"));
|
||||
const refs = (w.attributes("aria-labelledby") ?? "")
|
||||
.split(" ")
|
||||
.filter(Boolean);
|
||||
expect([...refs].sort()).toEqual([...labelIds].sort());
|
||||
expect(new Set(labelIds).size).toBe(labelIds.length);
|
||||
}
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("isolates indicator labels from both surrounding and nested field scopes", async () => {
|
||||
const w = mount({
|
||||
render: () =>
|
||||
h(TextField, {}, () => [
|
||||
h(Label, { id: "outer-field" }, () => "Outer"),
|
||||
h(Input),
|
||||
h(ProgressBar, {}, () => [
|
||||
h(Label, { id: "progress-title" }, () => "Progress"),
|
||||
h(TextField, {}, () => [
|
||||
h(Label, { id: "inner-field" }, () => "Inner"),
|
||||
h(Input),
|
||||
]),
|
||||
]),
|
||||
]),
|
||||
});
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
const indicator = w.get("[data-slot=progress-bar]");
|
||||
expect(indicator.attributes("aria-labelledby")).toBe("progress-title");
|
||||
const label = w.get("#progress-title");
|
||||
expect(label.element.tagName).toBe("SPAN");
|
||||
expect(label.attributes("for")).toBeUndefined();
|
||||
const inputs = w.findAll("[data-slot=input]");
|
||||
expect(w.get("#outer-field").attributes("for")).toBe(
|
||||
inputs[0].attributes("id"),
|
||||
);
|
||||
expect(w.get("#inner-field").attributes("for")).toBe(
|
||||
inputs[1].attributes("id"),
|
||||
);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("hydrates complete label associations with nested field isolation", async () => {
|
||||
const show = ref(true);
|
||||
const Example = defineComponent({
|
||||
render: () =>
|
||||
h("div", {}, [
|
||||
h(ProgressBar, {}, () => [
|
||||
h(Label, {}, () => "Progress"),
|
||||
...(show.value
|
||||
? [h(Label, { id: "extra-progress-label" }, () => "Detail")]
|
||||
: []),
|
||||
h(TextField, {}, () => [h(Label, {}, () => "Field"), h(Input)]),
|
||||
]),
|
||||
h(Meter, {}, () => h(Label, {}, () => "Storage")),
|
||||
h(ProgressCircle, {}, () => h(Label, {}, () => "Loading")),
|
||||
]),
|
||||
});
|
||||
const html = await renderToString(createSSRApp(Example));
|
||||
const host = document.createElement("div");
|
||||
host.innerHTML = html;
|
||||
document.body.append(host);
|
||||
function assertReferences() {
|
||||
const ids = [...host.querySelectorAll("[id]")].map((el) => el.id);
|
||||
expect(new Set(ids).size).toBe(ids.length);
|
||||
for (const el of host.querySelectorAll("[aria-labelledby],[for]")) {
|
||||
for (const id of (
|
||||
el.getAttribute("aria-labelledby") ??
|
||||
el.getAttribute("for") ??
|
||||
""
|
||||
)
|
||||
.split(" ")
|
||||
.filter(Boolean))
|
||||
expect(ids).toContain(id);
|
||||
}
|
||||
}
|
||||
assertReferences();
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
const app = createSSRApp(Example);
|
||||
try {
|
||||
app.mount(host);
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
assertReferences();
|
||||
const progress = host.querySelector("[data-slot=progress-bar]")!;
|
||||
const nestedLabel = host.querySelector(
|
||||
"[data-slot=textfield] [data-slot=label]",
|
||||
)!;
|
||||
expect(progress.getAttribute("aria-labelledby")?.split(" ")).not.toContain(
|
||||
nestedLabel.id,
|
||||
);
|
||||
show.value = false;
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
assertReferences();
|
||||
expect(progress.getAttribute("aria-labelledby")).not.toContain(
|
||||
"extra-progress-label",
|
||||
);
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
expect(error).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
app.unmount();
|
||||
host.remove();
|
||||
warn.mockRestore();
|
||||
error.mockRestore();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user