Skip to content

Commit

Permalink
Adding TEFCA Viewer unittest for formatName (#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertandremitchell authored Jun 24, 2024
1 parent 161e5a8 commit a0f25ab
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
4 changes: 3 additions & 1 deletion containers/tefca-viewer/src/app/format-service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
58 changes: 57 additions & 1 deletion containers/tefca-viewer/src/app/tests/format-service.test.tsx
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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("");
});
});

0 comments on commit a0f25ab

Please sign in to comment.