diff --git a/containers/tefca-viewer/src/app/format-service.tsx b/containers/tefca-viewer/src/app/format-service.tsx index 29371f7aad..b241814bb7 100644 --- a/containers/tefca-viewer/src/app/format-service.tsx +++ b/containers/tefca-viewer/src/app/format-service.tsx @@ -54,7 +54,9 @@ export function formatCodeableConcept(concept: CodeableConcept | undefined) { export function formatName(names: HumanName[]): string { let name = ""; if (names.length > 0) { - name = names[0].given?.join(" ") + " " + names[0].family; + const givenNames = names[0].given?.filter((n) => n).join(" ") ?? ""; + const familyName = names[0].family ?? ""; + name = `${givenNames} ${familyName}`.trim(); } return name; } diff --git a/containers/tefca-viewer/src/app/tests/format-service.test.tsx b/containers/tefca-viewer/src/app/tests/format-service.test.tsx index bee1937690..cd3a00eaf6 100644 --- a/containers/tefca-viewer/src/app/tests/format-service.test.tsx +++ b/containers/tefca-viewer/src/app/tests/format-service.test.tsx @@ -1,4 +1,5 @@ -import { formatDate } from "@/app/format-service"; +import { formatDate, formatName } from "@/app/format-service"; +import { HumanName } from "fhir/r4"; describe("Format Date", () => { it("should return the correct formatted date", () => { @@ -30,3 +31,58 @@ describe("Format Date", () => { expect(result).toBeUndefined(); }); }); + +describe.only("formatName", () => { + it("should format a single HumanName correctly", () => { + const names: HumanName[] = [ + { + family: "Doe", + given: ["John"], + }, + ]; + const result = formatName(names); + expect(result).toBe("John Doe"); + }); + + it("should handle multiple given names correctly", () => { + const names: HumanName[] = [ + { + family: "Smith", + given: ["Jane", "Alice"], + }, + ]; + const result = formatName(names); + expect(result).toBe("Jane Alice Smith"); + }); + + it("should return an empty string if family name is missing", () => { + const names: HumanName[] = [ + { + given: ["John"], + }, + ]; + const result = formatName(names); + expect(result).toBe("John"); + }); + + it("should return an empty string if given names are missing", () => { + const names: HumanName[] = [ + { + family: "Doe", + }, + ]; + const result = formatName(names); + expect(result).toBe("Doe"); + }); + + it("should handle missing given and family names gracefully", () => { + const names: HumanName[] = [ + { + family: undefined, + given: [""], + }, + ]; + const result = formatName(names); + expect(result).toBe(""); + }); +});