121 lines
4.2 KiB
TypeScript
121 lines
4.2 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { mount, flushPromises } from "@vue/test-utils";
|
|
import { readFileSync } from "node:fs";
|
|
const demos = import.meta.glob("../apps/docs/demos/date-picker-*.vue", {
|
|
eager: true,
|
|
import: "default",
|
|
});
|
|
const expected = [
|
|
"basic",
|
|
"disabled",
|
|
"controlled",
|
|
"with-validation",
|
|
"format-options",
|
|
"form-example",
|
|
"with-custom-indicator",
|
|
"render-function",
|
|
"international-calendar",
|
|
"custom-styles",
|
|
];
|
|
const wrappers: ReturnType<typeof mount>[] = [];
|
|
beforeEach(() => {
|
|
vi.stubGlobal(
|
|
"ResizeObserver",
|
|
class {
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
},
|
|
);
|
|
});
|
|
afterEach(async () => {
|
|
wrappers.splice(0).forEach((w) => w.unmount());
|
|
await flushPromises();
|
|
vi.unstubAllGlobals();
|
|
document.body.innerHTML = "";
|
|
});
|
|
describe("DatePicker documentation", () => {
|
|
it("includes every pinned preview and uses the displayed SFC as its implementation", () => {
|
|
const md = readFileSync("apps/docs/components/date-picker.md", "utf8");
|
|
expect(
|
|
[...md.matchAll(/<ComponentPreview name="date-picker-([^"]+)"/g)].map(
|
|
(match) => match[1],
|
|
),
|
|
).toEqual(expected);
|
|
expect(md).not.toContain("[!toc]");
|
|
expect(Object.keys(demos)).toHaveLength(10);
|
|
});
|
|
it("mounts all ten examples without warnings or missing date controls", async () => {
|
|
const warnings: string[] = [];
|
|
for (const demo of Object.values(demos)) {
|
|
const w = mount(demo as never, {
|
|
global: {
|
|
config: { warnHandler: (message) => warnings.push(message) },
|
|
},
|
|
});
|
|
wrappers.push(w);
|
|
await flushPromises();
|
|
expect(w.find('[data-slot="date-picker"]').exists()).toBe(true);
|
|
expect(w.find('[data-slot="date-input-group-segment"]').exists()).toBe(
|
|
true,
|
|
);
|
|
expect(w.find('[data-slot="date-picker-trigger"]').exists()).toBe(true);
|
|
expect(w.find('[data-slot="date-picker-value"]').exists()).toBe(true);
|
|
}
|
|
expect(warnings).toEqual([]);
|
|
});
|
|
it("the controlled example updates and clears the real date", async () => {
|
|
const component = demos["../apps/docs/demos/date-picker-controlled.vue"];
|
|
const w = mount(component as never);
|
|
wrappers.push(w);
|
|
const value = () =>
|
|
(w.get('[data-slot="date-picker-value"]').element as HTMLInputElement)
|
|
.value;
|
|
expect(value()).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
const buttons = w.findAll("button");
|
|
await buttons.find((button) => button.text() === "清空")!.trigger("click");
|
|
await flushPromises();
|
|
expect(value()).toBe("");
|
|
expect(w.get('[data-slot="description"]').text()).toContain("(空)");
|
|
await w
|
|
.findAll("button")
|
|
.find((button) => button.text() === "设为今天")!
|
|
.trigger("click");
|
|
await flushPromises();
|
|
expect(value()).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
expect(w.get('[data-slot="description"]').text()).not.toContain("(空)");
|
|
});
|
|
it("format options keeps one picker value while TimeField edits its time", async () => {
|
|
const component =
|
|
demos["../apps/docs/demos/date-picker-format-options.vue"];
|
|
const w = mount(component as never, { attachTo: document.body });
|
|
wrappers.push(w);
|
|
await flushPromises();
|
|
expect(
|
|
(w.get('[data-slot="date-picker-value"]').element as HTMLInputElement)
|
|
.value,
|
|
).toContain("T08:45:00");
|
|
await w.get('[data-slot="date-picker-trigger"]').trigger("click");
|
|
await flushPromises();
|
|
const timeField = document.querySelector('[data-slot="time-field"]');
|
|
expect(timeField).not.toBeNull();
|
|
const hour = timeField!.querySelector<HTMLElement>('[data-type="hour"]')!;
|
|
expect(hour.textContent?.trim()).toBe("8");
|
|
hour.focus();
|
|
hour.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "ArrowUp",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await flushPromises();
|
|
expect(
|
|
(w.get('[data-slot="date-picker-value"]').element as HTMLInputElement)
|
|
.value,
|
|
).toContain("T09:45:00");
|
|
expect(document.querySelectorAll('[name="date"]')).toHaveLength(1);
|
|
expect(document.querySelectorAll('[name="time"]')).toHaveLength(1);
|
|
});
|
|
});
|