11import { renderHook } from '@testing-library/react' ;
2- import { useLocation } from 'react-router-dom' ;
2+ import { useParams } from 'react-router-dom' ;
33import { 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
75jest . 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
169describe ( '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 ( ) ) ;
0 commit comments