1
+ import * as ExistsModule from '../exists' ;
2
+ import { S3ApiClient } from './s3-api-client' ;
3
+
4
+ describe ( 'S3ApiClient' , ( ) => {
5
+ describe ( 'uploadFileToPresignedUrl' , ( ) => {
6
+ let s3ApiClient ;
7
+ let fakeReadStream ;
8
+ let fakeSuccessResponse ;
9
+
10
+ beforeEach ( ( ) => {
11
+ fakeReadStream = '🦦' ;
12
+ fakeSuccessResponse = { status : 200 } ;
13
+ spyOn ( ExistsModule , 'exists' ) . and . returnValue ( true ) ;
14
+ s3ApiClient = new S3ApiClient ( ) ;
15
+ s3ApiClient . _fs = jasmine . createSpyObj ( 'fs' , [ 'createReadStream' ] ) ;
16
+ s3ApiClient . _fs . createReadStream . and . returnValue ( fakeReadStream ) ;
17
+ s3ApiClient . _fetch = jasmine . createSpy ( ) ;
18
+ s3ApiClient . _fetch . and . resolveTo ( fakeSuccessResponse ) ;
19
+ } ) ;
20
+
21
+ it ( 'should throw if file does not exist' , async ( ) => {
22
+ const path = '/file/not/found' ;
23
+ ( < jasmine . Spy > ExistsModule . exists ) . and . returnValue ( false ) ;
24
+ await expectAsync ( s3ApiClient . uploadFileToPresignedUrl ( 'url' , path ) ) . toBeRejectedWithError ( `File does not exist at path: ${ path } !` ) ;
25
+ } ) ;
26
+
27
+ it ( 'should call fetch with presignedUrl' , async ( ) => {
28
+ const url = 'https://bugsplat.com' ;
29
+ const path = '/some/fake/path' ;
30
+
31
+ await s3ApiClient . uploadFileToPresignedUrl ( url , path ) ;
32
+
33
+ expect ( s3ApiClient . _fetch ) . toHaveBeenCalledWith ( url , jasmine . anything ( ) ) ;
34
+ } ) ;
35
+
36
+ it ( 'should call fetch with init containing method and headers' , async ( ) => {
37
+ const url = 'https://bugsplat.com' ;
38
+ const path = '/some/fake/path' ;
39
+ const size = 1337 ;
40
+
41
+ await s3ApiClient . uploadFileToPresignedUrl ( url , path , size ) ;
42
+
43
+ expect ( s3ApiClient . _fetch ) . toHaveBeenCalledWith (
44
+ jasmine . anything ( ) ,
45
+ jasmine . objectContaining ( {
46
+ method : 'PUT' ,
47
+ headers : {
48
+ 'content-type' : 'application/octet-stream' ,
49
+ 'content-length' : `${ size } `
50
+ } ,
51
+ body : fakeReadStream
52
+ } )
53
+ ) ;
54
+ } ) ;
55
+
56
+ it ( 'should return response' , async ( ) => {
57
+ const url = 'https://bugsplat.com' ;
58
+ const path = '/some/fake/path' ;
59
+ const size = 1337 ;
60
+
61
+ const response = await s3ApiClient . uploadFileToPresignedUrl ( url , path , size ) ;
62
+
63
+ expect ( response ) . toEqual ( fakeSuccessResponse ) ;
64
+ } ) ;
65
+
66
+ describe ( 'error' , ( ) => {
67
+ it ( 'should throw if response status is not 200' , async ( ) => {
68
+ const url = 'https://bugsplat.com' ;
69
+ const path = '/some/fake/path' ;
70
+ const size = 1337 ;
71
+ s3ApiClient . _fetch . and . resolveTo ( { status : 500 } ) ;
72
+
73
+ await expectAsync ( s3ApiClient . uploadFileToPresignedUrl ( url , path , size ) ) . toBeRejectedWithError ( `Error uploading ${ path } to presignedUrl` ) ;
74
+ } ) ;
75
+ } ) ;
76
+ } ) ;
77
+ } ) ;
0 commit comments