Skip to content

Commit

Permalink
more calendar tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte committed Dec 3, 2023
1 parent 0e63866 commit e4eb83c
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 1 deletion.
95 changes: 94 additions & 1 deletion src/tests/calendar/Calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { testKbd as kbd } from "../utils.js";
import CalendarTest from "./CalendarTest.svelte";
import type { Calendar } from "$lib";
import { CalendarDate, CalendarDateTime, toZoned } from "@internationalized/date";
import CalendarMultiTest from "./CalendarMultiTest.svelte";

const calendarDate = new CalendarDate(1980, 1, 20);
const calendarDateTime = new CalendarDateTime(1980, 1, 20, 12, 30, 0, 0);
Expand All @@ -25,6 +26,14 @@ function setup(props: Calendar.Props = {}) {
return { ...returned, user, calendar };
}

function setupMulti(props: Calendar.Props<true> = {}) {
const user = userEvent.setup();
const returned = render(CalendarMultiTest, { ...props, multiple: true });
const calendar = returned.getByTestId("calendar");
expect(calendar).toBeVisible();
return { ...returned, user, calendar };
}

describe("Calendar", () => {
it("has no accessibility violations", async () => {
const { container } = render(CalendarTest);
Expand Down Expand Up @@ -551,4 +560,88 @@ describe("Calendar", () => {
});
});

describe.todo("Calendar - `multiple`");
describe("Calendar - `multiple`", () => {
it("handles default value when `value` prop is provided - `CalendarDate[]`", async () => {
const d1 = new CalendarDate(1980, 1, 2);
const d2 = new CalendarDate(1980, 1, 5);

const { calendar } = setupMulti({
value: [d1, d2]
});

const selectedDays = calendar.querySelectorAll("[data-selected]");
expect(selectedDays.length).toBe(2);
expect(selectedDays[0]).toHaveTextContent(String(d1.day));
expect(selectedDays[1]).toHaveTextContent(String(d2.day));
});

it("handles default value when `value` prop is provided - `CalendarDateTime[]`", async () => {
const d1 = new CalendarDateTime(1980, 1, 2);
const d2 = new CalendarDateTime(1980, 1, 5);

const { calendar } = setupMulti({
value: [d1, d2]
});

const selectedDays = calendar.querySelectorAll("[data-selected]");
expect(selectedDays.length).toBe(2);
expect(selectedDays[0]).toHaveTextContent(String(d1.day));
expect(selectedDays[1]).toHaveTextContent(String(d2.day));
});

it("handles default value when `value` prop is provided - `ZonedDateTime[]`", async () => {
const d1 = toZoned(new CalendarDateTime(1980, 1, 2), "America/New_York");
const d2 = toZoned(new CalendarDateTime(1980, 1, 5), "America/New_York");

const { calendar } = setupMulti({
value: [d1, d2]
});

const selectedDays = calendar.querySelectorAll("[data-selected]");
expect(selectedDays.length).toBe(2);
expect(selectedDays[0]).toHaveTextContent(String(d1.day));
expect(selectedDays[1]).toHaveTextContent(String(d2.day));
});

it("sets placeholder to last value in `value` prop", async () => {
const d1 = new CalendarDate(1980, 1, 2);
const d2 = new CalendarDate(1980, 5, 5);

const { calendar, getByTestId } = setupMulti({
value: [d1, d2]
});

const selectedDays = calendar.querySelectorAll("[data-selected]");
expect(selectedDays.length).toBe(1);
expect(getByTestId("heading")).toHaveTextContent("May 1980");
});

it("allows deselection", async () => {
const d1 = new CalendarDate(1980, 1, 2);
const d2 = new CalendarDate(1980, 1, 5);

const { calendar, user } = setupMulti({
value: [d1, d2]
});
const selectedDays = calendar.querySelectorAll<HTMLElement>("[data-selected]");
expect(selectedDays.length).toBe(2);
await user.click(selectedDays[0]);
expect(calendar.querySelectorAll("[data-selected]").length).toBe(1);
});

it("prevents deselection when only one date is selected and `preventDeselect` is `true`", async () => {
const d1 = new CalendarDate(1980, 1, 2);

const { calendar, user } = setupMulti({
value: [d1],
preventDeselect: true
});
const selectedDays = calendar.querySelectorAll("[data-selected]");
expect(selectedDays.length).toBe(1);
await user.click(selectedDays[0]);
const selectedDays2 = calendar.querySelectorAll("[data-selected]");
expect(selectedDays2.length).toBe(1);
await user.click(selectedDays2[0]);
expect(calendar.querySelectorAll("[data-selected]").length).toBe(1);
});
});
61 changes: 61 additions & 0 deletions src/tests/calendar/CalendarMultiTest.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<script lang="ts">
import { Calendar } from "$lib";
type $$Props = Calendar.Props<true>;
export let placeholder: $$Props["placeholder"] = undefined;
export let value: $$Props["value"] = undefined;
export let multiple: $$Props["multiple"] = true;
</script>

<main>
<div data-testid="value">{value}</div>
<Calendar.Root
let:months
let:weekdays
bind:placeholder
bind:value
{...$$restProps}
{multiple}
data-testid="calendar"
>
<Calendar.Header data-testid="header">
<Calendar.PrevButton data-testid="prev-button">Prev</Calendar.PrevButton>
<Calendar.Heading data-testid="heading" />
<Calendar.NextButton data-testid="next-button">Next</Calendar.NextButton>
</Calendar.Header>
<div>
{#each months as month}
{@const m = month.value.month}
<Calendar.Grid data-testid="grid-{m}">
<Calendar.GridHead data-testid="grid-head-{m}">
<Calendar.GridRow data-testid="grid-row-{m}">
{#each weekdays as day, i}
<Calendar.HeadCell data-testid="weekday-{m}-{i}">
{day}
</Calendar.HeadCell>
{/each}
</Calendar.GridRow>
</Calendar.GridHead>
<Calendar.GridBody data-testid="grid-body-{m}">
{#each month.weeks as weekDates, i}
<Calendar.GridRow data-testid="grid-row-{m}-{i}" data-week>
{#each weekDates as date, d}
<Calendar.Cell {date} data-testid="cell-{date.month}-{d}">
<Calendar.Date
{date}
month={month.value}
data-testid="date-{date.month}-{date.day}"
>
{date.day}
</Calendar.Date>
</Calendar.Cell>
{/each}
</Calendar.GridRow>
{/each}
</Calendar.GridBody>
</Calendar.Grid>
{/each}
</div>
</Calendar.Root>
</main>

0 comments on commit e4eb83c

Please sign in to comment.