Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for all parts #39

Merged
merged 1 commit into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/calendar-month/calendar-month.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { CalendarMonth } from "../calendar-month/calendar-month.js";
import { fixture } from "atomico/test-dom";
import { PlainDate } from "../utils/temporal.js";
import { today } from "../utils/date.js";
import { toDate, today } from "../utils/date.js";

type MonthContextInstance = InstanceType<typeof CalendarMonthContext>;

Expand Down Expand Up @@ -105,9 +105,18 @@ describe("CalendarMonth", () => {

const selected = getSelectedDays(month);
expect(selected.length).to.eq(3);

expect(selected[0]).to.have.attribute("aria-label", "1 January");
expect(selected[0]!.part.contains("selected")).to.eq(true);
expect(selected[0]!.part.contains("range-start"));

expect(selected[1]).to.have.attribute("aria-label", "2 January");
expect(selected[1]!.part.contains("selected")).to.eq(true);
expect(selected[1]!.part.contains("range-inner"));

expect(selected[2]).to.have.attribute("aria-label", "3 January");
expect(selected[2]!.part.contains("selected")).to.eq(true);
expect(selected[2]!.part.contains("range-end"));
});
});

Expand All @@ -132,6 +141,7 @@ describe("CalendarMonth", () => {
const selected = getSelectedDays(month);
expect(selected.length).to.eq(1);
expect(selected[0]).to.have.attribute("aria-label", "1 January");
expect(selected[0]!.part.contains("selected")).to.eq(true);
});
});
});
Expand All @@ -146,21 +156,17 @@ describe("CalendarMonth", () => {
expect(title).not.to.eq(undefined);
});

it("marks selected day as pressed", async () => {
const month = await mount(
<Fixture
focusedDate={PlainDate.from("2020-01-01")}
value={PlainDate.from("2020-01-02")}
/>
);
const grid = getGrid(month);
it("marks today", async () => {
const month = await mount(<Fixture />);

// should be single selected element
const selected = getSelectedDays(month);
const todaysDate = toDate(today()).toLocaleDateString("en-GB", {
day: "numeric",
month: "long",
});
const button = getDayButton(month, todaysDate)!;

expect(selected.length).to.eq(1);
expect(selected[0]).to.have.trimmed.text("2");
expect(selected[0]).to.have.attribute("aria-label", "2 January");
expect(button.part.contains("today")).to.eq(true);
expect(button).to.have.attribute("aria-current", "date");
});

it("uses a roving tab index", async () => {
Expand Down Expand Up @@ -198,7 +204,7 @@ describe("CalendarMonth", () => {
expect(spy.last[0].detail.toString()).to.eq("2020-04-19");
});

it("cannot select a disabled date", async () => {
it("cannot select a disallowed date", async () => {
const spy = createSpy<(e: CustomEvent<PlainDate>) => void>();
const calendar = await mount(
<Fixture
Expand All @@ -210,6 +216,7 @@ describe("CalendarMonth", () => {

const day = getDayButton(calendar, "4 January")!;
expect(day.part.contains("disallowed")).to.eq(true);
expect(day).to.have.attribute("aria-disabled", "true");

await click(day);
expect(spy.called).to.eq(false);
Expand Down