@@ -1002,6 +1002,73 @@ describe('AttachmentManager', () => {
10021002 } ) ;
10031003 } ) ;
10041004
1005+ describe ( 'waitForPendingAttachments' , ( ) => {
1006+ it ( 'resolves immediately when there are no pending or uploading attachments' , async ( ) => {
1007+ const {
1008+ messageComposer : { attachmentManager } ,
1009+ } = setup ( ) ;
1010+
1011+ await expect (
1012+ attachmentManager . waitForPendingAttachments ( ) ,
1013+ ) . resolves . toBeUndefined ( ) ;
1014+ } ) ;
1015+
1016+ it ( 'resolves when in-flight uploads finish' , async ( ) => {
1017+ const {
1018+ messageComposer : { attachmentManager } ,
1019+ mockChannel,
1020+ } = setup ( ) ;
1021+
1022+ let resolveUpload ! : ( value : { file : string ; thumb_url ?: string } ) => void ;
1023+ const uploadPromise = new Promise < { file : string ; thumb_url ?: string } > (
1024+ ( resolve ) => {
1025+ resolveUpload = resolve ;
1026+ } ,
1027+ ) ;
1028+ mockChannel . sendImage . mockImplementation ( ( ) => uploadPromise ) ;
1029+
1030+ const file = new File ( [ '' ] , 'test.jpg' , { type : 'image/jpeg' } ) ;
1031+ const local = await attachmentManager . fileToLocalUploadAttachment ( file ) ;
1032+ void attachmentManager . uploadAttachment ( local ) ;
1033+
1034+ await vi . waitFor ( ( ) => {
1035+ expect ( attachmentManager . uploadsInProgressCount ) . toBe ( 1 ) ;
1036+ } ) ;
1037+
1038+ const settled = attachmentManager . waitForPendingAttachments ( ) ;
1039+ resolveUpload ( { file : 'done-url' } ) ;
1040+ await expect ( settled ) . resolves . toBeUndefined ( ) ;
1041+ expect ( attachmentManager . successfulUploadsCount ) . toBe ( 1 ) ;
1042+ } ) ;
1043+
1044+ it ( 'resolves when in-flight uploads fail' , async ( ) => {
1045+ const {
1046+ messageComposer : { attachmentManager } ,
1047+ mockChannel,
1048+ } = setup ( ) ;
1049+
1050+ let rejectUpload ! : ( err : Error ) => void ;
1051+ const uploadPromise = new Promise < never > ( ( _ , reject ) => {
1052+ rejectUpload = reject ;
1053+ } ) ;
1054+ mockChannel . sendImage . mockImplementation ( ( ) => uploadPromise ) ;
1055+
1056+ const file = new File ( [ '' ] , 'test.jpg' , { type : 'image/jpeg' } ) ;
1057+ const local = await attachmentManager . fileToLocalUploadAttachment ( file ) ;
1058+ const uploadWork = attachmentManager . uploadAttachment ( local ) ;
1059+
1060+ await vi . waitFor ( ( ) => {
1061+ expect ( attachmentManager . uploadsInProgressCount ) . toBe ( 1 ) ;
1062+ } ) ;
1063+
1064+ const settled = attachmentManager . waitForPendingAttachments ( ) ;
1065+ rejectUpload ( new Error ( 'upload failed' ) ) ;
1066+ await expect ( settled ) . resolves . toBeUndefined ( ) ;
1067+ await uploadWork ;
1068+ expect ( attachmentManager . failedUploadsCount ) . toBe ( 1 ) ;
1069+ } ) ;
1070+ } ) ;
1071+
10051072 describe ( 'uploadAttachment' , ( ) => {
10061073 it ( 'should upload files successfully' , async ( ) => {
10071074 const {
0 commit comments