332 lines
13 KiB
TypeScript
332 lines
13 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { mount, flushPromises } from "@vue/test-utils";
|
|
import { createSSRApp, defineComponent, h, ref } from "vue";
|
|
import { renderToString } from "@vue/server-renderer";
|
|
import {
|
|
Slider,
|
|
SliderTrack,
|
|
SliderFill,
|
|
SliderThumb,
|
|
SliderOutput,
|
|
SliderMarks,
|
|
Label,
|
|
Description,
|
|
Fieldset,
|
|
ConfigProvider,
|
|
} from "../packages/vue/src";
|
|
const wrappers: ReturnType<typeof mount>[] = [];
|
|
afterEach(async () => {
|
|
wrappers.splice(0).forEach((wrapper) => wrapper.unmount());
|
|
await flushPromises();
|
|
document.body.innerHTML = "";
|
|
});
|
|
const parts =
|
|
"<Label>Volume</Label><SliderOutput/><SliderTrack><SliderFill/><SliderThumb/></SliderTrack>";
|
|
function fixture(template: string, setup = () => ({})) {
|
|
const wrapper = mount(
|
|
defineComponent({
|
|
components: {
|
|
Slider,
|
|
SliderTrack,
|
|
SliderFill,
|
|
SliderThumb,
|
|
SliderOutput,
|
|
SliderMarks,
|
|
Label,
|
|
Description,
|
|
Fieldset,
|
|
ConfigProvider,
|
|
},
|
|
setup,
|
|
template,
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
wrappers.push(wrapper);
|
|
return wrapper;
|
|
}
|
|
const sliders = (wrapper: ReturnType<typeof mount>) =>
|
|
wrapper.findAll('input[type="range"]');
|
|
const values = (wrapper: ReturnType<typeof mount>) =>
|
|
sliders(wrapper).map((input) => (input.element as HTMLInputElement).value);
|
|
|
|
describe("Slider public contracts", () => {
|
|
it("maps baseline data slots, initial value, output and native Label association", async () => {
|
|
const wrapper = fixture(`<Slider :default-value="30">${parts}</Slider>`);
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["30"]);
|
|
expect(wrapper.get("output").text()).toBe("30");
|
|
expect(wrapper.get("input").attributes("aria-labelledby")).toContain(
|
|
wrapper.get("[data-slot=label]").attributes("id"),
|
|
);
|
|
expect(
|
|
wrapper.findAll('[data-slot^="slider"]').length,
|
|
).toBeGreaterThanOrEqual(6);
|
|
});
|
|
it("uses number events for one thumb and supports decimal bounds without duplicate end events", async () => {
|
|
const update = vi.fn(),
|
|
end = vi.fn();
|
|
const wrapper = fixture(
|
|
`<Slider :default-value="0.2" :max-value="0.3" :step="0.1" @update:model-value="update" @change-end="end">${parts}</Slider>`,
|
|
() => ({ update, end }),
|
|
);
|
|
await wrapper.get("input").trigger("keydown", { key: "ArrowRight" });
|
|
expect(values(wrapper)).toEqual(["0.3"]);
|
|
expect(update.mock.calls).toEqual([[0.3]]);
|
|
expect(end.mock.calls).toEqual([[0.3]]);
|
|
await wrapper.get("input").trigger("keydown", { key: "End" });
|
|
expect(end).toHaveBeenCalledOnce();
|
|
});
|
|
it("keeps explicit range index and prevents crossing adjacent thumbs", async () => {
|
|
const update = vi.fn();
|
|
const wrapper = fixture(
|
|
`<Slider :default-value="[20,80]" @update:model-value="update"><SliderTrack><SliderThumb name="lower"/><SliderThumb :index="1" name="upper"/></SliderTrack></Slider>`,
|
|
() => ({ update }),
|
|
);
|
|
await sliders(wrapper)[1].trigger("keydown", { key: "ArrowLeft" });
|
|
expect(values(wrapper)).toEqual(["20", "79"]);
|
|
expect(update).toHaveBeenLastCalledWith([20, 79]);
|
|
await sliders(wrapper)[0].trigger("keydown", { key: "End" });
|
|
expect(values(wrapper)).toEqual(["79", "79"]);
|
|
});
|
|
it("restores rejected controlled native input and allows identical retries", async () => {
|
|
const update = vi.fn();
|
|
const wrapper = fixture(
|
|
`<Slider :model-value="20" @update:model-value="update">${parts}</Slider>`,
|
|
() => ({ update }),
|
|
);
|
|
for (let i = 0; i < 2; i++) {
|
|
await wrapper.get("input").setValue("30");
|
|
await flushPromises();
|
|
}
|
|
expect(values(wrapper)).toEqual(["20"]);
|
|
expect(update.mock.calls).toEqual([[30], [30]]);
|
|
});
|
|
it("reacts to controlled ownership additions/removals and explicit undefined", async () => {
|
|
const props = ref<Record<string, unknown>>({});
|
|
const wrapper = fixture(
|
|
`<Slider v-bind="props" :default-value="40">${parts}</Slider>`,
|
|
() => ({ props }),
|
|
);
|
|
props.value = { modelValue: undefined };
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["0"]);
|
|
props.value = { modelValue: [20] };
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["20"]);
|
|
props.value = {};
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["40"]);
|
|
});
|
|
it("honors cancelled keydown handlers, arrays and capture", async () => {
|
|
const cancel = (event: Event) => event.preventDefault();
|
|
const update = vi.fn();
|
|
const wrapper = fixture(
|
|
`<Slider @update:model-value="update"><SliderTrack><SliderThumb :onKeydown="[cancel]"/></SliderTrack></Slider>`,
|
|
() => ({ cancel, update }),
|
|
);
|
|
await wrapper.get("input").trigger("keydown", { key: "End" });
|
|
await flushPromises();
|
|
expect(update).not.toHaveBeenCalled();
|
|
expect(values(wrapper)).toEqual(["0"]);
|
|
const capture = fixture(
|
|
`<Slider @keydown.capture.prevent><SliderTrack><SliderThumb/></SliderTrack></Slider>`,
|
|
);
|
|
await capture.get("input").trigger("keydown", { key: "End" });
|
|
expect(values(capture)).toEqual(["0"]);
|
|
});
|
|
it("honors stopImmediatePropagation without executing internal stepping", async () => {
|
|
const stop = (event: Event) => event.stopImmediatePropagation();
|
|
const wrapper = fixture(
|
|
`<Slider><SliderTrack><SliderThumb @keydown="stop"/></SliderTrack></Slider>`,
|
|
() => ({ stop }),
|
|
);
|
|
await wrapper.get("input").trigger("keydown", { key: "End" });
|
|
expect(values(wrapper)).toEqual(["0"]);
|
|
});
|
|
it("submits each named native input once, omits a disabled thumb, inherits Fieldset", async () => {
|
|
const disabled = ref(false);
|
|
const wrapper = fixture(
|
|
`<form><Fieldset :disabled="disabled"><Slider :default-value="[20,80]"><SliderTrack><SliderThumb name="range"/><SliderThumb :index="1" name="range" is-disabled/></SliderTrack></Slider></Fieldset></form>`,
|
|
() => ({ disabled }),
|
|
);
|
|
expect(new FormData(wrapper.get("form").element).getAll("range")).toEqual([
|
|
"20",
|
|
]);
|
|
await sliders(wrapper)[1].trigger("keydown", { key: "Home" });
|
|
expect(values(wrapper)).toEqual(["20", "80"]);
|
|
disabled.value = true;
|
|
await flushPromises();
|
|
expect(new FormData(wrapper.get("form").element).getAll("range")).toEqual(
|
|
[],
|
|
);
|
|
expect(
|
|
sliders(wrapper).every(
|
|
(input) => (input.element as HTMLInputElement).disabled,
|
|
),
|
|
).toBe(true);
|
|
});
|
|
it("resets the initial native/submitted range and respects cancelled reset", async () => {
|
|
const wrapper = fixture(
|
|
`<form><Slider :default-value="[20,80]"><SliderTrack><SliderThumb name="a"/><SliderThumb :index="1" name="b"/></SliderTrack></Slider></form>`,
|
|
);
|
|
await sliders(wrapper)[0].trigger("keydown", { key: "ArrowRight" });
|
|
wrapper.get("form").element.reset();
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["20", "80"]);
|
|
expect([...new FormData(wrapper.get("form").element).values()]).toEqual([
|
|
"20",
|
|
"80",
|
|
]);
|
|
wrapper
|
|
.get("form")
|
|
.element.addEventListener("reset", (event) => event.preventDefault());
|
|
await sliders(wrapper)[0].trigger("keydown", { key: "ArrowRight" });
|
|
wrapper.get("form").element.reset();
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["21", "80"]);
|
|
});
|
|
it("preserves controlled reset and external form association", async () => {
|
|
const wrapper = fixture(
|
|
`<div><form id="outside"/><Slider :model-value="50"><SliderTrack><SliderThumb name="a" form="outside"/></SliderTrack></Slider></div>`,
|
|
);
|
|
wrapper.get("form").element.reset();
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["50"]);
|
|
expect(new FormData(wrapper.get("form").element).getAll("a")).toEqual([
|
|
"50",
|
|
]);
|
|
});
|
|
it("moves in RTL/vertical direction, formats values through ConfigProvider", async () => {
|
|
const wrapper = fixture(
|
|
`<ConfigProvider dir="rtl" locale="de-DE"><Slider :default-value="1234" :max-value="2000"><SliderOutput/><SliderTrack><SliderThumb/></SliderTrack></Slider></ConfigProvider>`,
|
|
);
|
|
expect(wrapper.get("output").text()).toBe("1.234");
|
|
await wrapper.get("input").trigger("keydown", { key: "ArrowRight" });
|
|
expect(values(wrapper)).toEqual(["1233"]);
|
|
expect(
|
|
wrapper.get("[data-slot=slider-thumb]").attributes("style"),
|
|
).toContain("right:");
|
|
const vertical = fixture(
|
|
`<Slider orientation="vertical" :default-value="40">${parts}</Slider>`,
|
|
);
|
|
await vertical
|
|
.get("input")
|
|
.trigger("keydown", { key: "ArrowUp", shiftKey: true });
|
|
expect(values(vertical)).toEqual(["50"]);
|
|
expect(vertical.get("input").attributes("aria-orientation")).toBe(
|
|
"vertical",
|
|
);
|
|
});
|
|
it("updates explicit attrs, description IDs, classes and custom output", async () => {
|
|
const show = ref(true),
|
|
attributes = ref<Record<string, unknown>>({
|
|
"aria-label": "Custom",
|
|
class: "custom-thumb",
|
|
});
|
|
const wrapper = fixture(
|
|
`<Slider :default-value="[10,90]"><Description id="help" v-if="show">Help</Description><SliderOutput v-slot="{values}">{{values.join(' to ')}}</SliderOutput><SliderTrack><SliderFill/><SliderThumb v-bind="attributes"/><SliderThumb :index="1"/></SliderTrack><SliderMarks data-test="marks"/></Slider>`,
|
|
() => ({ show, attributes }),
|
|
);
|
|
await flushPromises();
|
|
expect(wrapper.get("output").text()).toBe("10 to 90");
|
|
expect(sliders(wrapper)[0].attributes("aria-describedby")).toBe("help");
|
|
expect(wrapper.get(".custom-thumb").attributes("data-slot")).toBe(
|
|
"slider-thumb",
|
|
);
|
|
attributes.value = {};
|
|
show.value = false;
|
|
await flushPromises();
|
|
expect(sliders(wrapper)[0].attributes("aria-label")).toBeUndefined();
|
|
expect(sliders(wrapper)[0].attributes("aria-describedby")).toBeUndefined();
|
|
expect(wrapper.find(".custom-thumb").exists()).toBe(false);
|
|
});
|
|
it("hydrates explicit range positions and names without warnings", async () => {
|
|
const app = {
|
|
render: () =>
|
|
h(Slider, { defaultValue: [20, 80] }, () => [
|
|
h(Label, {}, () => "Price"),
|
|
h(SliderTrack, {}, () => [
|
|
h(SliderFill),
|
|
h(SliderThumb),
|
|
h(SliderThumb, { index: 1 }),
|
|
]),
|
|
]),
|
|
};
|
|
const html = await renderToString(createSSRApp(app));
|
|
const container = document.createElement("div");
|
|
container.innerHTML = html;
|
|
document.body.append(container);
|
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
const client = createSSRApp(app);
|
|
try {
|
|
client.mount(container);
|
|
await flushPromises();
|
|
expect(warn).not.toHaveBeenCalled();
|
|
expect(error).not.toHaveBeenCalled();
|
|
expect(
|
|
[...container.querySelectorAll("input")].map((input) => input.value),
|
|
).toEqual(["20", "80"]);
|
|
} finally {
|
|
client.unmount();
|
|
warn.mockRestore();
|
|
error.mockRestore();
|
|
}
|
|
});
|
|
it("compiles and mounts all nine baseline demos without Vue warnings", async () => {
|
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
try {
|
|
for (const name of [
|
|
"default",
|
|
"disabled",
|
|
"vertical",
|
|
"range",
|
|
"controlled",
|
|
"custom-value-formatting",
|
|
"custom-output",
|
|
"render-function",
|
|
"custom-styles",
|
|
]) {
|
|
const demo = await import(`../apps/docs/demos/slider-${name}.vue`);
|
|
const wrapper = mount(demo.default, { attachTo: document.body });
|
|
try {
|
|
await flushPromises();
|
|
expect(sliders(wrapper).length).toBeGreaterThan(0);
|
|
} finally {
|
|
wrapper.unmount();
|
|
}
|
|
}
|
|
expect(warn).not.toHaveBeenCalled();
|
|
} finally {
|
|
warn.mockRestore();
|
|
}
|
|
});
|
|
it("keeps off-grid maxima consistent with actual native input and output", async () => {
|
|
const wrapper = fixture(
|
|
`<Slider :default-value="0.8" :step="0.3" :max-value="0.8">${parts}</Slider>`,
|
|
);
|
|
expect(values(wrapper)).toEqual(["0.6"]);
|
|
expect(wrapper.get("input").attributes("aria-valuenow")).toBe("0.6");
|
|
expect(wrapper.get("output").text()).toBe("0.6");
|
|
await wrapper.get("input").trigger("keydown", { key: "End" });
|
|
expect(values(wrapper)).toEqual(["0.6"]);
|
|
});
|
|
it("rebinds reset to a dynamically changed native form owner", async () => {
|
|
const owner = ref("first");
|
|
const wrapper = fixture(
|
|
`<div><form id="first"/><form id="second"/><Slider :default-value="20"><SliderTrack><SliderThumb :form="owner" name="amount"/></SliderTrack></Slider></div>`,
|
|
() => ({ owner }),
|
|
);
|
|
await wrapper.get("input").trigger("keydown", { key: "ArrowRight" });
|
|
owner.value = "second";
|
|
await flushPromises();
|
|
wrapper.get("#first").element.dispatchEvent(new Event("reset"));
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["21"]);
|
|
(wrapper.get("#second").element as HTMLFormElement).reset();
|
|
await flushPromises();
|
|
expect(values(wrapper)).toEqual(["20"]);
|
|
});
|
|
});
|