Skip to content

Commit 49e6c0b

Browse files
[PRME-188] fix and reuse full name logic from patient summary (#749)
1 parent 61fbfca commit 49e6c0b

File tree

7 files changed

+45
-5
lines changed

7 files changed

+45
-5
lines changed

app/src/components/blocks/_documentUpload/documentSelectStage/DocumentSelectStage.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { formatNhsNumber } from '../../../../helpers/utils/formatNhsNumber';
1717
import { routeChildren, routes } from '../../../../types/generic/routes';
1818
import { DOCUMENT_TYPE, UploadDocument } from '../../../../types/pages/UploadDocumentsPage/types';
1919
import DocumentSelectStage, { Props } from './DocumentSelectStage';
20+
import { getFormattedPatientFullName } from '../../../../helpers/utils/formatPatientFullName';
2021

2122
vi.mock('../../../../helpers/hooks/usePatient');
2223
vi.mock('react-router-dom', async () => {
@@ -133,7 +134,7 @@ describe('DocumentSelectStage', () => {
133134
.closest('.nhsuk-inset-text');
134135
expect(insetText).toBeInTheDocument();
135136

136-
const expectedFullName = `${patientDetails.familyName}, ${patientDetails.givenName}`;
137+
const expectedFullName = getFormattedPatientFullName(patientDetails);
137138
expect(screen.getByText(/Patient name/i)).toBeInTheDocument();
138139
expect(screen.getByText(expectedFullName)).toBeInTheDocument();
139140

app/src/components/blocks/_documentUpload/documentUploadCompleteStage/DocumentUploadCompleteStage.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { buildPatientDetails } from '../../../../helpers/test/testBuilders';
77
import { getFormattedDate } from '../../../../helpers/utils/formatDate';
88
import { formatNhsNumber } from '../../../../helpers/utils/formatNhsNumber';
99
import usePatient from '../../../../helpers/hooks/usePatient';
10+
import { getFormattedPatientFullName } from '../../../../helpers/utils/formatPatientFullName';
1011

1112
const mockNavigate = vi.fn();
1213
vi.mock('../../../../helpers/hooks/usePatient');
@@ -37,7 +38,7 @@ describe('DocumentUploadCompleteStage', () => {
3738
),
3839
).toBeInTheDocument();
3940

40-
const expectedFullName = `${patientDetails.familyName}, ${patientDetails.givenName}`;
41+
const expectedFullName = getFormattedPatientFullName(patientDetails);
4142
expect(screen.getByTestId("patient-name").textContent).toEqual("Patient name: " + expectedFullName)
4243

4344
const expectedNhsNumber = formatNhsNumber(patientDetails.nhsNumber);

app/src/components/blocks/_documentUpload/documentUploadCompleteStage/DocumentUploadCompleteStage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import useTitle from '../../../../helpers/hooks/useTitle';
55
import usePatient from '../../../../helpers/hooks/usePatient';
66
import { formatNhsNumber } from '../../../../helpers/utils/formatNhsNumber';
77
import { getFormattedDateFromString } from '../../../../helpers/utils/formatDate';
8+
import { getFormattedPatientFullName } from '../../../../helpers/utils/formatPatientFullName';
89

910
const DocumentUploadCompleteStage = () => {
1011
const navigate = useNavigate();
1112
const patientDetails = usePatient();
1213
const nhsNumber: string = patientDetails?.nhsNumber ?? '';
1314
const formattedNhsNumber = formatNhsNumber(nhsNumber);
1415
const dob: string = getFormattedDateFromString(patientDetails?.birthDate)
16+
const patientName = getFormattedPatientFullName(patientDetails);
1517

1618
useTitle({ pageTitle: 'Record upload complete' });
1719

18-
const patientName = `${patientDetails?.familyName}, ${patientDetails?.givenName}`
1920

2021
return (
2122
<div className="lloydgeorge_upload-complete" data-testid="upload-complete-page">

app/src/components/blocks/_documentUpload/documentUploadConfirmStage/DocumentUploadConfirmStage.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as ReactRouter from 'react-router-dom';
1313
import { MemoryHistory, createMemoryHistory } from 'history';
1414
import userEvent from '@testing-library/user-event';
1515
import { routeChildren, routes } from '../../../../types/generic/routes';
16+
import { getFormattedPatientFullName } from '../../../../helpers/utils/formatPatientFullName';
1617

1718
const mockedUseNavigate = vi.fn();
1819
vi.mock('../../../../helpers/hooks/usePatient');
@@ -100,7 +101,7 @@ describe('DocumentUploadCompleteStage', () => {
100101
.closest('.nhsuk-inset-text');
101102
expect(insetText).toBeInTheDocument();
102103

103-
const expectedFullName = `${patientDetails.familyName}, ${patientDetails.givenName}`;
104+
const expectedFullName = getFormattedPatientFullName(patientDetails);
104105
expect(screen.getByText(/Patient name/i)).toBeInTheDocument();
105106
expect(screen.getByText(expectedFullName)).toBeInTheDocument();
106107

app/src/components/generic/patientSummary/PatientSummary.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getFormattedDate } from '../../../helpers/utils/formatDate';
44
import { SummaryList, Tag } from 'nhsuk-react-components';
55
import type { PatientDetails } from '../../../types/generic/patientDetails';
66
import { formatNhsNumber } from '../../../helpers/utils/formatNhsNumber';
7+
import { getFormattedPatientFullName } from '../../../helpers/utils/formatPatientFullName';
78

89
/**
910
* Props for the PatientSummary component.
@@ -79,7 +80,7 @@ const Details: React.FC<{ item: PatientInfo }> = ({ item }) => {
7980
case PatientInfo.FULL_NAME:
8081
key = 'Patient name';
8182
elementId = 'patient-summary-full-name';
82-
value = `${patientDetails?.familyName}, ${patientDetails?.givenName?.join(' ')}`;
83+
value = getFormattedPatientFullName(patientDetails)
8384
break;
8485
case PatientInfo.NHS_NUMBER:
8586
key = 'NHS number';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { buildPatientDetails } from "../test/testBuilders";
2+
import { getFormattedPatientFullName } from "./formatPatientFullName";
3+
4+
const patientDetails = buildPatientDetails();
5+
6+
describe('getFormattedPatientFullName', () => {
7+
test.each([
8+
['Doe', ['John'], 'Doe, John'],
9+
['Doe', ['John,Michael'], 'Doe, John Michael'],
10+
[' Doe ', [' John , Michael '], 'Doe, John Michael'],
11+
['Doe', [''], 'Doe,'],
12+
['', ['John'], ', John'],
13+
['', [''], ',']
14+
])('should format "%s" and "%s" as "%s"', (familyName, givenName, expected) => {
15+
patientDetails.familyName = familyName;
16+
patientDetails.givenName = givenName;
17+
expect(getFormattedPatientFullName(patientDetails)).toBe(expected);
18+
});
19+
20+
it('should handle null input', () => {
21+
expect(getFormattedPatientFullName(null)).toBe(',');
22+
});
23+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { PatientDetails } from "../../types/generic/patientDetails";
2+
3+
export const getFormattedPatientFullName = (patientDetails: PatientDetails | null): string => {
4+
const familyName = patientDetails?.familyName?.trim() || '';
5+
6+
const givenNames = patientDetails?.givenName
7+
?.flatMap(name => name.split(','))
8+
.map(name => name.trim())
9+
.join(' ') || '';
10+
11+
return `${familyName}, ${givenNames}`.trim();
12+
};

0 commit comments

Comments
 (0)