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; const wrappers: ReturnType[] = []; function mountInLocale(locale: Ref, 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); });