8
8
*/
9
9
/* global File */
10
10
jest . autoMockOff ( ) ;
11
+ jest . mock ( 'http' ) ;
12
+ jest . mock ( 'https' ) ;
11
13
12
14
const ParseFile = require ( '../ParseFile' ) . default ;
13
15
const CoreManager = require ( '../CoreManager' ) ;
16
+ const EventEmitter = require ( '../EventEmitter' ) ;
17
+
18
+ const mockHttp = require ( 'http' ) ;
19
+ const mockHttps = require ( 'https' ) ;
14
20
15
21
function generateSaveMock ( prefix ) {
16
22
return function ( name , payload , options ) {
@@ -30,7 +36,8 @@ describe('ParseFile', () => {
30
36
beforeEach ( ( ) => {
31
37
CoreManager . setFileController ( {
32
38
saveFile : generateSaveMock ( 'http://files.parsetfss.com/a/' ) ,
33
- saveBase64 : generateSaveMock ( 'http://files.parsetfss.com/a/' )
39
+ saveBase64 : generateSaveMock ( 'http://files.parsetfss.com/a/' ) ,
40
+ saveUri : generateSaveMock ( 'http://files.parsetfss.com/a/' ) ,
34
41
} ) ;
35
42
} ) ;
36
43
@@ -48,6 +55,12 @@ describe('ParseFile', () => {
48
55
expect ( file . _source . type ) . toBe ( 'image/png' ) ;
49
56
} ) ;
50
57
58
+ it ( 'can create files with file uri' , ( ) => {
59
+ const file = new ParseFile ( 'parse-image' , { uri :'http://example.com/image.png' } ) ;
60
+ expect ( file . _source . format ) . toBe ( 'uri' ) ;
61
+ expect ( file . _source . uri ) . toBe ( 'http://example.com/image.png' ) ;
62
+ } ) ;
63
+
51
64
it ( 'can extract data type from base64 with data type containing a number' , ( ) => {
52
65
const file = new ParseFile ( 'parse.m4a' , {
53
66
base64 : 'data:audio/m4a;base64,ParseA=='
@@ -129,6 +142,17 @@ describe('ParseFile', () => {
129
142
} ) ;
130
143
} ) ;
131
144
145
+ it ( 'updates fields when saved with uri' , ( ) => {
146
+ const file = new ParseFile ( 'parse.png' , { uri : 'https://example.com/image.png' } ) ;
147
+ expect ( file . name ( ) ) . toBe ( 'parse.png' ) ;
148
+ expect ( file . url ( ) ) . toBe ( undefined ) ;
149
+ return file . save ( ) . then ( function ( result ) {
150
+ expect ( result ) . toBe ( file ) ;
151
+ expect ( result . name ( ) ) . toBe ( 'parse.png' ) ;
152
+ expect ( result . url ( ) ) . toBe ( 'http://files.parsetfss.com/a/parse.png' ) ;
153
+ } ) ;
154
+ } ) ;
155
+
132
156
it ( 'generates a JSON representation' , ( ) => {
133
157
const file = new ParseFile ( 'parse.txt' , { base64 : 'ParseA==' } ) ;
134
158
return file . save ( ) . then ( function ( result ) {
@@ -245,4 +269,86 @@ describe('FileController', () => {
245
269
expect ( f . url ( ) ) . toBe ( 'https://files.parsetfss.com/a//api.parse.com/1/files/parse.txt' ) ;
246
270
} ) ;
247
271
} ) ;
272
+
273
+ it ( 'saveUri without uri type' , ( ) => {
274
+ try {
275
+ defaultController . saveUri ( 'name' , { format : 'unknown' } ) ;
276
+ } catch ( error ) {
277
+ expect ( error . message ) . toBe ( 'saveUri can only be used with Uri-type sources.' ) ;
278
+ }
279
+ } ) ;
280
+
281
+ it ( 'saveUri with uri type' , async ( ) => {
282
+ const source = { format : 'uri' , uri : 'https://example.com/image.png' } ;
283
+ jest . spyOn (
284
+ defaultController ,
285
+ 'download'
286
+ )
287
+ . mockImplementationOnce ( ( ) => {
288
+ return Promise . resolve ( {
289
+ base64 : 'ParseA==' ,
290
+ contentType : 'image/png' ,
291
+ } ) ;
292
+ } ) ;
293
+
294
+ jest . spyOn ( defaultController , 'saveBase64' ) ;
295
+ await defaultController . saveUri ( 'fileName' , source , { } ) ;
296
+ expect ( defaultController . download ) . toHaveBeenCalledTimes ( 1 ) ;
297
+ expect ( defaultController . saveBase64 ) . toHaveBeenCalledTimes ( 1 ) ;
298
+ expect ( defaultController . saveBase64 . mock . calls [ 0 ] ) . toEqual ( [
299
+ 'fileName' ,
300
+ { format : 'base64' , base64 : 'ParseA==' , type : 'image/png' } ,
301
+ { }
302
+ ] ) ;
303
+ } ) ;
304
+
305
+ it ( 'download with base64 http' , async ( ) => {
306
+ const mockResponse = Object . create ( EventEmitter . prototype ) ;
307
+ EventEmitter . call ( mockResponse ) ;
308
+ mockResponse . setEncoding = function ( ) { }
309
+ mockResponse . headers = {
310
+ 'content-type' : 'image/png'
311
+ } ;
312
+ const spy = jest . spyOn ( mockHttp , 'get' )
313
+ . mockImplementationOnce ( ( uri , cb ) => {
314
+ cb ( mockResponse ) ;
315
+ mockResponse . emit ( 'data' , 'base64String' ) ;
316
+ mockResponse . emit ( 'end' ) ;
317
+ return {
318
+ on : function ( ) { }
319
+ } ;
320
+ } ) ;
321
+
322
+ const data = await defaultController . download ( 'http://example.com/image.png' ) ;
323
+ expect ( data . base64 ) . toBe ( 'base64String' ) ;
324
+ expect ( data . contentType ) . toBe ( 'image/png' ) ;
325
+ expect ( mockHttp . get ) . toHaveBeenCalledTimes ( 1 ) ;
326
+ expect ( mockHttps . get ) . toHaveBeenCalledTimes ( 0 ) ;
327
+ spy . mockRestore ( ) ;
328
+ } ) ;
329
+
330
+ it ( 'download with base64 https' , async ( ) => {
331
+ const mockResponse = Object . create ( EventEmitter . prototype ) ;
332
+ EventEmitter . call ( mockResponse ) ;
333
+ mockResponse . setEncoding = function ( ) { }
334
+ mockResponse . headers = {
335
+ 'content-type' : 'image/png'
336
+ } ;
337
+ const spy = jest . spyOn ( mockHttps , 'get' )
338
+ . mockImplementationOnce ( ( uri , cb ) => {
339
+ cb ( mockResponse ) ;
340
+ mockResponse . emit ( 'data' , 'base64String' ) ;
341
+ mockResponse . emit ( 'end' ) ;
342
+ return {
343
+ on : function ( ) { }
344
+ } ;
345
+ } ) ;
346
+
347
+ const data = await defaultController . download ( 'https://example.com/image.png' ) ;
348
+ expect ( data . base64 ) . toBe ( 'base64String' ) ;
349
+ expect ( data . contentType ) . toBe ( 'image/png' ) ;
350
+ expect ( mockHttp . get ) . toHaveBeenCalledTimes ( 0 ) ;
351
+ expect ( mockHttps . get ) . toHaveBeenCalledTimes ( 1 ) ;
352
+ spy . mockRestore ( ) ;
353
+ } ) ;
248
354
} ) ;
0 commit comments