Skip to content

Commit af77272

Browse files
committed
more cal tests
1 parent fbe87de commit af77272

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

src/tests/calendar/Calendar.spec.ts

+78-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { render, waitFor } from "@testing-library/svelte";
1+
import { render } from "@testing-library/svelte";
22
import userEvent from "@testing-library/user-event";
33
import { axe } from "jest-axe";
44
import { describe, it } from "vitest";
55
import { testKbd as kbd } from "../utils.js";
66
import CalendarTest from "./CalendarTest.svelte";
77
import type { Calendar } from "$lib";
88
import { CalendarDate, CalendarDateTime, toZoned } from "@internationalized/date";
9-
import { get } from "svelte/store";
109

1110
const calendarDate = new CalendarDate(1980, 1, 20);
1211
const calendarDateTime = new CalendarDateTime(1980, 1, 20, 12, 30, 0, 0);
@@ -232,4 +231,81 @@ describe("Calendar", () => {
232231
expect(heading).toHaveTextContent("December 1979 - January 1980");
233232
expect(firstMonthDay).not.toHaveAttribute("data-value", firstMonthDayDateStr);
234233
});
234+
235+
it("properly handles `pagedNavigation` with multiple months", async () => {
236+
const { getByTestId, calendar, user } = setup({
237+
value: calendarDateTime,
238+
numberOfMonths: 2,
239+
pagedNavigation: true
240+
});
241+
242+
const selectedDay = calendar.querySelector("[data-selected]") as HTMLElement;
243+
expect(selectedDay).toHaveTextContent(String(calendarDateTime.day));
244+
245+
const heading = getByTestId("heading");
246+
expect(heading).toHaveTextContent("January - February 1980");
247+
248+
const firstMonthDayDateStr = calendarDateTime.set({ day: 12 }).toString();
249+
const firstMonthDay = getByTestId("date-1-12");
250+
expect(firstMonthDay).toHaveTextContent("12");
251+
expect(firstMonthDay).toHaveAttribute("data-value", firstMonthDayDateStr);
252+
253+
const secondMonthDay = getByTestId("date-2-15");
254+
const secondMonthDayDateStr = calendarDateTime.set({ day: 15, month: 2 }).toString();
255+
expect(secondMonthDay).toHaveTextContent("15");
256+
expect(secondMonthDay).toHaveAttribute("data-value", secondMonthDayDateStr);
257+
258+
const prevButton = getByTestId("prev-button");
259+
const nextButton = getByTestId("next-button");
260+
261+
await user.click(nextButton);
262+
expect(heading).toHaveTextContent("March - April 1980");
263+
expect(firstMonthDay).not.toHaveAttribute("data-value", firstMonthDayDateStr);
264+
265+
await user.click(prevButton);
266+
expect(heading).toHaveTextContent("January - February 1980");
267+
expect(firstMonthDay).toHaveAttribute("data-value", firstMonthDayDateStr);
268+
269+
await user.click(prevButton);
270+
expect(heading).toHaveTextContent("November - December 1979");
271+
expect(firstMonthDay).not.toHaveAttribute("data-value", firstMonthDayDateStr);
272+
});
273+
274+
it("always renders six weeks when `fixedWeeks` is `true`", async () => {
275+
const { getByTestId, calendar, user } = setup({
276+
value: calendarDate,
277+
fixedWeeks: true
278+
});
279+
280+
function getNumberOfWeeks() {
281+
return calendar.querySelectorAll("[data-week]").length;
282+
}
283+
284+
const nextButton = getByTestId("next-button");
285+
286+
for (let i = 0; i < 12; i++) {
287+
expect(getNumberOfWeeks()).toBe(6);
288+
await user.click(nextButton);
289+
}
290+
291+
for (let i = 0; i < 24; i++) {
292+
expect(getNumberOfWeeks()).toBe(6);
293+
await user.click(nextButton);
294+
}
295+
});
296+
297+
it("should update the selected date when value controlled externally", async () => {
298+
const { getByTestId, user, calendar } = setup({
299+
value: calendarDate
300+
});
301+
302+
const selectedDate = calendar.querySelector("[data-selected]") as HTMLElement;
303+
expect(selectedDate).toHaveTextContent("20");
304+
expect(calendar.querySelectorAll("[data-selected]").length).toBe(1);
305+
306+
const addDayBtn = getByTestId("add-day");
307+
await user.click(addDayBtn);
308+
expect(calendar.querySelector("[data-selected]")).toHaveTextContent("21");
309+
expect(calendar.querySelectorAll("[data-selected]").length).toBe(1);
310+
});
235311
});

src/tests/calendar/CalendarTest.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</Calendar.GridHead>
5353
<Calendar.GridBody data-testid="grid-body-{m}">
5454
{#each month.weeks as weekDates, i}
55-
<Calendar.GridRow data-testid="grid-row-{m}-{i}">
55+
<Calendar.GridRow data-testid="grid-row-{m}-{i}" data-week>
5656
{#each weekDates as date, d}
5757
<Calendar.Cell {date} data-testid="cell-{date.month}-{d}">
5858
<Calendar.Date

0 commit comments

Comments
 (0)