|
1 |
| -import { render, waitFor } from "@testing-library/svelte"; |
| 1 | +import { render } from "@testing-library/svelte"; |
2 | 2 | import userEvent from "@testing-library/user-event";
|
3 | 3 | import { axe } from "jest-axe";
|
4 | 4 | import { describe, it } from "vitest";
|
5 | 5 | import { testKbd as kbd } from "../utils.js";
|
6 | 6 | import CalendarTest from "./CalendarTest.svelte";
|
7 | 7 | import type { Calendar } from "$lib";
|
8 | 8 | import { CalendarDate, CalendarDateTime, toZoned } from "@internationalized/date";
|
9 |
| -import { get } from "svelte/store"; |
10 | 9 |
|
11 | 10 | const calendarDate = new CalendarDate(1980, 1, 20);
|
12 | 11 | const calendarDateTime = new CalendarDateTime(1980, 1, 20, 12, 30, 0, 0);
|
@@ -232,4 +231,81 @@ describe("Calendar", () => {
|
232 | 231 | expect(heading).toHaveTextContent("December 1979 - January 1980");
|
233 | 232 | expect(firstMonthDay).not.toHaveAttribute("data-value", firstMonthDayDateStr);
|
234 | 233 | });
|
| 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 | + }); |
235 | 311 | });
|
0 commit comments