fix: enforce ColorSlider union props and clean part refs

This commit is contained in:
maoyingjie
2026-10-27 10:23:10 +08:00
parent 92eb59e389
commit ea881cbcf8
9 changed files with 221 additions and 62 deletions
+60
View File
@@ -579,6 +579,66 @@ describe("ColorSlider public contract", () => {
expect(component.emitted("changeEnd")).toBeUndefined();
});
it("clears thumb-owned refs when only the thumb unmounts and allows remount", async () => {
const show = shallowRef(true);
const w = slide(
{},
`<ColorSlider ref="slider" channel="hue" default-value="hsl(10,100%,50%)"><ColorSliderTrack><ColorSliderThumb v-if="show" /></ColorSliderTrack></ColorSlider>`,
() => ({ show }),
);
const first = input(w).element;
const focus = vi.spyOn(first, "focus");
const bar = geometry(w);
await pointer(bar, "pointerdown", { clientX: 65 });
expect(bar.hasPointerCapture(1)).toBe(true);
focus.mockClear();
show.value = false;
await flushPromises();
expect(bar.hasPointerCapture(1)).toBe(false);
expect(emitted(w, "changeEnd")).toHaveLength(0);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(focus).not.toHaveBeenCalled();
show.value = true;
await flushPromises();
expect(input(w).element).not.toBe(first);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(document.activeElement).toBe(input(w).element);
const remounted = geometry(w);
await pointer(remounted, "pointerdown", { clientX: 175 });
await pointer(remounted, "pointerup", { clientX: 175 });
expect(values(w, "changeEnd")[0]?.getChannelValue("hue")).toBe(270);
});
it("clears track-owned refs when the track unmounts and remounts", async () => {
const show = shallowRef(true);
const w = slide(
{},
`<ColorSlider ref="slider" channel="hue" default-value="hsl(10,100%,50%)"><ColorSliderTrack v-if="show"><ColorSliderThumb /></ColorSliderTrack></ColorSlider>`,
() => ({ show }),
);
const oldTrack = geometry(w);
const oldInput = input(w).element;
const focus = vi.spyOn(oldInput, "focus");
await pointer(oldTrack, "pointerdown", { clientX: 65 });
expect(oldTrack.hasPointerCapture(1)).toBe(true);
focus.mockClear();
show.value = false;
await flushPromises();
expect(oldTrack.hasPointerCapture(1)).toBe(false);
expect(emitted(w, "changeEnd")).toHaveLength(0);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(focus).not.toHaveBeenCalled();
show.value = true;
await flushPromises();
const remounted = geometry(w);
expect(remounted).not.toBe(oldTrack);
await pointer(remounted, "pointerdown", { clientX: 175 });
await pointer(remounted, "pointerup", { clientX: 175 });
expect(values(w, "changeEnd")[0]?.getChannelValue("hue")).toBe(270);
});
it("aborts stale drags on external replacement but accepts synchronous echoes", async () => {
const value = shallowRef(parseColor("hsb(240,50%,50%)"));
const w = slide(