Skip to content

Commit

Permalink
fix: handle empty other race options (bloom-housing#4513) (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinBuyck authored Jan 2, 2025
1 parent fd460c0 commit e0b17ee
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
16 changes: 9 additions & 7 deletions api/src/utilities/application-export-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]',
Expand All @@ -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;
};
Expand Down
6 changes: 6 additions & 0 deletions api/test/unit/utilities/application-export-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions shared-helpers/__tests__/formKeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
11 changes: 9 additions & 2 deletions shared-helpers/src/utilities/formKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit e0b17ee

Please sign in to comment.