From e8f2ca31875a9798fa779139a1dead24cdc1c95a Mon Sep 17 00:00:00 2001 From: laug <5089281+laug@users.noreply.github.com> Date: Sun, 1 Sep 2024 13:51:28 +0900 Subject: [PATCH] test: fix a test that would always fail if run on first day of month --- src/test/exclude_dates.test.tsx | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/test/exclude_dates.test.tsx b/src/test/exclude_dates.test.tsx index 39a435bef..4684873f5 100644 --- a/src/test/exclude_dates.test.tsx +++ b/src/test/exclude_dates.test.tsx @@ -1,14 +1,18 @@ import { render } from "@testing-library/react"; -import { subDays } from "date-fns"; +import { addDays, subDays } from "date-fns"; import React from "react"; import DatePicker from "../index"; describe("DatePicker", () => { - const excludeDates = [new Date(), subDays(new Date(), 1)]; + const today = new Date(); + // otherDate must be in same month, otherwise it will not be shown on the calendar + const otherDate = + today.getDate() === 1 ? addDays(today, 1) : subDays(today, 1); + const excludeDates = [today, otherDate]; const excludeDatesWithMessages = [ - { date: subDays(new Date(), 1), message: "This day is excluded" }, - { date: new Date(), message: "Today is excluded" }, + { date: otherDate, message: "This day is excluded" }, + { date: today, message: "Today is excluded" }, ]; it("should disable dates specified in excludeDates props", () => { @@ -40,11 +44,11 @@ describe("DatePicker", () => { ); expect(disabledTimeItems.length).toBe(excludeDatesWithMessages.length); - expect(disabledTimeItems[0]?.getAttribute("title")).toBe( - "This day is excluded", - ); - expect(disabledTimeItems[1]?.getAttribute("title")).toBe( - "Today is excluded", - ); + expect( + disabledTimeItems[today < otherDate ? 1 : 0]?.getAttribute("title"), + ).toBe("This day is excluded"); + expect( + disabledTimeItems[today < otherDate ? 0 : 1]?.getAttribute("title"), + ).toBe("Today is excluded"); }); });