From a9999262fc5396e4879f508af24efae2900d1e39 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 22 Jul 2024 17:13:34 +0200 Subject: [PATCH] refactor(date): move non-validation funcs to util, add tests --- .../__test__/testcases.ts | 9 ++-- .../anwaltlicheVertretung/guards.ts | 2 +- .../finanzielleAngaben/context.ts | 3 +- .../persoenlicheDaten/context.ts | 3 +- app/flows/fluggastrechteFormular/context.ts | 8 +-- .../stringReplacements.ts | 2 +- .../sections/__test__/footer.test.ts | 2 +- .../pdf/beratungshilfe/sections/footer.ts | 2 +- app/services/validation/date.ts | 33 +----------- app/util/__test__/date.test.ts | 53 +++++++++++++++++++ app/util/__test__/dateFormatting.ts | 13 ----- app/util/date.ts | 31 +++++++++++ .../anwaltlicheVertretung.ts | 2 +- 13 files changed, 99 insertions(+), 64 deletions(-) create mode 100644 app/util/__test__/date.test.ts delete mode 100644 app/util/__test__/dateFormatting.ts create mode 100644 app/util/date.ts diff --git a/app/flows/beratungshilfeFormular/anwaltlicheVertretung/__test__/testcases.ts b/app/flows/beratungshilfeFormular/anwaltlicheVertretung/__test__/testcases.ts index 44064ece0..d1159e5f1 100644 --- a/app/flows/beratungshilfeFormular/anwaltlicheVertretung/__test__/testcases.ts +++ b/app/flows/beratungshilfeFormular/anwaltlicheVertretung/__test__/testcases.ts @@ -1,10 +1,7 @@ import type { TestCases } from "~/flows/__test__/TestCases"; import { machine } from "~/flows/beratungshilfeFormular/__test__/testMachine"; import type { BeratungshilfeAnwaltlicheVertretung } from "~/flows/beratungshilfeFormular/anwaltlicheVertretung/context"; -import { - sevenDaysAgoDate, - thirtyDaysAgoDate, -} from "~/util/__test__/dateFormatting"; +import { addDays, today, toGermanDateFormat } from "~/util/date"; const rechtsproblemStart = "rechtsproblem/start"; const anwaltlicheVertretungStart = "anwaltliche-vertretung/start"; @@ -52,7 +49,7 @@ const cases = [ { anwaltskanzlei: "yes", beratungStattgefunden: "yes", - beratungStattgefundenDatum: sevenDaysAgoDate(), + beratungStattgefundenDatum: toGermanDateFormat(addDays(today(), -7)), }, [ anwaltlicheVertretungStart, @@ -67,7 +64,7 @@ const cases = [ { anwaltskanzlei: "yes", beratungStattgefunden: "yes", - beratungStattgefundenDatum: thirtyDaysAgoDate(), + beratungStattgefundenDatum: toGermanDateFormat(addDays(today(), -30)), }, [ anwaltlicheVertretungStart, diff --git a/app/flows/beratungshilfeFormular/anwaltlicheVertretung/guards.ts b/app/flows/beratungshilfeFormular/anwaltlicheVertretung/guards.ts index c35ba15fb..56a949f7e 100644 --- a/app/flows/beratungshilfeFormular/anwaltlicheVertretung/guards.ts +++ b/app/flows/beratungshilfeFormular/anwaltlicheVertretung/guards.ts @@ -1,6 +1,6 @@ import type { BeratungshilfeAnwaltlicheVertretung } from "~/flows/beratungshilfeFormular/anwaltlicheVertretung/context"; import type { GenericGuard, Guards } from "~/flows/guards.server"; -import { dateUTCFromGermanDateString, today } from "~/services/validation/date"; +import { dateUTCFromGermanDateString, today } from "~/util/date"; const anwaltskanzleiYes: GenericGuard = ({ context, diff --git a/app/flows/beratungshilfeFormular/finanzielleAngaben/context.ts b/app/flows/beratungshilfeFormular/finanzielleAngaben/context.ts index 24f290114..41fd80c51 100644 --- a/app/flows/beratungshilfeFormular/finanzielleAngaben/context.ts +++ b/app/flows/beratungshilfeFormular/finanzielleAngaben/context.ts @@ -2,7 +2,7 @@ import { z } from "zod"; import { staatlicheLeistungen } from "~/flows/beratungshilfeVorabcheck/context"; import { pageDataSchema } from "~/services/flow/pageDataSchema"; import { checkedOptional } from "~/services/validation/checkedCheckbox"; -import { addYears, createDateSchema, today } from "~/services/validation/date"; +import { createDateSchema } from "~/services/validation/date"; import { integerSchema } from "~/services/validation/integer"; import { buildMoneyValidationSchema } from "~/services/validation/money/buildMoneyValidationSchema"; import { optionalOrSchema } from "~/services/validation/optionalOrSchema"; @@ -13,6 +13,7 @@ import { YesNoAnswer, customRequiredErrorMessage, } from "~/services/validation/YesNoAnswer"; +import { addYears, today } from "~/util/date"; const Eigentuemer = z.enum( ["myself", "partner", "myselfAndPartner", "myselfAndSomeoneElse"], diff --git a/app/flows/beratungshilfeFormular/persoenlicheDaten/context.ts b/app/flows/beratungshilfeFormular/persoenlicheDaten/context.ts index a216340cf..135842456 100644 --- a/app/flows/beratungshilfeFormular/persoenlicheDaten/context.ts +++ b/app/flows/beratungshilfeFormular/persoenlicheDaten/context.ts @@ -1,7 +1,8 @@ import { z } from "zod"; -import { addYears, createDateSchema, today } from "~/services/validation/date"; +import { createDateSchema } from "~/services/validation/date"; import { optionalOrSchema } from "~/services/validation/optionalOrSchema"; import { phoneNumberSchema } from "~/services/validation/phoneNumber"; +import { addYears, today } from "~/util/date"; import type { GenericGuard } from "../../guards.server"; import { namePrivatPerson, adresse } from "../../persoenlicheDaten/context"; diff --git a/app/flows/fluggastrechteFormular/context.ts b/app/flows/fluggastrechteFormular/context.ts index ea5ddf2a3..6261cf62c 100644 --- a/app/flows/fluggastrechteFormular/context.ts +++ b/app/flows/fluggastrechteFormular/context.ts @@ -9,12 +9,7 @@ import { checkedOptional, checkedRequired, } from "~/services/validation/checkedCheckbox"; -import { - createDateSchema, - today, - addDays, - addYears, -} from "~/services/validation/date"; +import { createDateSchema } from "~/services/validation/date"; import { integerSchema } from "~/services/validation/integer"; import { stringOptionalSchema } from "~/services/validation/stringOptional"; import { stringRequiredSchema } from "~/services/validation/stringRequired"; @@ -23,6 +18,7 @@ import { customRequiredErrorMessage, YesNoAnswer, } from "~/services/validation/YesNoAnswer"; +import { addDays, addYears, today } from "~/util/date"; import { adresse, persoenlicheDaten, diff --git a/app/flows/fluggastrechteVorabcheck/stringReplacements.ts b/app/flows/fluggastrechteVorabcheck/stringReplacements.ts index d7071d899..963353a76 100644 --- a/app/flows/fluggastrechteVorabcheck/stringReplacements.ts +++ b/app/flows/fluggastrechteVorabcheck/stringReplacements.ts @@ -1,7 +1,7 @@ import airports from "data/airports/data.json"; import { getRouteCompensationBetweenAirports } from "~/services/airports/getRouteCompensationBetweenAirports"; import type { Translations } from "~/services/cms/index.server"; -import { toGermanDateFormat, today } from "~/services/validation/date"; +import { toGermanDateFormat, today } from "~/util/date"; import { getTranslationByKey } from "~/util/getTranslationByKey"; import type { FluggastrechtVorabcheckContext } from "./context"; diff --git a/app/services/pdf/beratungshilfe/sections/__test__/footer.test.ts b/app/services/pdf/beratungshilfe/sections/__test__/footer.test.ts index b651ec147..25633b191 100644 --- a/app/services/pdf/beratungshilfe/sections/__test__/footer.test.ts +++ b/app/services/pdf/beratungshilfe/sections/__test__/footer.test.ts @@ -1,5 +1,5 @@ import { getBeratungshilfeParameters } from "~/services/pdf/beratungshilfe/beratungshilfe.server"; -import { today, toGermanDateFormat } from "~/services/validation/date"; +import { today, toGermanDateFormat } from "~/util/date"; import { fillFooter } from "../footer"; describe("fillFooter", () => { diff --git a/app/services/pdf/beratungshilfe/sections/footer.ts b/app/services/pdf/beratungshilfe/sections/footer.ts index 916116fa8..5421cdcf9 100644 --- a/app/services/pdf/beratungshilfe/sections/footer.ts +++ b/app/services/pdf/beratungshilfe/sections/footer.ts @@ -1,6 +1,6 @@ import type { BeratungshilfePDF } from "data/pdf/beratungshilfe/beratungshilfe.generated"; import type { BeratungshilfeFormularContext } from "~/flows/beratungshilfeFormular"; -import { today, toGermanDateFormat } from "~/services/validation/date"; +import { today, toGermanDateFormat } from "~/util/date"; import { uppercaseFirstLetter } from "~/util/strings"; export function fillFooter( diff --git a/app/services/validation/date.ts b/app/services/validation/date.ts index e22ab598f..929ba8ea9 100644 --- a/app/services/validation/date.ts +++ b/app/services/validation/date.ts @@ -1,29 +1,6 @@ import isDate from "validator/lib/isDate"; import { z } from "zod"; - -export function dateUTCFromGermanDateString(date: string) { - const [day, month, year] = date.split("."); - return new Date(Date.UTC(Number(year), Number(month) - 1, Number(day))); -} - -export function addDays(date: Date, days: number) { - const result = new Date(date); - result.setDate(result.getDate() + days); - return result; -} - -export function addYears(date: Date, years: number) { - const result = new Date(date); - result.setFullYear(result.getFullYear() + years); - return result; -} - -export function today() { - const today = new Date(); - return new Date( - Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate()), - ); -} +import { dateUTCFromGermanDateString } from "~/util/date"; const isValidDate = (date: string) => isDate(date, { @@ -73,11 +50,3 @@ export const createDateSchema = (args?: { return dateString; }); }; - -export const toGermanDateFormat = (date: Date) => { - return date.toLocaleDateString("de", { - day: "2-digit", - month: "2-digit", - year: "numeric", - }); -}; diff --git a/app/util/__test__/date.test.ts b/app/util/__test__/date.test.ts new file mode 100644 index 000000000..4cba72c90 --- /dev/null +++ b/app/util/__test__/date.test.ts @@ -0,0 +1,53 @@ +import { + addDays, + addYears, + dateUTCFromGermanDateString, + toGermanDateFormat, + today, +} from "../date"; + +describe("date", () => { + describe("today()", () => { + it("should return an instance of Date", () => { + expect(today()).toBeInstanceOf(Date); + }); + }); + + describe("toGermanDateFormat()", () => { + it("formats correctly", () => { + expect(toGermanDateFormat(new Date("2000-01-01"))).toEqual("01.01.2000"); + }); + }); + + describe("dateUTCFromGermanDateString()", () => { + it("parses correctly", () => { + expect(dateUTCFromGermanDateString("01.01.2000")).toEqual( + new Date("2000-01-01"), + ); + }); + }); + + describe("addDays()", () => { + it("adds and subtracts correctly", () => { + expect(addDays(new Date("2000-01-01"), 1)).toEqual( + new Date("2000-01-02"), + ); + + expect(addDays(new Date("2000-01-01"), -1)).toEqual( + new Date("1999-12-31"), + ); + }); + }); + + describe("addYears()", () => { + it("adds and subtracts correctly", () => { + expect(addYears(new Date("2000-01-01"), 1)).toEqual( + new Date("2001-01-01"), + ); + + expect(addYears(new Date("2000-01-01"), -1)).toEqual( + new Date("1999-01-01"), + ); + }); + }); +}); diff --git a/app/util/__test__/dateFormatting.ts b/app/util/__test__/dateFormatting.ts deleted file mode 100644 index c26baa0c8..000000000 --- a/app/util/__test__/dateFormatting.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { toGermanDateFormat, today } from "~/services/validation/date"; - -export const sevenDaysAgoDate = () => { - const date = today(); - date.setDate(date.getDate() - 7); - return toGermanDateFormat(date); -}; - -export const thirtyDaysAgoDate = () => { - const date = today(); - date.setDate(date.getDate() - 30); - return toGermanDateFormat(date); -}; diff --git a/app/util/date.ts b/app/util/date.ts new file mode 100644 index 000000000..d02e7f546 --- /dev/null +++ b/app/util/date.ts @@ -0,0 +1,31 @@ +export function addDays(date: Date, days: number) { + const result = new Date(date); + result.setDate(result.getDate() + days); + return result; +} + +export function addYears(date: Date, years: number) { + const result = new Date(date); + result.setFullYear(result.getFullYear() + years); + return result; +} + +export function today() { + const today = new Date(); + return new Date( + Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate()), + ); +} + +export function dateUTCFromGermanDateString(date: string) { + const [day, month, year] = date.split("."); + return new Date(Date.UTC(Number(year), Number(month) - 1, Number(day))); +} + +export const toGermanDateFormat = (date: Date) => { + return date.toLocaleDateString("de", { + day: "2-digit", + month: "2-digit", + year: "numeric", + }); +}; diff --git a/tests/e2e/beratungshilfeFormular/anwaltlicheVertretung.ts b/tests/e2e/beratungshilfeFormular/anwaltlicheVertretung.ts index 76a0ca3be..db9525205 100644 --- a/tests/e2e/beratungshilfeFormular/anwaltlicheVertretung.ts +++ b/tests/e2e/beratungshilfeFormular/anwaltlicheVertretung.ts @@ -1,5 +1,5 @@ import { type Page } from "@playwright/test"; -import { today, toGermanDateFormat } from "~/services/validation/date"; +import { today, toGermanDateFormat } from "~/util/date"; import type { BeratungshilfeFormular } from "../pom/BeratungshilfeFormular"; import { expectPageToBeAccessible } from "../util/expectPageToBeAccessible";