77 buildSearchResult ,
88 buildUserAuth ,
99} from '../../helpers/test/testBuilders' ;
10- import { routes } from '../../types/generic/routes' ;
10+ import { routeChildren , routes } from '../../types/generic/routes' ;
1111import axios from 'axios' ;
1212import usePatient from '../../helpers/hooks/usePatient' ;
1313import * as ReactRouter from 'react-router-dom' ;
@@ -17,6 +17,8 @@ import { afterEach, beforeEach, describe, expect, it, vi, Mock, Mocked } from 'v
1717import SessionProvider , { Session } from '../../providers/sessionProvider/SessionProvider' ;
1818import { REPOSITORY_ROLE } from '../../types/generic/authRole' ;
1919import getDocumentSearchResults from '../../helpers/requests/getDocumentSearchResults' ;
20+ import getDocument from '../../helpers/requests/getDocument' ;
21+ import useConfig from '../../helpers/hooks/useConfig' ;
2022
2123const mockedUseNavigate = vi . fn ( ) ;
2224vi . mock ( 'react-router-dom' , async ( ) => ( {
@@ -31,10 +33,13 @@ vi.mock('../../helpers/hooks/useBaseAPIHeaders');
3133vi . mock ( '../../helpers/hooks/usePatient' ) ;
3234vi . mock ( '../../helpers/hooks/useConfig' ) ;
3335vi . mock ( '../../helpers/requests/getDocumentSearchResults' ) ;
36+ vi . mock ( '../../helpers/requests/getDocument' ) ;
3437
3538const mockedAxios = axios as Mocked < typeof axios > ;
3639const mockedUsePatient = usePatient as Mock ;
3740const mockedGetSearchResults = getDocumentSearchResults as Mock ;
41+ const mockedGetDocument = getDocument as Mock ;
42+ const mockedUseConfig = useConfig as Mock ;
3843const mockPatient = buildPatientDetails ( ) ;
3944
4045let history = createMemoryHistory ( {
@@ -52,6 +57,11 @@ describe('<DocumentSearchResultsPage />', () => {
5257
5358 import . meta. env . VITE_ENVIRONMENT = 'vitest' ;
5459 mockedUsePatient . mockReturnValue ( mockPatient ) ;
60+ mockedUseConfig . mockReturnValue ( {
61+ featureFlags : {
62+ uploadDocumentIteration3Enabled : true ,
63+ } ,
64+ } ) ;
5565 } ) ;
5666 afterEach ( ( ) => {
5767 vi . clearAllMocks ( ) ;
@@ -66,9 +76,7 @@ describe('<DocumentSearchResultsPage />', () => {
6676 ] ) (
6777 'renders the page after a successful response from api when role is $role' ,
6878 async ( { role, title } ) => {
69- mockedAxios . get . mockResolvedValue ( async ( ) => {
70- return Promise . resolve ( [ buildSearchResult ( ) ] ) ;
71- } ) ;
79+ mockedGetSearchResults . mockResolvedValue ( [ buildSearchResult ( ) ] ) ;
7280
7381 renderPage ( history , role ) ;
7482
@@ -118,6 +126,22 @@ describe('<DocumentSearchResultsPage />', () => {
118126 ) . not . toBeInTheDocument ( ) ;
119127 expect ( screen . queryByTestId ( 'delete-all-documents-btn' ) ) . not . toBeInTheDocument ( ) ;
120128 } ) ;
129+
130+ it ( 'displays a service error when document search fails with bad request' , async ( ) => {
131+ const errorResponse = {
132+ response : {
133+ status : 400 ,
134+ message : 'bad request' ,
135+ } ,
136+ } ;
137+ mockedGetSearchResults . mockRejectedValueOnce ( errorResponse ) ;
138+
139+ renderPage ( history ) ;
140+
141+ await waitFor ( ( ) => {
142+ expect ( screen . getByTestId ( 'service-error' ) ) . toBeInTheDocument ( ) ;
143+ } ) ;
144+ } ) ;
121145 } ) ;
122146
123147 describe ( 'Accessibility' , ( ) => {
@@ -191,6 +215,102 @@ describe('<DocumentSearchResultsPage />', () => {
191215 expect ( mockedUseNavigate ) . toHaveBeenCalledWith ( routes . SESSION_EXPIRED ) ;
192216 } ) ;
193217 } ) ;
218+
219+ it ( 'navigates to server error page when document search return 500 server error' , async ( ) => {
220+ const errorResponse = {
221+ response : {
222+ status : 500 ,
223+ message : 'An error occurred' ,
224+ } ,
225+ } ;
226+ mockedGetSearchResults . mockRejectedValueOnce ( errorResponse ) ;
227+
228+ renderPage ( history ) ;
229+
230+ await waitFor ( ( ) => {
231+ expect ( mockedUseNavigate ) . toHaveBeenCalledWith ( expect . stringContaining ( routes . SERVER_ERROR ) ) ;
232+ } ) ;
233+ } ) ;
234+
235+ it ( 'loads the document and navigates to view screen on view link clicked' , async ( ) => {
236+ mockedGetSearchResults . mockResolvedValue ( [ buildSearchResult ( ) ] ) ;
237+
238+ renderPage ( history ) ;
239+
240+ await waitFor ( ( ) => {
241+ expect (
242+ screen . queryByRole ( 'progressbar' , { name : 'Loading...' } ) ,
243+ ) . not . toBeInTheDocument ( ) ;
244+ } ) ;
245+
246+ const viewLink = screen . getByTestId ( 'view-0-link' ) ;
247+ await act ( async ( ) => {
248+ await userEvent . click ( viewLink ) ;
249+ } ) ;
250+
251+ expect ( mockedGetDocument ) . toHaveBeenCalledTimes ( 1 ) ;
252+ expect ( mockedUseNavigate ) . toHaveBeenCalledWith ( routeChildren . DOCUMENT_VIEW ) ;
253+ } ) ;
254+
255+ it ( 'navigates to server error when load document fails with 500' , async ( ) => {
256+ mockedGetSearchResults . mockResolvedValue ( [ buildSearchResult ( ) ] ) ;
257+
258+ const errorResponse = {
259+ response : {
260+ status : 500 ,
261+ message : 'server error' ,
262+ } ,
263+ } ;
264+ mockedGetDocument . mockRejectedValue ( errorResponse ) ;
265+
266+ renderPage ( history ) ;
267+
268+ await waitFor ( ( ) => {
269+ expect (
270+ screen . queryByRole ( 'progressbar' , { name : 'Loading...' } ) ,
271+ ) . not . toBeInTheDocument ( ) ;
272+ } ) ;
273+
274+ const viewLink = screen . getByTestId ( 'view-0-link' ) ;
275+ await act ( async ( ) => {
276+ await userEvent . click ( viewLink ) ;
277+ } ) ;
278+
279+ expect ( mockedGetDocument ) . toHaveBeenCalledTimes ( 1 ) ;
280+ expect ( mockedUseNavigate ) . toHaveBeenCalledWith ( routeChildren . DOCUMENT_VIEW ) ;
281+ expect ( mockedUseNavigate ) . toHaveBeenCalledWith (
282+ expect . stringContaining ( routes . SERVER_ERROR ) ,
283+ ) ;
284+ } ) ;
285+
286+ it ( 'navigates to session expired when load document fails with 403' , async ( ) => {
287+ mockedGetSearchResults . mockResolvedValue ( [ buildSearchResult ( ) ] ) ;
288+
289+ const errorResponse = {
290+ response : {
291+ status : 403 ,
292+ message : 'forbidden' ,
293+ } ,
294+ } ;
295+ mockedGetDocument . mockRejectedValue ( errorResponse ) ;
296+
297+ renderPage ( history ) ;
298+
299+ await waitFor ( ( ) => {
300+ expect (
301+ screen . queryByRole ( 'progressbar' , { name : 'Loading...' } ) ,
302+ ) . not . toBeInTheDocument ( ) ;
303+ } ) ;
304+
305+ const viewLink = screen . getByTestId ( 'view-0-link' ) ;
306+ await act ( async ( ) => {
307+ await userEvent . click ( viewLink ) ;
308+ } ) ;
309+
310+ expect ( mockedGetDocument ) . toHaveBeenCalledTimes ( 1 ) ;
311+ expect ( mockedUseNavigate ) . toHaveBeenCalledWith ( routeChildren . DOCUMENT_VIEW ) ;
312+ expect ( mockedUseNavigate ) . toHaveBeenCalledWith ( routes . SESSION_EXPIRED ) ;
313+ } ) ;
194314 } ) ;
195315
196316 const renderPage = ( history : History , role ?: REPOSITORY_ROLE ) : void => {
0 commit comments