Skip to content

Commit

Permalink
refactor(date): move non-validation funcs to util, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chohner committed Jul 22, 2024
1 parent 82bc960 commit a999926
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -52,7 +49,7 @@ const cases = [
{
anwaltskanzlei: "yes",
beratungStattgefunden: "yes",
beratungStattgefundenDatum: sevenDaysAgoDate(),
beratungStattgefundenDatum: toGermanDateFormat(addDays(today(), -7)),
},
[
anwaltlicheVertretungStart,
Expand All @@ -67,7 +64,7 @@ const cases = [
{
anwaltskanzlei: "yes",
beratungStattgefunden: "yes",
beratungStattgefundenDatum: thirtyDaysAgoDate(),
beratungStattgefundenDatum: toGermanDateFormat(addDays(today(), -30)),
},
[
anwaltlicheVertretungStart,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BeratungshilfeAnwaltlicheVertretung> = ({
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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"],
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
8 changes: 2 additions & 6 deletions app/flows/fluggastrechteFormular/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -23,6 +18,7 @@ import {
customRequiredErrorMessage,
YesNoAnswer,
} from "~/services/validation/YesNoAnswer";
import { addDays, addYears, today } from "~/util/date";
import {
adresse,
persoenlicheDaten,
Expand Down
2 changes: 1 addition & 1 deletion app/flows/fluggastrechteVorabcheck/stringReplacements.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion app/services/pdf/beratungshilfe/sections/footer.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
33 changes: 1 addition & 32 deletions app/services/validation/date.ts
Original file line number Diff line number Diff line change
@@ -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, {
Expand Down Expand Up @@ -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",
});
};
53 changes: 53 additions & 0 deletions app/util/__test__/date.test.ts
Original file line number Diff line number Diff line change
@@ -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"),
);
});
});
});
13 changes: 0 additions & 13 deletions app/util/__test__/dateFormatting.ts

This file was deleted.

31 changes: 31 additions & 0 deletions app/util/date.ts
Original file line number Diff line number Diff line change
@@ -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",
});
};
2 changes: 1 addition & 1 deletion tests/e2e/beratungshilfeFormular/anwaltlicheVertretung.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down

0 comments on commit a999926

Please sign in to comment.