From fe0320941f150ec94552f850c7d800e7f54cab02 Mon Sep 17 00:00:00 2001 From: Tim Arney Date: Fri, 18 Oct 2024 06:13:52 -0400 Subject: [PATCH 1/2] add closing notice for preview page add tests add watch command --- .../form-builder/[id]/preview/Preview.tsx | 17 +++++---- .../(form filler)/id/[...props]/page.tsx | 2 +- .../{Notice.tsx => ClosingNotice.tsx} | 26 +++++++++++--- lib/utils/date/utcToEst.vitest.ts | 36 +++++++++++++++++++ package.json | 1 + vitest.config.mts | 4 +-- 6 files changed, 73 insertions(+), 13 deletions(-) rename components/clientComponents/forms/ClosingNotice/{Notice.tsx => ClosingNotice.tsx} (52%) create mode 100644 lib/utils/date/utcToEst.vitest.ts diff --git a/app/(gcforms)/[locale]/(form administration)/form-builder/[id]/preview/Preview.tsx b/app/(gcforms)/[locale]/(form administration)/form-builder/[id]/preview/Preview.tsx index f7a9ced157..9d65b6fdd7 100644 --- a/app/(gcforms)/[locale]/(form administration)/form-builder/[id]/preview/Preview.tsx +++ b/app/(gcforms)/[locale]/(form administration)/form-builder/[id]/preview/Preview.tsx @@ -9,6 +9,7 @@ import { FormProperties, PublicFormRecord } from "@lib/types"; import { RichText } from "@clientComponents/forms"; import { Button } from "@clientComponents/globals"; import { NextButton } from "@clientComponents/forms/NextButton/NextButton"; +import { ClosingNotice } from "@clientComponents/forms/ClosingNotice/ClosingNotice"; import { FormServerErrorCodes, @@ -39,12 +40,15 @@ export const Preview = ({ }) => { const { status } = useSession(); const { i18n } = useTranslation(["common", "confirmation"]); - const { id, getSchema, getIsPublished, getSecurityAttribute } = useTemplateStore((s) => ({ - id: s.id, - getSchema: s.getSchema, - getIsPublished: s.getIsPublished, - getSecurityAttribute: s.getSecurityAttribute, - })); + const { id, getSchema, getIsPublished, getSecurityAttribute, closingDate } = useTemplateStore( + (s) => ({ + id: s.id, + getSchema: s.getSchema, + getIsPublished: s.getIsPublished, + getSecurityAttribute: s.getSecurityAttribute, + closingDate: s.closingDate, + }) + ); const isPastClosingDate = useIsFormClosed(); @@ -170,6 +174,7 @@ export const Preview = ({ ) : (
+ {closingDate && }

{formRecord.form[localizeField(LocalizedFormProperties.TITLE, language)] || t("gcFormsTest", { ns: "form-builder" })} diff --git a/app/(gcforms)/[locale]/(form filler)/id/[...props]/page.tsx b/app/(gcforms)/[locale]/(form filler)/id/[...props]/page.tsx index b63080ab94..0205df189d 100644 --- a/app/(gcforms)/[locale]/(form filler)/id/[...props]/page.tsx +++ b/app/(gcforms)/[locale]/(form filler)/id/[...props]/page.tsx @@ -11,7 +11,7 @@ import { GCFormsProvider } from "@lib/hooks/useGCFormContext"; import { FormWrapper } from "./clientSide"; import { allowGrouping } from "@formBuilder/components/shared/right-panel/treeview/util/allowGrouping"; import { serverTranslation } from "@i18n"; -import { Notice as ClosingNotice } from "@clientComponents/forms/ClosingNotice/Notice"; +import { ClosingNotice } from "@clientComponents/forms/ClosingNotice/ClosingNotice"; export async function generateMetadata({ params: { locale, props }, diff --git a/components/clientComponents/forms/ClosingNotice/Notice.tsx b/components/clientComponents/forms/ClosingNotice/ClosingNotice.tsx similarity index 52% rename from components/clientComponents/forms/ClosingNotice/Notice.tsx rename to components/clientComponents/forms/ClosingNotice/ClosingNotice.tsx index a7c3d9eda9..29a3ab93b7 100644 --- a/components/clientComponents/forms/ClosingNotice/Notice.tsx +++ b/components/clientComponents/forms/ClosingNotice/ClosingNotice.tsx @@ -3,8 +3,15 @@ import { isFutureDate } from "lib/utils/date/isFutureDate"; import { useTranslation } from "@i18n/client"; import { formClosingDateEst } from "lib/utils/date/utcToEst"; +import { logMessage } from "@lib/logger"; -export const Notice = ({ closingDate, language }: { closingDate?: string; language: string }) => { +export const ClosingNotice = ({ + closingDate, + language, +}: { + closingDate?: string; + language: string; +}) => { const { t } = useTranslation("common"); if (!closingDate) { @@ -15,11 +22,22 @@ export const Notice = ({ closingDate, language }: { closingDate?: string; langua return null; } - const { month, day, year, hour, minute } = formClosingDateEst(closingDate, language); + let month, day, year, hour, minute; + + try { + ({ month, day, year, hour, minute } = formClosingDateEst(closingDate, language)); + } catch (error) { + logMessage.info("Unable to parse closing date", closingDate); + return null; + } + + if (!month || !day || !year || !hour || !minute) { + return null; + } return ( -
-
{t("closingNotice.title")}
+
+
{t("closingNotice.title")}

{t("closingNotice.text1")}
diff --git a/lib/utils/date/utcToEst.vitest.ts b/lib/utils/date/utcToEst.vitest.ts new file mode 100644 index 0000000000..a134658cc2 --- /dev/null +++ b/lib/utils/date/utcToEst.vitest.ts @@ -0,0 +1,36 @@ +import { describe, it, expect } from 'vitest'; +import { formClosingDateEst } from './utcToEst'; + +describe('utcToEst', () => { + it('should convert UTC date to EST date in English', () => { + const utcDate = '2023-10-01T12:00:00Z'; + const lang = 'en'; + const result = formClosingDateEst(utcDate, lang); + expect(result).toStrictEqual({ + "day": "01", + "hour": "08", + "minute": "00", + "month": "October", + "year": "2023", + }); + }); + + it('should convert UTC date to EST date in French', () => { + const utcDate = '2023-10-01T12:00:00Z'; + const lang = 'fr'; + const result = formClosingDateEst(utcDate, lang); + expect(result).toStrictEqual({ + "day": "01", + "hour": "08", + "minute": "00", + "month": "octobre", + "year": "2023", + }); + }); + + it('should handle invalid date input', () => { + const utcDate = 'invalid-date'; + const lang = 'en'; + expect(() => formClosingDateEst(utcDate, lang)).toThrow(); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index ae3407ea0f..f85fe1a0bb 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "format:check": "prettier --check .", "test": "jest", "test:vitest": "vitest run", + "test:watch:vitest": "vitest --watch", "test:watch": "DEBUG_PRINT_LIMIT=10000 jest --watch", "cypress": "cypress open", "cypress:run": "CYPRESS_DEBUG=true cypress run --browser chrome", diff --git a/vitest.config.mts b/vitest.config.mts index d78611a678..4415c389ef 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -6,7 +6,7 @@ export default defineConfig({ test: { globals: true, // migration from Jest - By default, vitest does not provide global APIs for explicitness environment: "node", - // TODO: remove include after complete Vitest migration - include: ["__vitests__/**/*.test.ts", "lib/vitests/**/*.test.ts"], + // Note: The following pattern .vi.test.ts has been added to avoid conflicts with jest tests co-located with the source code + include: ["__vitests__/**/*.test.ts", "lib/vitests/**/*.test.ts", "**/*.vitest.+(ts|tsx|js|jsx)"], }, }); From ae3147c918fdfe6857d6f27a31ec27660a00ea08 Mon Sep 17 00:00:00 2001 From: Tim Arney Date: Fri, 18 Oct 2024 08:16:57 -0400 Subject: [PATCH 2/2] update note --- vitest.config.mts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vitest.config.mts b/vitest.config.mts index 4415c389ef..36519cc4f7 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -6,7 +6,7 @@ export default defineConfig({ test: { globals: true, // migration from Jest - By default, vitest does not provide global APIs for explicitness environment: "node", - // Note: The following pattern .vi.test.ts has been added to avoid conflicts with jest tests co-located with the source code + // Note: The following pattern .vitest.ts has been added to avoid conflicts with jest tests co-located with the source code include: ["__vitests__/**/*.test.ts", "lib/vitests/**/*.test.ts", "**/*.vitest.+(ts|tsx|js|jsx)"], }, });