@@ -2,7 +2,7 @@ import { render, waitFor, screen, RenderResult } from '@testing-library/react';
22import DocumentUploadConfirmStage from './DocumentUploadConfirmStage' ;
33import { formatNhsNumber } from '../../../../helpers/utils/formatNhsNumber' ;
44import { getFormattedDate } from '../../../../helpers/utils/formatDate' ;
5- import { buildDocumentConfig , buildPatientDetails } from '../../../../helpers/test/testBuilders' ;
5+ import { buildDocument , buildDocumentConfig , buildPatientDetails } from '../../../../helpers/test/testBuilders' ;
66import usePatient from '../../../../helpers/hooks/usePatient' ;
77import {
88 DOCUMENT_UPLOAD_STATE ,
@@ -11,7 +11,6 @@ import {
1111import * as ReactRouter from 'react-router-dom' ;
1212import { MemoryHistory , createMemoryHistory } from 'history' ;
1313import userEvent from '@testing-library/user-event' ;
14- import { routeChildren , routes } from '../../../../types/generic/routes' ;
1514import { getFormattedPatientFullName } from '../../../../helpers/utils/formatPatientFullName' ;
1615import { DOCUMENT_TYPE } from '../../../../helpers/utils/documentType' ;
1716import { getJourney } from '../../../../helpers/utils/urlManipulations' ;
@@ -34,22 +33,6 @@ vi.mock('react-router-dom', async () => {
3433
3534const mockedUseNavigate = vi . fn ( ) ;
3635
37- vi . mock ( './components/DocumentList' , async ( ) => {
38- const actual = await vi . importActual ( './components/DocumentList' ) ;
39- return {
40- ...actual ,
41- default : ( { documents } : { documents : UploadDocument [ ] } ) : React . JSX . Element => {
42- return (
43- < div data-testid = "document-list" >
44- Document List with
45- < span data-testid = "document-list-count" > { documents . length } </ span >
46- documents
47- </ div >
48- ) ;
49- } ,
50- } ;
51- } ) ;
52-
5336vi . mock ( '../documentUploadLloydGeorgePreview/DocumentUploadLloydGeorgePreview' , ( ) => ( {
5437 default : ( {
5538 documents,
@@ -80,9 +63,10 @@ let history = createMemoryHistory({
8063 initialIndex : 0 ,
8164} ) ;
8265
83- let docConfig = buildDocumentConfig ( ) ;
8466const mockConfirmFiles = vi . fn ( ) ;
8567
68+ let mockDocuments : UploadDocument [ ] = [ ] ;
69+
8670describe ( 'DocumentUploadConfirmStage' , ( ) => {
8771 beforeEach ( ( ) => {
8872 vi . mocked ( usePatient ) . mockReturnValue ( patientDetails ) ;
@@ -92,18 +76,19 @@ describe('DocumentUploadConfirmStage', () => {
9276 } ) ;
9377 afterEach ( ( ) => {
9478 vi . clearAllMocks ( ) ;
79+ mockDocuments = [ ] ;
9580 } ) ;
9681
9782 it ( 'renders' , async ( ) => {
98- renderApp ( history , 1 ) ;
83+ renderApp ( history ) ;
9984
10085 await waitFor ( async ( ) => {
10186 expect ( screen . getByText ( 'Check files are for the correct patient' ) ) . toBeInTheDocument ( ) ;
10287 } ) ;
10388 } ) ;
10489
10590 it ( 'should call confirmFiles when confirm button is clicked' , async ( ) => {
106- renderApp ( history , 1 ) ;
91+ renderApp ( history ) ;
10792
10893 await userEvent . click ( await screen . findByTestId ( 'confirm-button' ) ) ;
10994
@@ -113,17 +98,19 @@ describe('DocumentUploadConfirmStage', () => {
11398 } ) ;
11499
115100 it . each ( [
116- { fileCount : 3 , expectedPreviewCount : 3 , stitched : true } ,
117- { fileCount : 1 , expectedPreviewCount : 1 , stitched : false } ,
101+ { fileCount : 3 , expectedPreviewCount : 3 , docType : DOCUMENT_TYPE . LLOYD_GEORGE } ,
102+ { fileCount : 1 , expectedPreviewCount : 1 , docType : DOCUMENT_TYPE . EHR } ,
118103 ] ) (
119104 'should render correct number files in the preview %s' ,
120- async ( { fileCount, expectedPreviewCount, stitched } ) => {
121- docConfig = buildDocumentConfig ( {
122- snomedCode : DOCUMENT_TYPE . EHR ,
123- stitched,
124- } ) ;
125-
126- renderApp ( history , fileCount ) ;
105+ async ( { fileCount, expectedPreviewCount, docType } ) => {
106+ for ( let i = 1 ; i <= fileCount ; i ++ ) {
107+ mockDocuments . push ( buildDocument (
108+ new File ( [ 'file' ] , `file 1.pdf` , { type : 'application/pdf' } ) ,
109+ DOCUMENT_UPLOAD_STATE . SELECTED ,
110+ docType ,
111+ ) ) ;
112+ }
113+ renderApp ( history ) ;
127114
128115 await waitFor ( async ( ) => {
129116 expect ( screen . getByTestId ( 'lloyd-george-preview-count' ) . textContent ) . toBe (
@@ -133,9 +120,68 @@ describe('DocumentUploadConfirmStage', () => {
133120 } ,
134121 ) ;
135122
123+ it ( 'should hide preview when the previewed document is removed' , async ( ) => {
124+ mockDocuments . push ( buildDocument (
125+ new File ( [ 'file' ] , `file 1.pdf` , { type : 'application/pdf' } ) ,
126+ DOCUMENT_UPLOAD_STATE . SELECTED ,
127+ DOCUMENT_TYPE . EHR_ATTACHMENTS ,
128+ ) ) ;
129+ mockDocuments . push ( buildDocument (
130+ new File ( [ 'file' ] , `file 2.pdf` , { type : 'application/pdf' } ) ,
131+ DOCUMENT_UPLOAD_STATE . SELECTED ,
132+ DOCUMENT_TYPE . EHR_ATTACHMENTS ,
133+ ) ) ;
134+ renderApp ( history ) ;
135+
136+ const firstDocumentViewButton = await screen . findByTestId ( `preview-${ mockDocuments [ 0 ] . id } -button` ) ;
137+ expect ( firstDocumentViewButton ) . toBeInTheDocument ( ) ;
138+
139+ await userEvent . click ( firstDocumentViewButton ) ;
140+
141+ await waitFor ( ( ) => {
142+ expect ( screen . getByTestId ( 'lloyd-george-preview' ) ) . toBeInTheDocument ( ) ;
143+ } ) ;
144+
145+ const removeButton = screen . getByTestId ( `remove-${ mockDocuments [ 0 ] . id } -button` ) ;
146+ expect ( removeButton ) . toBeInTheDocument ( ) ;
147+
148+ await userEvent . click ( removeButton ) ;
149+
150+ await waitFor ( ( ) => {
151+ expect ( screen . queryByTestId ( 'lloyd-george-preview' ) ) . not . toBeInTheDocument ( ) ;
152+ } ) ;
153+ } ) ;
154+
155+ it ( 'should show preview when 1 pdf remains after removing a document' , async ( ) => {
156+ mockDocuments . push ( buildDocument (
157+ new File ( [ 'file' ] , `file 1.pdf` , { type : 'application/pdf' } ) ,
158+ DOCUMENT_UPLOAD_STATE . SELECTED ,
159+ DOCUMENT_TYPE . EHR_ATTACHMENTS ,
160+ ) ) ;
161+ mockDocuments . push ( buildDocument (
162+ new File ( [ 'file' ] , `file 2.txt` , { type : 'text/plain' } ) ,
163+ DOCUMENT_UPLOAD_STATE . SELECTED ,
164+ DOCUMENT_TYPE . EHR_ATTACHMENTS ,
165+ ) ) ;
166+ renderApp ( history ) ;
167+
168+ await waitFor ( ( ) => {
169+ expect ( screen . queryByTestId ( 'lloyd-george-preview' ) ) . not . toBeInTheDocument ( ) ;
170+ } ) ;
171+
172+ const removeButton = screen . getByTestId ( `remove-${ mockDocuments [ 0 ] . id } -button` ) ;
173+ expect ( removeButton ) . toBeInTheDocument ( ) ;
174+
175+ await userEvent . click ( removeButton ) ;
176+
177+ await waitFor ( ( ) => {
178+ expect ( screen . queryByTestId ( 'lloyd-george-preview' ) ) . not . toBeInTheDocument ( ) ;
179+ } ) ;
180+ } ) ;
181+
136182 describe ( 'Navigation' , ( ) => {
137183 it ( 'should navigate to previous screen when go back is clicked' , async ( ) => {
138- renderApp ( history , 1 ) ;
184+ renderApp ( history ) ;
139185
140186 userEvent . click ( await screen . findByTestId ( 'go-back-link' ) ) ;
141187
@@ -145,7 +191,7 @@ describe('DocumentUploadConfirmStage', () => {
145191 } ) ;
146192
147193 it ( 'renders patient summary fields is inset' , async ( ) => {
148- renderApp ( history , 1 ) ;
194+ renderApp ( history ) ;
149195
150196 const insetText = screen
151197 . getByText ( 'Make sure that all files uploaded are for this patient only:' )
@@ -179,7 +225,7 @@ describe('DocumentUploadConfirmStage', () => {
179225 } ) ;
180226
181227 it ( 'should still render all page elements correctly' , async ( ) => {
182- renderApp ( history , 1 ) ;
228+ renderApp ( history ) ;
183229
184230 await waitFor ( async ( ) => {
185231 expect (
@@ -191,22 +237,19 @@ describe('DocumentUploadConfirmStage', () => {
191237 } ) ;
192238 } ) ;
193239
194- const renderApp = ( history : MemoryHistory , docsLength : number ) : RenderResult => {
195- const documents : UploadDocument [ ] = [ ] ;
196- for ( let i = 1 ; i <= docsLength ; i ++ ) {
197- documents . push ( {
198- attempts : 0 ,
199- id : `${ i } ` ,
200- docType : DOCUMENT_TYPE . LLOYD_GEORGE ,
201- file : new File ( [ 'file' ] , `file ${ i } .pdf` , { type : 'application/pdf' } ) ,
202- state : DOCUMENT_UPLOAD_STATE . SELECTED ,
203- } ) ;
240+ const renderApp = ( history : MemoryHistory ) : RenderResult => {
241+ if ( mockDocuments . length === 0 ) {
242+ mockDocuments . push ( buildDocument (
243+ new File ( [ 'file' ] , `file 1.pdf` , { type : 'application/pdf' } ) ,
244+ DOCUMENT_UPLOAD_STATE . SELECTED ,
245+ DOCUMENT_TYPE . LLOYD_GEORGE ,
246+ ) ) ;
204247 }
205248
206249 return render (
207250 < ReactRouter . Router navigator = { history } location = { history . location } >
208251 < DocumentUploadConfirmStage
209- documents = { documents }
252+ documents = { mockDocuments }
210253 confirmFiles = { mockConfirmFiles }
211254 setDocuments = { ( ) => { } }
212255 />
0 commit comments