@@ -1358,4 +1358,106 @@ describe('ChannelService', () => {
13581358
13591359 expect ( spy ) . toHaveBeenCalledWith ( { channel1 : newMessage . created_at } ) ;
13601360 } ) ;
1361+
1362+ it ( 'should call custom #customFileUploadRequest and #customImageUploadRequest if provided' , async ( ) => {
1363+ await init ( ) ;
1364+ let channel ! : Channel < DefaultStreamChatGenerics > ;
1365+ service . activeChannel$ . pipe ( first ( ) ) . subscribe ( ( c ) => ( channel = c ! ) ) ;
1366+ const customImageUploadRequest = jasmine
1367+ . createSpy ( )
1368+ . and . callFake ( ( file : File ) => {
1369+ switch ( file . name ) {
1370+ case 'file_error.jpg' :
1371+ return Promise . reject ( new Error ( ) ) ;
1372+ default :
1373+ return Promise . resolve ( { file : 'url/to/image' } ) ;
1374+ }
1375+ } ) ;
1376+ const customFileUploadRequest = jasmine
1377+ . createSpy ( )
1378+ . and . callFake ( ( file : File ) => {
1379+ switch ( file . name ) {
1380+ case 'file_error.pdf' :
1381+ return Promise . reject ( new Error ( ) ) ;
1382+ default :
1383+ return Promise . resolve ( { file : 'url/to/pdf' } ) ;
1384+ }
1385+ } ) ;
1386+ service . customImageUploadRequest = customImageUploadRequest ;
1387+ service . customFileUploadRequest = customFileUploadRequest ;
1388+ spyOn ( channel , 'sendImage' ) ;
1389+ spyOn ( channel , 'sendFile' ) ;
1390+ const file1 = { name : 'food.png' } as File ;
1391+ const file2 = { name : 'file_error.jpg' } as File ;
1392+ const file3 = { name : 'menu.pdf' } as File ;
1393+ const file4 = { name : 'file_error.pdf' } as File ;
1394+ const attachments = [
1395+ { file : file1 , type : 'image' , state : 'uploading' } ,
1396+ { file : file2 , type : 'image' , state : 'uploading' } ,
1397+ { file : file3 , type : 'file' , state : 'uploading' } ,
1398+ { file : file4 , type : 'file' , state : 'uploading' } ,
1399+ ] as AttachmentUpload [ ] ;
1400+ const result = await service . uploadAttachments ( attachments ) ;
1401+ const expectedResult = [
1402+ {
1403+ file : file1 ,
1404+ state : 'success' ,
1405+ url : 'url/to/image' ,
1406+ type : 'image' ,
1407+ } ,
1408+ { file : file2 , state : 'error' , type : 'image' } ,
1409+ {
1410+ file : file3 ,
1411+ state : 'success' ,
1412+ url : 'url/to/pdf' ,
1413+ type : 'file' ,
1414+ } ,
1415+ { file : file4 , state : 'error' , type : 'file' } ,
1416+ ] ;
1417+
1418+ expect ( channel . sendImage ) . not . toHaveBeenCalled ( ) ;
1419+ expect ( channel . sendFile ) . not . toHaveBeenCalled ( ) ;
1420+
1421+ expectedResult . forEach ( ( r , i ) => {
1422+ expect ( r ) . toEqual ( result [ i ] ) ;
1423+ } ) ;
1424+ } ) ;
1425+
1426+ it ( 'should call custom #customImageDeleteRequest if provided' , async ( ) => {
1427+ await init ( ) ;
1428+ let channel ! : Channel < DefaultStreamChatGenerics > ;
1429+ service . activeChannel$ . pipe ( first ( ) ) . subscribe ( ( c ) => ( channel = c ! ) ) ;
1430+ const customImageDeleteRequest = jasmine . createSpy ( ) ;
1431+ service . customImageDeleteRequest = customImageDeleteRequest ;
1432+ spyOn ( channel , 'deleteImage' ) ;
1433+ const url = 'url/to/image' ;
1434+ await service . deleteAttachment ( {
1435+ url,
1436+ type : 'image' ,
1437+ state : 'success' ,
1438+ file : { } as any as File ,
1439+ } ) ;
1440+
1441+ expect ( customImageDeleteRequest ) . toHaveBeenCalledWith ( url , channel ) ;
1442+ expect ( channel . deleteImage ) . not . toHaveBeenCalled ( ) ;
1443+ } ) ;
1444+
1445+ it ( 'should call custom #customFileDeleteRequest if provided' , async ( ) => {
1446+ await init ( ) ;
1447+ let channel ! : Channel < DefaultStreamChatGenerics > ;
1448+ service . activeChannel$ . pipe ( first ( ) ) . subscribe ( ( c ) => ( channel = c ! ) ) ;
1449+ const customFileDeleteRequest = jasmine . createSpy ( ) ;
1450+ service . customFileDeleteRequest = customFileDeleteRequest ;
1451+ spyOn ( channel , 'deleteFile' ) ;
1452+ const url = 'url/to/file' ;
1453+ await service . deleteAttachment ( {
1454+ url,
1455+ type : 'file' ,
1456+ state : 'success' ,
1457+ file : { } as any as File ,
1458+ } ) ;
1459+
1460+ expect ( customFileDeleteRequest ) . toHaveBeenCalledWith ( url , channel ) ;
1461+ expect ( channel . deleteFile ) . not . toHaveBeenCalled ( ) ;
1462+ } ) ;
13611463} ) ;
0 commit comments