Files
heroui-vue/tests/calendar-demos.test.ts
T
maoyingjie 2e3fdd4800 fix(demos): inherit ConfigProvider locale and restore the format-options popover width
HD-03 scope, all against heroui ce90ea3 cn sources:

- calendar-disabled/read-only: restore the today() default value and the
  baseline description lines (日历已禁用 / 日历为只读) inside the
  flex flex-col items-center wrapper
- calendar-controlled: restore the second button row (设为今天/设为圣诞节/
  清空) and the baseline feedback wording (已选日期:…(未选))
- locale inheritance: add a public useLocale() accessor (the components'
  ConfigProvider → en-US/ltr chain) and use it for startOfWeek in
  calendar-controlled, isWeekend in booking-calendar/unavailable-dates and
  the DateFormatter in date-range-picker-format-options instead of the
  hardcoded 'en-US' / 'zh-CN' literals
- date-range-picker-format-options: restore the pinned w-full popover class;
  the pinned source positions the popover against the initial containing
  block (full viewport width — verified on the reference docs at desktop and
  narrow), while Reka's shrink-to-fit positioning wrapper collapses the same
  class. A scoped adapter rule (.hv-date-range-picker__popover.w-full →
  100vw, layering-safe like the dropdown static-wrapper adapter) restores
  the baseline effect: viewport-wide popover, 252px calendar, stretched
  time rows, identical boundary clamping

- tests: locale dynamic-switching regressions covering zh-CN/en-US for week
  start, weekend rules (with ar-EG flipping the weekend for a observable
  rule change) and range formatting; full suite 110 files / 1627 tests,
  typecheck, build:docs and the date-range-picker / api-root-aliases /
  calendar tarball contracts pass
2026-10-28 16:35:26 +08:00

157 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, ref, type Component, type Ref } from "vue";
import {
getDayOfWeek,
getLocalTimeZone,
isWeekend,
startOfMonth,
startOfWeek,
today,
} from "@internationalized/date";
import { ConfigProvider } from "../packages/vue/src";
const demos = import.meta.glob("../apps/docs/demos/calendar-*.vue", {
eager: true,
import: "default",
}) as Record<string, Component>;
const wrappers: ReturnType<typeof mount>[] = [];
function mountInLocale(locale: Ref<string>, name: string) {
const demo = demos[`../apps/docs/demos/${name}.vue`];
const w = mount(
defineComponent({
render: () => h(ConfigProvider, { locale: locale.value }, () => h(demo!)),
}),
{ attachTo: document.body },
);
wrappers.push(w);
return w;
}
afterEach(() => {
wrappers.splice(0).forEach((w) => w.unmount());
document.body.innerHTML = "";
vi.restoreAllMocks();
});
it("mounts all nineteen baseline demos together without duplicate IDs or Vue warnings", async () => {
expect(Object.keys(demos)).toHaveLength(19);
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const w = mount(
defineComponent({
render: () =>
h(
"div",
Object.entries(demos).map(([key, demo]) => h(demo, { key })),
),
}),
{ attachTo: document.body },
);
wrappers.push(w);
await flushPromises();
const ids = Array.from(document.querySelectorAll("[id]")).map((e) => e.id);
expect(new Set(ids).size).toBe(ids.length);
expect(warn).not.toHaveBeenCalled();
});
it("the 本周 jump uses the active ConfigProvider locale and follows runtime switches", async () => {
const locale = ref("en-US");
mountInLocale(locale, "calendar-controlled");
await flushPromises();
const weekButton = () =>
[...document.querySelectorAll("button")].find(
(b) => b.textContent?.trim() === "本周",
)!;
const picked = () =>
document.body.textContent!.match(/已选日期:(\d{4}-\d{2}-\d{2})/)?.[1];
weekButton().click();
await flushPromises();
const enStart = startOfWeek(today(getLocalTimeZone()), "en-US").toString();
expect(enStart).toBe(
startOfWeek(today(getLocalTimeZone()), "en-US").toString(),
);
expect(picked()).toBe(enStart);
locale.value = "zh-CN";
await flushPromises();
weekButton().click();
await flushPromises();
const zhStart = startOfWeek(today(getLocalTimeZone()), "zh-CN").toString();
// The two locales disagree on the first day of the week (Sunday vs Monday),
// so the same action must land on different days after the switch.
expect(zhStart).not.toBe(enStart);
expect(picked()).toBe(zhStart);
});
it("weekend disabling follows the active locale, including weekend-rule changes", async () => {
const base = startOfWeek(today(getLocalTimeZone()), "en-US");
const day = (offset: number) => base.add({ days: offset }).toString();
const friday = day(5),
saturday = day(6),
sunday = day(0);
const flags = () =>
Object.fromEntries(
[friday, saturday, sunday].map((date) => [
date,
!!document
.querySelector(`[data-date="${date}"]`)
?.getAttribute("data-unavailable"),
]),
);
const locale = ref("en-US");
mountInLocale(locale, "calendar-unavailable-dates");
await flushPromises();
// en-US/zh-CN share the Sat–Sun weekend (CLDR); the flags must equal the
// locale rule either way.
expect(flags()).toEqual({
[friday]: isWeekend(base.add({ days: 5 }), "en-US"),
[saturday]: isWeekend(base.add({ days: 6 }), "en-US"),
[sunday]: isWeekend(base.add({ days: 0 }), "en-US"),
});
expect(flags()[saturday]).toBe(true);
expect(flags()[sunday]).toBe(true);
expect(flags()[friday]).toBe(false);
locale.value = "zh-CN";
await flushPromises();
expect(flags()).toEqual({
[friday]: isWeekend(base.add({ days: 5 }), "zh-CN"),
[saturday]: isWeekend(base.add({ days: 6 }), "zh-CN"),
[sunday]: isWeekend(base.add({ days: 0 }), "zh-CN"),
});
// ar-EG has a Friday–Saturday weekend: the same cells must flip.
locale.value = "ar-EG";
await flushPromises();
expect(flags()).toEqual({
[friday]: true,
[saturday]: true,
[sunday]: false,
});
});
it("the booking calendar combines locale weekends with the booked days", async () => {
const booked = [5, 6, 12, 13, 14, 20];
const monthStart = startOfMonth(today(getLocalTimeZone()));
const days = Array.from({ length: 31 }, (_, i) =>
monthStart.add({ days: i }),
).filter((d) => d.month === monthStart.month);
const iso = (d: (typeof days)[number]) => d.toString();
const unavailable = (d: (typeof days)[number]) =>
!!document
.querySelector(`[data-date="${iso(d)}"]`)
?.getAttribute("data-unavailable");
const sunday = days.find(
(d) => !booked.includes(d.day) && getDayOfWeek(d, "en-US", "sun") === 0,
)!;
const arWorkingDay = days.find(
(d) => booked.includes(d.day) && !isWeekend(d, "ar-EG"),
)!;
const locale = ref("en-US");
mountInLocale(locale, "calendar-booking-calendar");
await flushPromises();
// Booked days stay unavailable whatever the weekend rule is.
expect(unavailable(arWorkingDay)).toBe(true);
// The locale weekend applies on top of the booked days.
expect(unavailable(sunday)).toBe(true);
locale.value = "ar-EG";
await flushPromises();
// Sunday becomes a working day under ar-EG and is not booked here.
expect(unavailable(sunday)).toBe(false);
expect(unavailable(arWorkingDay)).toBe(true);
});