forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Issues with scheduling timezone change without end time (calcom#…
…19103) * fix: Issues with scheduling timezone change without end time * test: remove DatePicker component tests * feat: implement DatePicker component with tests and update button data-testid
- Loading branch information
1 parent
92811b6
commit 3b2a3a5
Showing
3 changed files
with
86 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 44 additions & 25 deletions
69
packages/ui/components/form/datepicker/datepicker.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,63 @@ | ||
/* eslint-disable playwright/missing-playwright-await */ | ||
import { render, fireEvent } from "@testing-library/react"; | ||
import { format } from "date-fns"; | ||
import { vi } from "vitest"; | ||
|
||
import DatePicker from "./DatePicker"; | ||
|
||
describe("Tests for DatePicker component", () => { | ||
const date = new Date("2023-07-15"); | ||
const onChangeMock = vi.fn(); | ||
|
||
test("Should display the selected date correctly and call the onDatesChange callback when the selected date changes", () => { | ||
const mockOnDatesChange = vi.fn((changedDate: Date) => format(new Date(changedDate), "yyyy-MM-dd")); | ||
const { container } = render(<DatePicker date={date} onDatesChange={mockOnDatesChange} />); | ||
describe("Tests for DatePicker Component", () => { | ||
const testDate = new Date("2024-02-20"); | ||
|
||
const day = container.querySelector('input[name="day"]') as HTMLInputElement; | ||
const dayEvent = { target: { value: "27" } }; | ||
test("Should render correctly with default date", () => { | ||
const testDate = new Date("2024-02-20"); | ||
const { getByTestId } = render(<DatePicker date={testDate} />); | ||
|
||
const month = container.querySelector('input[name="month"]') as HTMLInputElement; | ||
const monthEvent = { target: { value: "06" } }; | ||
const dateButton = getByTestId("pick-date"); | ||
expect(dateButton).toHaveTextContent(format(testDate, "LLL dd, y")); | ||
}); | ||
|
||
const year = container.querySelector('input[name="year"]') as HTMLInputElement; | ||
const yearEvent = { target: { value: "2022" } }; | ||
test("Should show placeholder when no date is provided", () => { | ||
const { getByTestId } = render(<DatePicker date={null as unknown as Date} />); | ||
|
||
fireEvent.change(day, dayEvent); | ||
expect(mockOnDatesChange).toHaveReturnedWith("2023-07-27"); | ||
const dateButton = getByTestId("pick-date"); | ||
expect(dateButton).toHaveTextContent("Pick a date"); | ||
}); | ||
|
||
fireEvent.change(month, monthEvent); | ||
expect(mockOnDatesChange).toHaveReturnedWith("2023-06-27"); | ||
test("Should handle date selection correctly", async () => { | ||
const testDate = new Date("2024-02-20"); | ||
const { getByTestId, getAllByRole } = render(<DatePicker date={testDate} onDatesChange={onChangeMock} />); | ||
|
||
fireEvent.change(year, yearEvent); | ||
expect(mockOnDatesChange).toHaveReturnedWith("2022-06-27"); | ||
const dateButton = getByTestId("pick-date"); | ||
fireEvent.click(dateButton); | ||
const gridCells = getAllByRole("gridcell"); | ||
const selectedDate = gridCells.find((cell) => { | ||
return cell.getAttribute("tabindex") === "0"; | ||
}); | ||
|
||
expect(mockOnDatesChange).toHaveBeenCalledTimes(3); | ||
expect(selectedDate).toBeTruthy(); | ||
await expect(selectedDate).not.toHaveClass("opacity-50"); | ||
}); | ||
test("Should respect minDate prop", async () => { | ||
const testDate = new Date("2024-02-20"); | ||
const minDate = new Date("2024-02-19"); | ||
const { getByTestId, getAllByRole } = render( | ||
<DatePicker date={testDate} minDate={minDate} onDatesChange={onChangeMock} /> | ||
); | ||
|
||
const dateButton = getByTestId("pick-date"); | ||
fireEvent.click(dateButton); | ||
|
||
const disabledDates = getAllByRole("gridcell").filter((cell) => cell.classList.contains("opacity-50")); | ||
expect(disabledDates.length).toBeGreaterThan(0); | ||
await expect(disabledDates[0]).toHaveAttribute("disabled"); | ||
}); | ||
|
||
test("Should disable the DatePicker when disabled prop is true", () => { | ||
const { getByDisplayValue } = render(<DatePicker date={date} disabled />); | ||
test("Should respect disabled prop", () => { | ||
const { getByTestId } = render(<DatePicker date={testDate} disabled={true} />); | ||
|
||
const dateInput = getByDisplayValue(format(date, "yyyy-MM-dd")) as HTMLInputElement; | ||
expect(dateInput).toBeDisabled(); | ||
const dateButton = getByTestId("pick-date"); | ||
expect(dateButton.classList.toString()).toContain("disabled:cursor-not-allowed"); | ||
expect(dateButton.classList.toString()).toContain("disabled:opacity-30"); | ||
}); | ||
}); | ||
|
||
HTMLCanvasElement.prototype.getContext = vi.fn() as never; |