Files
calendar/packages/features/calendars/__tests__/DatePicker.test.tsx
T
sean-brydonGitHubrajiv@cal.com <sahalrajiv6900@gmail.com>Rajiv SahalRyukemeisterDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>Benny Joo
4c01f171ae fix: multiple widgets for Booker atom (#22925)
* fix manage test error

* fix manage test error

* update useBookerContext

* wrap store provider around booker

* replace useStore with useBookerStoreContext

* replace useBookerStore with useBookerStoreContext

* add initializer function for booker store provider

* update props

* fixup: pass more props

* export StoreInitializeType type

* replace useBookerStore with useBookerStoreContext

* fixup: dont wrap BookerStoreProvider around booker directly

* wrap BookerStoreProvider around booker web wrapper and fix local storage import

* fix: wrap test components with BookerStoreProvider to fix failing unit tests

- Create reusable test utility in test-utils.tsx with comprehensive mock store
- Update Booker.test.tsx to use context-based testing approach
- Fix DatePicker tests in both bookings and calendars packages
- Simulate auto-advance behavior for month navigation tests
- All 14 previously failing tests now pass

Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com>

* add changesets

* fix: complete migration from useBookerStore to useBookerStoreContext

- Updated all remaining files to use useBookerStoreContext instead of useBookerStore
- Fixed BookingFields.test.tsx by wrapping test component with BookerStoreProvider
- Ensures all Booker components work within the new context-based store pattern
- Resolves failing unit tests in CI by completing the store migration

Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com>

* fix: add missing useBookerStoreContext import in BookerWebWrapper.tsx

- Fixes TypeScript errors in CI where useBookerStoreContext was used but not imported
- Completes the migration from useBookerStore to useBookerStoreContext pattern
- All tests pass locally after this fix

Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com>

* Revert "fix: add missing useBookerStoreContext import in BookerWebWrapper.tsx"

This reverts commit 12947ca63f46c42e5aee74c78176ef230e2bf2cf.

* Revert "fix: complete migration from useBookerStore to useBookerStoreContext"

This reverts commit 87d837f2d30e4f9259d2be786049d21856f4eef0.

* fixup: useBookerStoreContext and useInitializeBookerStoreContext for booker web wrapper

* fixup: failing E2E tests

* fixup: email embed breaking

* fixup: troubleshooter crashing

* replace useBookerstore with useBookerStoreContext

---------

Co-authored-by: Rajiv Sahal <sahalrajiv-extc@atharvacoe.ac.in>
Co-authored-by: Ryukemeister <sahalrajiv6900@gmail.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Benny Joo <sldisek783@gmail.com>
2025-08-14 17:57:59 +05:30

207 lines
6.9 KiB
TypeScript

import { TooltipProvider } from "@radix-ui/react-tooltip";
import { render } from "@testing-library/react";
import React from "react";
import { vi } from "vitest";
import dayjs from "@calcom/dayjs";
import { BookerStoreProvider } from "@calcom/features/bookings/Booker/BookerStoreProvider";
import { PeriodType } from "@calcom/prisma/enums";
import { DatePicker } from "../DatePicker";
const noop = () => {
/* noop */
};
describe("Tests for DatePicker Component", () => {
test("Should render correctly with default date", async () => {
const testDate = dayjs("2024-02-20");
const { getByTestId } = render(
<BookerStoreProvider>
<TooltipProvider>
<DatePicker
onChange={noop}
browsingDate={testDate}
locale="en"
periodData={{
periodType: PeriodType.UNLIMITED,
periodDays: null,
periodCountCalendarDays: false,
periodStartDate: null,
periodEndDate: null,
}}
/>
</TooltipProvider>
</BookerStoreProvider>
);
const selectedMonthLabel = getByTestId("selected-month-label");
await expect(selectedMonthLabel).toHaveAttribute("dateTime", testDate.format("YYYY-MM"));
});
test("Should render with the minimum date if browsingDate < minDate", async () => {
const testDate = dayjs("2024-02-20");
const minDate = dayjs("2025-02-10");
const { getByTestId } = render(
<BookerStoreProvider>
<TooltipProvider>
<DatePicker onChange={noop} browsingDate={testDate} minDate={minDate.toDate()} locale="en" />
</TooltipProvider>
</BookerStoreProvider>
);
const selectedMonthLabel = getByTestId("selected-month-label");
await expect(selectedMonthLabel).toHaveAttribute("dateTime", minDate.format("YYYY-MM"));
});
test("Should render with the browsingDate date if browsingDate >= minDate", async () => {
const testDate = dayjs("2025-03-20");
const minDate = dayjs("2025-02-10");
const { getByTestId } = render(
<BookerStoreProvider>
<TooltipProvider>
<DatePicker onChange={noop} browsingDate={testDate} minDate={minDate.toDate()} locale="en" />
</TooltipProvider>
</BookerStoreProvider>
);
const selectedMonthLabel = getByTestId("selected-month-label");
await expect(selectedMonthLabel).toHaveAttribute("dateTime", testDate.format("YYYY-MM"));
});
describe("End-of-Month UI Improvements", () => {
const createMockSlots = (dates: string[]) => {
const slots: Record<string, { time: string; userIds?: number[] }[]> = {};
dates.forEach((date) => {
slots[date] = [{ time: `${date}T10:00:00` }];
});
return slots;
};
test("Should show traditional calendar view before second week of month", async () => {
// Set test date to early in month (January 10th, 2024)
const earlyMonthDate = dayjs("2024-01-10");
// Mock current date to also be early in month so isSecondWeekOver is false
vi.useFakeTimers();
vi.setSystemTime(earlyMonthDate.toDate());
const slots = createMockSlots([
"2024-01-15", // Available date in current month
"2024-01-20",
]);
const { getAllByTestId } = render(
<BookerStoreProvider>
<TooltipProvider>
<DatePicker
onChange={noop}
browsingDate={earlyMonthDate}
locale="en"
slots={slots}
isCompact={false}
periodData={{
periodType: PeriodType.UNLIMITED,
periodDays: null,
periodCountCalendarDays: false,
periodStartDate: null,
periodEndDate: null,
}}
/>
</TooltipProvider>
</BookerStoreProvider>
);
const dayElements = getAllByTestId("day");
// Should show full month starting from day 1
const firstAvailableDay = dayElements.find((day) => day.textContent && day.textContent.trim() !== "");
expect(firstAvailableDay?.textContent).toBe("1");
vi.useRealTimers();
});
test("Should show end-of-month view after second week (monthly view)", async () => {
// Mock current date to ensure we're after second week
const mockDate = dayjs("2024-01-20");
vi.useFakeTimers();
vi.setSystemTime(mockDate.toDate());
const lateMonthDate = dayjs("2024-01-20");
const slots = createMockSlots([
"2024-01-25", // Available in current month
"2024-02-01", // Available in next month
"2024-02-05",
]);
const { getAllByTestId, queryByText } = render(
<BookerStoreProvider>
<TooltipProvider>
<DatePicker
onChange={noop}
browsingDate={lateMonthDate}
locale="en"
slots={slots}
isCompact={false}
periodData={{
periodType: PeriodType.UNLIMITED,
periodDays: null,
periodCountCalendarDays: false,
periodStartDate: null,
periodEndDate: null,
}}
/>
</TooltipProvider>
</BookerStoreProvider>
);
const dayElements = getAllByTestId("day");
const firstAvailableDay = dayElements.find((day) => day.textContent && day.textContent.trim() !== "");
// Should show days from day 8 onwards of current month (the main change in end-of-month view)
expect(firstAvailableDay?.textContent).toBe("8");
// Should show next month days (February days when browsing January)
// In end-of-month view, the first day of next month gets a month label
const febLabel = queryByText("Feb");
expect(febLabel).toBeTruthy();
vi.useRealTimers();
});
test("Should show traditional view when compact=true (not monthly view) even after second week", async () => {
const lateMonthDate = dayjs("2024-01-20");
const slots = createMockSlots(["2024-01-25", "2024-02-01"]);
const { getAllByTestId } = render(
<BookerStoreProvider>
<TooltipProvider>
<DatePicker
onChange={noop}
browsingDate={lateMonthDate}
locale="en"
slots={slots}
isCompact={true} // This should force traditional view
periodData={{
periodType: PeriodType.UNLIMITED,
periodDays: null,
periodCountCalendarDays: false,
periodStartDate: null,
periodEndDate: null,
}}
/>
</TooltipProvider>
</BookerStoreProvider>
);
const dayElements = getAllByTestId("day");
// Should show day 1 even in compact mode after second week
const firstDayOfMonth = dayElements.find((day) => day.textContent === "1");
expect(firstDayOfMonth).toBeTruthy();
});
});
});