From 0fb2b052be362082611bfc2d5ea057c7199dbc83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anthony=20Su=C3=A1rez?= Date: Fri, 7 Oct 2022 08:37:04 -0500 Subject: [PATCH 1/3] fix: Add test for Sunday .toBeDayOfWeek() bug --- test/lib/DateAssertion.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/lib/DateAssertion.test.ts b/test/lib/DateAssertion.test.ts index a21606c6..b073275f 100644 --- a/test/lib/DateAssertion.test.ts +++ b/test/lib/DateAssertion.test.ts @@ -29,6 +29,19 @@ describe("[Unit] DateAssertion.test.ts", () => { assert.deepStrictEqual(test.not.toBeDayOfWeek(1), test); }); }); + + context("when the day of the actual date is Sunday", () => { + context('when comparing to the "sunday" literal', () => { + it("does not raise an AssertionError", () => { + const actualDate = new Date(2022, 9, 2); // October 2nd, 2022 + const test = new DateAssertion(actualDate); + + assert.doesNotThrow(() => { + test.toBeDayOfWeek("sunday"); + }, AssertionError); + }); + }); + }); }); describe(".toMatchDateParts", () => { From 69b6564b745a6b97e956f44e42d9aa195107849a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anthony=20Su=C3=A1rez?= Date: Fri, 7 Oct 2022 08:39:42 -0500 Subject: [PATCH 2/3] fix: Convert "sunday" to 0 instead of 7 This makes the implementation consistent with JS' Date.getDay() and with DateAssertion.toBeDayOfWeek()'s documentation. --- src/lib/helpers/dates.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/helpers/dates.ts b/src/lib/helpers/dates.ts index 734ce742..5b0d8b55 100644 --- a/src/lib/helpers/dates.ts +++ b/src/lib/helpers/dates.ts @@ -2,13 +2,13 @@ import { DateOptions, DayOfWeek, Month } from "../DateAssertion.types"; export function dayOfWeekAsNumber (day: DayOfWeek): number { switch (day) { + case "sunday": return 0; case "monday": return 1; case "tuesday": return 2; case "wednesday": return 3; case "thursday": return 4; case "friday": return 5; case "saturday": return 6; - case "sunday": return 7; } } From 45a15f5cce13f1ddc74a191687cb9585e18f264a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anthony=20Su=C3=A1rez?= Date: Fri, 7 Oct 2022 08:50:30 -0500 Subject: [PATCH 3/3] docs: Improve DateAssertion.toBeDayOfWeek's docs This makes the meaning of number literals clear when using .toBeDayOfWeek(). --- src/lib/DateAssertion.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/DateAssertion.ts b/src/lib/DateAssertion.ts index 688b6949..ef573dc6 100644 --- a/src/lib/DateAssertion.ts +++ b/src/lib/DateAssertion.ts @@ -24,9 +24,9 @@ export class DateAssertion extends Assertion { } /** - * Check if a day of the week, might be string - * (e.g 'monday') or number (e.g 0-6), equals the - * day of the actual date. + * Check if a day of the week equals the day of the actual date. You can pass + * either a string (e.g. "monday"), or a number from 0 to 6, where 0 is + * Sunday and 6 is Saturday, as in Date.getDay(). * * @param dayOfWeek the day to compare with * @returns the assertion instance