From 20f0496fa594784f5236a966d457f0ab9d8d30b5 Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Thu, 2 Dec 2021 15:39:46 +0000 Subject: [PATCH] fix: unit test for "should recognize dates of the month in the past but next month" (#1131) Co-authored-by: Dominik Pschenitschni Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/1131 Reviewed-by: konrad Co-authored-by: Dominik Pschenitschni Co-committed-by: Dominik Pschenitschni --- src/helpers/time/parseDate.ts | 5 +++-- src/modules/parseTaskText.test.js | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/helpers/time/parseDate.ts b/src/helpers/time/parseDate.ts index 218491a3..0994b5d2 100644 --- a/src/helpers/time/parseDate.ts +++ b/src/helpers/time/parseDate.ts @@ -297,7 +297,8 @@ const getDayFromText = (text: string) => { } } - const date = new Date() + const now = new Date() + const date = new Date(now) const day = parseInt(results[0]) date.setDate(day) @@ -309,7 +310,7 @@ const getDayFromText = (text: string) => { date.setDate(day) } - if (date < new Date()) { + if (date < now) { date.setMonth(date.getMonth() + 1) } diff --git a/src/modules/parseTaskText.test.js b/src/modules/parseTaskText.test.js index a4c9eba3..885f792d 100644 --- a/src/modules/parseTaskText.test.js +++ b/src/modules/parseTaskText.test.js @@ -208,7 +208,11 @@ describe('Parse Task Text', () => { expect(result.text).toBe('Lorem Ipsum') expect(result.date.getDate()).toBe(date.getDate()) - expect(result.date.getMonth()).toBe(result.date.getDate() === 31 ? date.getMonth() + 2 : date.getMonth() + 1) + + const nextMonthWithDate = result.date.getDate() === 31 + ? (date.getMonth() + 2) % 12 + : (date.getMonth() + 1) % 12 + expect(result.date.getMonth()).toBe(nextMonthWithDate) }) it('should recognize dates of the month in the future', () => { const nextDay = new Date(+new Date() + 60 * 60 * 24 * 1000)