From e0b17eef8268afdbc7f756a97739c178a612da69 Mon Sep 17 00:00:00 2001 From: ColinBuyck <53269332+ColinBuyck@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:24:33 -0800 Subject: [PATCH] fix: handle empty other race options (#4513) (#1031) --- api/src/utilities/application-export-helpers.ts | 16 +++++++++------- .../utilities/application-export-helpers.spec.ts | 6 ++++++ shared-helpers/__tests__/formKeys.test.ts | 11 +++++++++++ shared-helpers/src/utilities/formKeys.ts | 11 +++++++++-- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/api/src/utilities/application-export-helpers.ts b/api/src/utilities/application-export-helpers.ts index d86ed227a6..840785f4a4 100644 --- a/api/src/utilities/application-export-helpers.ts +++ b/api/src/utilities/application-export-helpers.ts @@ -543,6 +543,8 @@ export const getHouseholdCsvHeaders = ( */ export const convertDemographicRaceToReadable = (type: string): string => { const [rootKey, customValue = ''] = type.split(':'); + //only show colon if user entered a custom value + const customValueFormatted = customValue ? `:${customValue}` : ''; const typeMap = { asian: 'Asian', 'asian-chinese': 'Asian[Chinese]', @@ -554,39 +556,39 @@ export const convertDemographicRaceToReadable = (type: string): string => { 'asian-centralAsian': 'Asian[Central Asian]', 'asian-southAsian': 'Asian[South Asian]', 'asian-southeastAsian': 'Asian[Southeast Asian]', - 'asian-otherAsian': `Asian[Other Asian:${customValue}]`, + 'asian-otherAsian': `Asian[Other Asian${customValueFormatted}]`, black: 'Black', 'black-african': 'Black[African]', 'black-africanAmerican': 'Black[African American]', 'black-caribbeanCentralSouthAmericanMexican': 'Black[Caribbean, Central American, South American or Mexican]', - 'black-otherBlack': `Black[Other Black:${customValue}]`, + 'black-otherBlack': `Black[Other Black${customValueFormatted}]`, indigenous: 'Indigenous', 'indigenous-alaskanNative': 'Indigenous[Alaskan Native]', 'indigenous-nativeAmerican': 'Indigenous[American Indian/Native American]', 'indigenous-indigenousFromMexicoCaribbeanCentralSouthAmerica': 'Indigenous[Indigenous from Mexico, the Caribbean, Central America, or South America]', - 'indigenous-otherIndigenous': `Indigenous[Other Indigenous:${customValue}]`, + 'indigenous-otherIndigenous': `Indigenous[Other Indigenous${customValueFormatted}]`, latino: 'Latino', 'latino-caribbean': 'Latino[Caribbean]', 'latino-centralAmerican': 'Latino[Central American]', 'latino-mexican': 'Latino[Mexican]', 'latino-southAmerican': 'Latino[South American]', - 'latino-otherLatino': `Latino[Other Latino:${customValue}]`, + 'latino-otherLatino': `Latino[Other Latino${customValueFormatted}]`, middleEasternOrAfrican: 'Middle Eastern, West African or North African', 'middleEasternOrAfrican-northAfrican': 'Middle Eastern, West African or North African[North African]', 'middleEasternOrAfrican-westAsian': 'Middle Eastern, West African or North African[West Asian]', - 'middleEasternOrAfrican-otherMiddleEasternNorthAfrican': `Middle Eastern, West African or North African[Other Middle Eastern or North African:${customValue}]`, + 'middleEasternOrAfrican-otherMiddleEasternNorthAfrican': `Middle Eastern, West African or North African[Other Middle Eastern or North African${customValueFormatted}]`, pacificIslander: 'Pacific Islander', 'pacificIslander-chamorro': 'Pacific Islander[Chamorro]', 'pacificIslander-nativeHawaiian': 'Pacific Islander[Native Hawaiian]', 'pacificIslander-samoan': 'Pacific Islander[Samoan]', - 'pacificIslander-otherPacificIslander': `Pacific Islander[Other Pacific Islander:${customValue}]`, + 'pacificIslander-otherPacificIslander': `Pacific Islander[Other Pacific Islander${customValueFormatted}]`, white: 'White', 'white-european': 'White[European]', - 'white-otherWhite': `White[Other White:${customValue}]`, + 'white-otherWhite': `White[Other White${customValueFormatted}]`, }; return typeMap[rootKey] ?? rootKey; }; diff --git a/api/test/unit/utilities/application-export-helpers.spec.ts b/api/test/unit/utilities/application-export-helpers.spec.ts index badef9ac71..42d4f604b7 100644 --- a/api/test/unit/utilities/application-export-helpers.spec.ts +++ b/api/test/unit/utilities/application-export-helpers.spec.ts @@ -277,6 +277,12 @@ describe('Testing application export helpers', () => { ).toBe('Pacific Islander[Other Pacific Islander:Fijian]'); }); + it('tests convertDemographicRaceToReadable with valid type and empty custom value', () => { + expect(convertDemographicRaceToReadable('black-otherBlack')).toBe( + 'Black[Other Black]', + ); + }); + it('tests convertDemographicRaceToReadable with type not in typeMap', () => { const custom = 'This is a custom value'; expect(convertDemographicRaceToReadable(custom)).toBe(custom); diff --git a/shared-helpers/__tests__/formKeys.test.ts b/shared-helpers/__tests__/formKeys.test.ts index cde5d80940..b6eabcfd10 100644 --- a/shared-helpers/__tests__/formKeys.test.ts +++ b/shared-helpers/__tests__/formKeys.test.ts @@ -52,4 +52,15 @@ describe("formKeys helpers", () => { const expectedArray = ["A", "B", "C", "D: 1,2"] expect(fieldGroupObjectToArray(testObj, "root")).toStrictEqual(expectedArray) }) + + it("fieldGroupObjectToArray with empty additional inputs", () => { + const testObj = { + ["root-A"]: "A", + ["root-B"]: "B", + ["root-C"]: "C", + ["root-D"]: "", + } + const expectedArray = ["A", "B", "C", "D"] + expect(fieldGroupObjectToArray(testObj, "root")).toStrictEqual(expectedArray) + }) }) diff --git a/shared-helpers/src/utilities/formKeys.ts b/shared-helpers/src/utilities/formKeys.ts index c01b8e2f3a..df4444a5bf 100644 --- a/shared-helpers/src/utilities/formKeys.ts +++ b/shared-helpers/src/utilities/formKeys.ts @@ -184,10 +184,17 @@ export const fieldGroupObjectToArray = ( const modifiedArray: string[] = [] const getValue = (elem: string) => { const formSubKey = elem.substring(elem.indexOf("-") + 1) - return formSubKey === formObject[elem] ? formSubKey : `${formSubKey}: ${formObject[elem]}` + return formSubKey === formObject[elem] || formObject[elem] === "" + ? formSubKey + : `${formSubKey}: ${formObject[elem]}` } Object.keys(formObject) - .filter((formValue) => formValue.split("-")[0] === rootKey && formObject[formValue]) + .filter( + (formValue) => + formValue.split("-")[0] === rootKey && + //empty string handles selected checkbox fields with empty additionalText + (formObject[formValue] || formObject[formValue] === "") + ) .forEach((elem) => { if (formObject[elem].isArray) { formObject[elem].forEach(() => {