Skip to content

Commit

Permalink
range calendar tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte committed Jul 7, 2024
1 parent 0c654b3 commit 3abe709
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions packages/bits-ui/src/tests/helpers/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function getSelectedDays(calendar: HTMLElement) {
return Array.from(calendar.querySelectorAll<HTMLElement>("[data-calendar-day][data-selected]"));
return Array.from(calendar.querySelectorAll<HTMLElement>("[data-bits-day][data-selected]"));
}

export function getSelectedDay(calendar: HTMLElement) {
return calendar.querySelector<HTMLElement>("[data-calendar-day][data-selected]") as HTMLElement;
return calendar.querySelector<HTMLElement>("[data-bits-day][data-selected]") as HTMLElement;
}
18 changes: 10 additions & 8 deletions packages/bits-ui/src/tests/range-calendar/RangeCalendar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { render } from "@testing-library/svelte";
import { render } from "@testing-library/svelte/svelte5";
import { userEvent } from "@testing-library/user-event";
import { axe } from "jest-axe";
import { describe, it } from "vitest";
import { CalendarDate, CalendarDateTime, toZoned } from "@internationalized/date";
import { tick } from "svelte";
import { getTestKbd } from "../utils.js";
import { getSelectedDays } from "../helpers/calendar.js";
import RangeCalendarTest from "./RangeCalendarTest.svelte";
import type { RangeCalendar } from "$lib/index.js";
import RangeCalendarTest, { type RangeCalendarTestProps } from "./RangeCalendarTest.svelte";

const kbd = getTestKbd();

Expand All @@ -32,14 +31,17 @@ const longWeekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "F
// prettier-ignore
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October" ,"November", "December"];

function setup(props: RangeCalendar.Props = {}) {
function setup(props: Partial<RangeCalendarTestProps> = {}) {
const user = userEvent.setup();
const returned = render(RangeCalendarTest, { ...props });
const calendar = returned.getByTestId("calendar");
expect(calendar).toBeVisible();
return { ...returned, user, calendar };
}

const SELECTED_DAY_SELECTOR = "[data-bits-day][data-selected]";
const SELECTED_ATTR = "data-selected";

describe("calendar", () => {
it("has no accessibility violations", async () => {
const { container } = render(RangeCalendarTest);
Expand All @@ -49,7 +51,7 @@ describe("calendar", () => {
it("respects a default value if provided - `CalendarDate`", async () => {
const { calendar, getByTestId } = setup({ value: calendarDateRange });

const selectedDays = calendar.querySelectorAll<HTMLElement>("[data-selected]");
const selectedDays = calendar.querySelectorAll<HTMLElement>(SELECTED_DAY_SELECTOR);
expect(selectedDays).toHaveLength(6);

const heading = getByTestId("heading");
Expand All @@ -59,7 +61,7 @@ describe("calendar", () => {
it("respects a default value if provided - `CalendarDateTime`", async () => {
const { calendar, getByTestId } = setup({ value: calendarDateTimeRange });

const selectedDays = calendar.querySelectorAll<HTMLElement>("[data-selected]");
const selectedDays = calendar.querySelectorAll<HTMLElement>(SELECTED_DAY_SELECTOR);
expect(selectedDays).toHaveLength(6);

const heading = getByTestId("heading");
Expand All @@ -69,7 +71,7 @@ describe("calendar", () => {
it("respects a default value if provided - `ZonedDateTime`", async () => {
const { calendar, getByTestId } = setup({ value: zonedDateTimeRange });

const selectedDays = calendar.querySelectorAll<HTMLElement>("[data-selected]");
const selectedDays = calendar.querySelectorAll<HTMLElement>(SELECTED_DAY_SELECTOR);
expect(selectedDays).toHaveLength(6);

const heading = getByTestId("heading");
Expand Down Expand Up @@ -291,7 +293,7 @@ describe("calendar", () => {
expect(thirdDayInMonth).toHaveAttribute("data-unavailable");
expect(thirdDayInMonth).toHaveAttribute("aria-disabled", "true");
await user.click(thirdDayInMonth);
expect(thirdDayInMonth).not.toHaveAttribute("data-selected");
expect(thirdDayInMonth).not.toHaveAttribute(SELECTED_ATTR);
});

it("formats the weekday labels correctly - `'narrow'`", async () => {
Expand Down
98 changes: 48 additions & 50 deletions packages/bits-ui/src/tests/range-calendar/RangeCalendarTest.svelte
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@
<script lang="ts">
import { RangeCalendar } from "$lib/index.js";
<script lang="ts" context="module">
import { RangeCalendar, type WithoutChildren } from "$lib/index.js";
type $$Props = RangeCalendar.Props;
export type RangeCalendarTestProps = WithoutChildren<RangeCalendar.RootProps>;
</script>

export let placeholder: $$Props["placeholder"] = undefined;
export let value: $$Props["value"] = undefined;
<script lang="ts">
let { placeholder, value, ...restProps }: RangeCalendarTestProps = $props();
</script>

<main>
<div data-testid="start-value">{value?.start}</div>
<div data-testid="end-value">{value?.end}</div>
<div data-testid="start-value">{String(value?.start)}</div>
<div data-testid="end-value">{String(value?.end)}</div>

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

0 comments on commit 3abe709

Please sign in to comment.