11import React from 'react' ;
22
3+ import { ActivityIndicator } from 'react-native' ;
4+
35import { act , cleanup , fireEvent , render , screen , waitFor } from '@testing-library/react-native' ;
46
57import { OverlayProvider } from '../../../contexts' ;
@@ -46,6 +48,19 @@ const renderComponent = ({ client, channel, props }) => {
4648 ) ;
4749} ;
4850
51+ const setPendingUploads = ( client , uploads ) => {
52+ act ( ( ) => {
53+ client . uploadManager . state . partialNext ( {
54+ uploads : Object . fromEntries (
55+ uploads . map ( ( { id, uploadProgress } ) => [ id , { id, uploadProgress } ] ) ,
56+ ) ,
57+ } ) ;
58+ } ) ;
59+ } ;
60+
61+ const countActivityIndicators = ( nodes ) =>
62+ nodes . reduce ( ( count , node ) => count + node . findAllByType ( ActivityIndicator ) . length , 0 ) ;
63+
4964describe ( 'AttachmentUploadPreviewList' , ( ) => {
5065 let client ;
5166 let channel ;
@@ -60,6 +75,7 @@ describe('AttachmentUploadPreviewList', () => {
6075 jest . clearAllMocks ( ) ;
6176 cleanup ( ) ;
6277 act ( ( ) => {
78+ client ?. uploadManager ?. reset ( ) ;
6379 channel . messageComposer . attachmentManager . initState ( ) ;
6480 } ) ;
6581 } ) ;
@@ -103,7 +119,11 @@ describe('AttachmentUploadPreviewList', () => {
103119 it ( 'should render FileAttachmentUploadPreview when the sound package is unavailable' , async ( ) => {
104120 const attachments = [
105121 generateAudioAttachment ( {
122+ asset_url : undefined ,
106123 localMetadata : {
124+ file : {
125+ uri : 'file://audio-attachment.mp3' ,
126+ } ,
107127 id : 'audio-attachment' ,
108128 uploadState : FileState . UPLOADING ,
109129 } ,
@@ -115,28 +135,36 @@ describe('AttachmentUploadPreviewList', () => {
115135 act ( ( ) => {
116136 channel . messageComposer . attachmentManager . upsertAttachments ( attachments ) ;
117137 } ) ;
138+ setPendingUploads ( client , [ { id : 'audio-attachment' } ] ) ;
118139
119140 renderComponent ( { channel, client, props } ) ;
120141
121- const { queryAllByTestId } = screen ;
142+ const { getAllByTestId , queryAllByTestId } = screen ;
122143
123144 await waitFor ( ( ) => {
124145 expect ( queryAllByTestId ( 'file-attachment-upload-preview' ) ) . toHaveLength ( 1 ) ;
125- expect ( queryAllByTestId ( 'upload-progress-indicator ') ) . toHaveLength ( 1 ) ;
146+ expect ( countActivityIndicators ( getAllByTestId ( 'file-attachment-upload-preview ') ) ) . toBe ( 1 ) ;
126147 } ) ;
127148 } ) ;
128149
129150 describe ( 'FileAttachmentUploadPreview' , ( ) => {
130151 it ( 'should render FileAttachmentUploadPreview with all uploading files' , async ( ) => {
131152 const attachments = [
132153 generateFileAttachment ( {
154+ asset_url : undefined ,
133155 localMetadata : {
156+ file : {
157+ uri : 'file://file-attachment.xls' ,
158+ } ,
134159 id : 'file-attachment' ,
135160 uploadState : FileState . UPLOADING ,
136161 } ,
137162 } ) ,
138163 generateVideoAttachment ( {
139164 localMetadata : {
165+ file : {
166+ uri : 'file://video-attachment.mp4' ,
167+ } ,
140168 id : 'video-attachment' ,
141169 uploadState : FileState . UPLOADING ,
142170 } ,
@@ -147,14 +175,15 @@ describe('AttachmentUploadPreviewList', () => {
147175 act ( ( ) => {
148176 channel . messageComposer . attachmentManager . upsertAttachments ( attachments ) ;
149177 } ) ;
178+ setPendingUploads ( client , [ { id : 'file-attachment' } , { id : 'video-attachment' } ] ) ;
150179
151180 renderComponent ( { channel, client, props } ) ;
152181
153182 const { getAllByTestId, queryAllByTestId } = screen ;
154183
155184 await waitFor ( ( ) => {
156185 expect ( queryAllByTestId ( 'file-attachment-upload-preview' ) ) . toHaveLength ( 2 ) ;
157- expect ( queryAllByTestId ( 'upload-progress-indicator ') ) . toHaveLength ( 2 ) ;
186+ expect ( countActivityIndicators ( getAllByTestId ( 'file-attachment-upload-preview ') ) ) . toBe ( 2 ) ;
158187 } ) ;
159188
160189 act ( ( ) => {
@@ -285,6 +314,7 @@ describe('AttachmentUploadPreviewList', () => {
285314 generateImageAttachment ( {
286315 localMetadata : {
287316 id : 'image-attachment' ,
317+ previewUri : 'file://image-attachment.png' ,
288318 uploadState : FileState . UPLOADING ,
289319 } ,
290320 } ) ,
@@ -294,14 +324,15 @@ describe('AttachmentUploadPreviewList', () => {
294324 await act ( ( ) => {
295325 channel . messageComposer . attachmentManager . upsertAttachments ( attachments ?? [ ] ) ;
296326 } ) ;
327+ setPendingUploads ( client , [ { id : 'image-attachment' } ] ) ;
297328
298329 renderComponent ( { channel, client, props } ) ;
299330
300331 const { getAllByTestId, queryAllByTestId } = screen ;
301332
302333 await waitFor ( ( ) => {
303334 expect ( queryAllByTestId ( 'image-attachment-upload-preview' ) ) . toHaveLength ( 1 ) ;
304- expect ( queryAllByTestId ( 'upload-progress-indicator ') ) . toHaveLength ( 1 ) ;
335+ expect ( countActivityIndicators ( getAllByTestId ( 'image-attachment-upload-preview ') ) ) . toBe ( 1 ) ;
305336 } ) ;
306337
307338 await act ( ( ) => {
@@ -437,6 +468,7 @@ describe('AttachmentUploadPreviewList', () => {
437468 generateImageAttachment ( {
438469 localMetadata : {
439470 id : 'image-attachment-1' ,
471+ previewUri : 'file://image-attachment-1.png' ,
440472 uploadState : FileState . UPLOADING ,
441473 } ,
442474 } ) ,
@@ -464,10 +496,11 @@ describe('AttachmentUploadPreviewList', () => {
464496 await act ( ( ) => {
465497 channel . messageComposer . attachmentManager . upsertAttachments ( attachments ?? [ ] ) ;
466498 } ) ;
499+ setPendingUploads ( client , [ { id : 'image-attachment-1' } ] ) ;
467500
468501 renderComponent ( { channel, client, props } ) ;
469502
470- const { queryAllByTestId } = screen ;
503+ const { getAllByTestId , queryAllByTestId } = screen ;
471504
472505 await waitFor ( ( ) => {
473506 const imageAttachments = queryAllByTestId ( 'image-attachment-upload-preview-image' ) ;
@@ -478,7 +511,7 @@ describe('AttachmentUploadPreviewList', () => {
478511
479512 await waitFor ( ( ) => {
480513 expect ( queryAllByTestId ( 'image-attachment-upload-preview' ) ) . toHaveLength ( 4 ) ;
481- expect ( queryAllByTestId ( 'upload-progress-indicator ') ) . toHaveLength ( 1 ) ;
514+ expect ( countActivityIndicators ( getAllByTestId ( 'image-attachment-upload-preview ') ) ) . toBe ( 1 ) ;
482515 expect ( queryAllByTestId ( 'retry-upload-progress-indicator' ) ) . toHaveLength ( 1 ) ;
483516 expect ( queryAllByTestId ( 'inline-not-supported-indicator' ) ) . toHaveLength ( 1 ) ;
484517 } ) ;
0 commit comments