@@ -630,4 +630,103 @@ describe('FilesManager', () => {
630
630
expect ( createSpy ) . toHaveBeenCalledWith ( 1 ) ;
631
631
} ) ;
632
632
} ) ;
633
+
634
+ describe ( 'delete' , ( ) => {
635
+ it ( 'should successfully delete a file by ID' , async ( ) => {
636
+ // Arrange
637
+ mockFetch . mockResolvedValueOnce ( {
638
+ ok : true ,
639
+ status : 200 ,
640
+ json : jest . fn ( ) . mockResolvedValueOnce ( { id : 1 , name : 'deleted-file.jpg' } ) ,
641
+ } ) ;
642
+
643
+ // Act
644
+ const result = await filesManager . delete ( 1 ) ;
645
+
646
+ // Assert
647
+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
648
+ const requestArg = mockFetch . mock . calls [ 0 ] [ 0 ] ;
649
+
650
+ // Check that the URL and method are correct
651
+ expect ( requestArg . url ) . toBe ( 'http://example.com/api/upload/files/1' ) ;
652
+ expect ( requestArg . method ) . toBe ( 'DELETE' ) ;
653
+
654
+ // Check the returned result
655
+ expect ( result ) . toEqual ( { id : 1 , name : 'deleted-file.jpg' } ) ;
656
+ } ) ;
657
+
658
+ it ( 'should throw FileNotFoundError when deleting a non-existent file' , async ( ) => {
659
+ // Arrange
660
+ const fileId = 999 ;
661
+ const mockRequest = new Request ( `http://example.com/api/upload/files/${ fileId } ` ) ;
662
+ const mockResponse = new Response ( 'Not Found' , { status : 404 , statusText : 'Not Found' } ) ;
663
+
664
+ // Create an HTTPNotFoundError that would be thrown by the HttpClient
665
+ const httpNotFoundError = new HTTPNotFoundError ( mockResponse , mockRequest ) ;
666
+ mockFetch . mockRejectedValueOnce ( httpNotFoundError ) ;
667
+
668
+ // Act & Assert
669
+ try {
670
+ await filesManager . delete ( fileId ) ;
671
+ fail ( 'Expected an error to be thrown' ) ;
672
+ } catch ( error ) {
673
+ // Check that the error is properly mapped to a FileNotFoundError
674
+ expect ( error ) . toBeInstanceOf ( FileNotFoundError ) ;
675
+ if ( error instanceof FileNotFoundError ) {
676
+ // Verify the error contains the file ID and a relevant message
677
+ expect ( error . fileId ) . toBe ( fileId ) ;
678
+ expect ( error . message ) . toContain ( `File with ID ${ fileId } not found` ) ;
679
+ // Verify the original request and response are preserved
680
+ expect ( error . request ) . toBe ( mockRequest ) ;
681
+ expect ( error . response ) . toBe ( mockResponse ) ;
682
+ }
683
+ }
684
+ } ) ;
685
+
686
+ it ( 'should throw FileForbiddenError when user does not have permission' , async ( ) => {
687
+ // Arrange
688
+ const fileId = 1 ;
689
+ const mockRequest = new Request ( `http://example.com/api/upload/files/${ fileId } ` ) ;
690
+ const mockResponse = new Response ( 'Forbidden' , { status : 403 , statusText : 'Forbidden' } ) ;
691
+
692
+ // Create an HTTPForbiddenError that would be thrown by the HttpClient
693
+ const httpForbiddenError = new HTTPForbiddenError ( mockResponse , mockRequest ) ;
694
+ mockFetch . mockRejectedValueOnce ( httpForbiddenError ) ;
695
+
696
+ // Act & Assert
697
+ try {
698
+ await filesManager . delete ( fileId ) ;
699
+ fail ( 'Expected an error to be thrown' ) ;
700
+ } catch ( error ) {
701
+ // Check that the error is properly mapped to a FileForbiddenError
702
+ expect ( error ) . toBeInstanceOf ( FileForbiddenError ) ;
703
+ if ( error instanceof FileForbiddenError ) {
704
+ // Verify the error contains the file ID and a relevant message
705
+ expect ( error . fileId ) . toBe ( fileId ) ;
706
+ expect ( error . message ) . toContain ( `Access to file with ID ${ fileId } is forbidden` ) ;
707
+ // Verify the original request and response are preserved
708
+ expect ( error . request ) . toBe ( mockRequest ) ;
709
+ expect ( error . response ) . toBe ( mockResponse ) ;
710
+ }
711
+ }
712
+ } ) ;
713
+
714
+ it ( 'should pass fileId to createFileHttpClient' , async ( ) => {
715
+ // Arrange
716
+ // Create spy on the private method using any
717
+ const createSpy = jest . spyOn ( filesManager as any , 'createFileHttpClient' ) ;
718
+
719
+ mockFetch . mockResolvedValueOnce ( {
720
+ ok : true ,
721
+ status : 200 ,
722
+ json : jest . fn ( ) . mockResolvedValueOnce ( { id : 1 , name : 'deleted-file.jpg' } ) ,
723
+ } ) ;
724
+
725
+ // Act
726
+ await filesManager . delete ( 1 ) ;
727
+
728
+ // Assert
729
+ expect ( createSpy ) . toHaveBeenCalledWith ( 1 ) ;
730
+ } ) ;
731
+ } ) ;
633
732
} ) ;
0 commit comments