Skip to content

Commit 67a571c

Browse files
committed
BN-31 | Refactor. Use Params To Fetch Patient UUID
1 parent ee64800 commit 67a571c

File tree

3 files changed

+38
-83
lines changed

3 files changed

+38
-83
lines changed

src/hooks/__tests__/usePatientUUID.test.ts

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import { renderHook } from '@testing-library/react';
2-
import { useLocation } from 'react-router-dom';
2+
import { useParams } from 'react-router-dom';
33
import { usePatientUUID } from '../usePatientUUID';
4-
import { extractFirstUuidFromPath } from '@utils/common';
5-
6-
// Mock react-router-dom's useLocation hook
4+
// Mock react-router-dom's useParams hook
75
jest.mock('react-router-dom', () => ({
8-
useLocation: jest.fn(),
9-
}));
10-
11-
// Mock the extractFirstUuidFromPath utility function
12-
jest.mock('@utils/common', () => ({
13-
extractFirstUuidFromPath: jest.fn(),
6+
useParams: jest.fn(),
147
}));
158

169
describe('usePatientUUID', () => {
@@ -19,36 +12,27 @@ describe('usePatientUUID', () => {
1912
jest.clearAllMocks();
2013
});
2114

22-
it('should call useLocation and extractFirstUuidFromPath', () => {
15+
it('should call useParams and return patientUuid', () => {
2316
// Arrange
24-
const mockLocation = {
25-
pathname: '/patients/123e4567-e89b-12d3-a456-426614174000',
17+
const mockParams = {
18+
patientUuid: '123e4567-e89b-12d3-a456-426614174000',
2619
};
27-
(useLocation as jest.Mock).mockReturnValue(mockLocation);
28-
(extractFirstUuidFromPath as jest.Mock).mockReturnValue(
29-
'123e4567-e89b-12d3-a456-426614174000',
30-
);
20+
(useParams as jest.Mock).mockReturnValue(mockParams);
3121

3222
// Act
3323
const { result } = renderHook(() => usePatientUUID());
3424

3525
// Assert
36-
expect(useLocation).toHaveBeenCalled();
37-
expect(extractFirstUuidFromPath).toHaveBeenCalledWith(
38-
mockLocation.pathname,
39-
);
26+
expect(useParams).toHaveBeenCalled();
4027
expect(result.current).toBe('123e4567-e89b-12d3-a456-426614174000');
4128
});
4229

43-
it('should return UUID when present in the path', () => {
30+
it('should return UUID when present in the params', () => {
4431
// Arrange
45-
const mockLocation = {
46-
pathname: '/patients/123e4567-e89b-12d3-a456-426614174000/details',
32+
const mockParams = {
33+
patientUuid: '123e4567-e89b-12d3-a456-426614174000',
4734
};
48-
(useLocation as jest.Mock).mockReturnValue(mockLocation);
49-
(extractFirstUuidFromPath as jest.Mock).mockReturnValue(
50-
'123e4567-e89b-12d3-a456-426614174000',
51-
);
35+
(useParams as jest.Mock).mockReturnValue(mockParams);
5236

5337
// Act
5438
const { result } = renderHook(() => usePatientUUID());
@@ -57,11 +41,10 @@ describe('usePatientUUID', () => {
5741
expect(result.current).toBe('123e4567-e89b-12d3-a456-426614174000');
5842
});
5943

60-
it('should return null when no UUID is present in the path', () => {
44+
it('should return null when no UUID is present in the params', () => {
6145
// Arrange
62-
const mockLocation = { pathname: '/patients/list' };
63-
(useLocation as jest.Mock).mockReturnValue(mockLocation);
64-
(extractFirstUuidFromPath as jest.Mock).mockReturnValue(null);
46+
const mockParams = {}; // No patientUuid in params
47+
(useParams as jest.Mock).mockReturnValue(mockParams);
6548

6649
// Act
6750
const { result } = renderHook(() => usePatientUUID());
@@ -70,11 +53,10 @@ describe('usePatientUUID', () => {
7053
expect(result.current).toBeNull();
7154
});
7255

73-
it('should handle empty pathname', () => {
56+
it('should handle empty patientUuid', () => {
7457
// Arrange
75-
const mockLocation = { pathname: '' };
76-
(useLocation as jest.Mock).mockReturnValue(mockLocation);
77-
(extractFirstUuidFromPath as jest.Mock).mockReturnValue(null);
58+
const mockParams = { patientUuid: '' };
59+
(useParams as jest.Mock).mockReturnValue(mockParams);
7860

7961
// Act
8062
const { result } = renderHook(() => usePatientUUID());
@@ -83,11 +65,10 @@ describe('usePatientUUID', () => {
8365
expect(result.current).toBeNull();
8466
});
8567

86-
it('should handle undefined pathname', () => {
68+
it('should handle undefined patientUuid', () => {
8769
// Arrange
88-
const mockLocation = { pathname: undefined };
89-
(useLocation as jest.Mock).mockReturnValue(mockLocation);
90-
(extractFirstUuidFromPath as jest.Mock).mockReturnValue(null);
70+
const mockParams = { patientUuid: undefined };
71+
(useParams as jest.Mock).mockReturnValue(mockParams);
9172

9273
// Act
9374
const { result } = renderHook(() => usePatientUUID());
@@ -96,17 +77,14 @@ describe('usePatientUUID', () => {
9677
expect(result.current).toBeNull();
9778
});
9879

99-
it('should handle complex paths with query parameters', () => {
80+
it('should handle patientUuid with additional params', () => {
10081
// Arrange
101-
const mockLocation = {
102-
pathname:
103-
'/dashboard/patients/123e4567-e89b-12d3-a456-426614174000/visits',
104-
search: '?date=2023-01-01',
82+
const mockParams = {
83+
patientUuid: '123e4567-e89b-12d3-a456-426614174000',
84+
visitId: '12345',
85+
date: '2023-01-01',
10586
};
106-
(useLocation as jest.Mock).mockReturnValue(mockLocation);
107-
(extractFirstUuidFromPath as jest.Mock).mockReturnValue(
108-
'123e4567-e89b-12d3-a456-426614174000',
109-
);
87+
(useParams as jest.Mock).mockReturnValue(mockParams);
11088

11189
// Act
11290
const { result } = renderHook(() => usePatientUUID());
@@ -115,16 +93,13 @@ describe('usePatientUUID', () => {
11593
expect(result.current).toBe('123e4567-e89b-12d3-a456-426614174000');
11694
});
11795

118-
it('should handle paths with hash fragments', () => {
96+
it('should handle patientUuid with special characters', () => {
11997
// Arrange
120-
const mockLocation = {
121-
pathname: '/patients/123e4567-e89b-12d3-a456-426614174000',
98+
const mockParams = {
99+
patientUuid: '123e4567-e89b-12d3-a456-426614174000',
122100
hash: '#medical-history',
123101
};
124-
(useLocation as jest.Mock).mockReturnValue(mockLocation);
125-
(extractFirstUuidFromPath as jest.Mock).mockReturnValue(
126-
'123e4567-e89b-12d3-a456-426614174000',
127-
);
102+
(useParams as jest.Mock).mockReturnValue(mockParams);
128103

129104
// Act
130105
const { result } = renderHook(() => usePatientUUID());

src/hooks/usePatientUUID.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { useLocation } from 'react-router-dom';
2-
import { extractFirstUuidFromPath } from '@utils/common';
1+
import { useParams } from 'react-router-dom';
32

4-
// TODO: Use Params to get the UUID
53
/**
6-
* Custom hook to extract the first UUID from the current URL
7-
*
8-
* @returns {string|null} The extracted UUID or null if not found
4+
* Hook to retrieve the patient UUID from the URL
5+
* @returns {string|null} The patient UUID or null if not found
6+
* @example
7+
* const patientUuid = usePatientUUID();
98
*/
109
export const usePatientUUID = (): string | null => {
11-
const location = useLocation();
12-
return extractFirstUuidFromPath(location.pathname);
10+
const params = useParams();
11+
return params.patientUuid || null;
1312
};

src/utils/common.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,3 @@ export const getFormattedError = (
7777
* @returns {string} A random ID
7878
*/
7979
export const generateId = () => Math.random().toString(36).substring(2, 9);
80-
81-
// TODO: Use Params to get the UUID
82-
/**
83-
* Extracts the first UUID found in a URL path
84-
*
85-
* @param {string} pathname - The URL pathname to extract UUID from
86-
* @returns {string|null} The extracted UUID or null if not found
87-
*/
88-
export const extractFirstUuidFromPath = (pathname: string): string | null => {
89-
if (!pathname || typeof pathname !== 'string') {
90-
return null;
91-
}
92-
93-
const uuidRegex =
94-
/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i;
95-
const match = pathname.match(uuidRegex);
96-
97-
return match ? match[0] : null;
98-
};

0 commit comments

Comments
 (0)