7
7
8
8
define ( [
9
9
'jquery' ,
10
- 'underscore' ,
11
10
'Magento_Ui/js/form/element/file-uploader'
12
- ] , function ( $ , _ , FileUploader ) {
11
+ ] , function ( $ , FileUploader ) {
13
12
'use strict' ;
14
13
15
14
describe ( 'Magento_Ui/js/form/element/file-uploader' , function ( ) {
16
- var component ,
17
- originalJQueryFnInit ;
15
+ var component ;
18
16
19
17
beforeEach ( function ( ) {
20
- originalJQueryFnInit = $ . fn . init ;
21
-
22
18
component = new FileUploader ( {
23
19
dataScope : 'abstract'
24
20
} ) ;
25
-
26
- component . initUploader ( ) ;
27
- } ) ;
28
-
29
- afterEach ( function ( ) {
30
- $ . fn . init = originalJQueryFnInit ;
31
21
} ) ;
32
22
33
23
describe ( 'initUploader method' , function ( ) {
@@ -261,119 +251,6 @@ define([
261
251
expect ( component . addFile ) . toHaveBeenCalled ( ) ;
262
252
} ) ;
263
253
264
- it ( 'calls aggregateError method if upload resulted in error' , function ( ) {
265
- spyOn ( component , 'aggregateError' ) ;
266
- spyOn ( component , 'addFile' ) ;
267
-
268
- component . onFileUploaded ( { } , {
269
- files : [ {
270
- name : 'hello.jpg'
271
- } ] ,
272
- result : {
273
- error : true
274
- }
275
- } ) ;
276
-
277
- expect ( component . aggregateError ) . toHaveBeenCalledWith ( 'hello.jpg' , true ) ;
278
- expect ( component . addFile ) . not . toHaveBeenCalled ( ) ;
279
- } ) ;
280
- } ) ;
281
-
282
- describe ( 'onElementRender handler' , function ( ) {
283
- it ( 'invokes initUploader method' , function ( ) {
284
- var input = document . createElement ( 'input' ) ;
285
-
286
- spyOn ( component , 'initUploader' ) ;
287
-
288
- component . onElementRender ( input ) ;
289
-
290
- expect ( component . initUploader ) . toHaveBeenCalledWith ( input ) ;
291
- } ) ;
292
- } ) ;
293
-
294
- describe ( 'aggregateError method' , function ( ) {
295
- it ( 'should append onto aggregatedErrors array when called' , function ( ) {
296
- spyOn ( component . aggregatedErrors , 'push' ) ;
297
-
298
- component . aggregateError ( 'blah.jpg' , 'File is too awesome' ) ;
299
-
300
- expect ( component . aggregatedErrors . push ) . toHaveBeenCalledWith ( {
301
- filename : 'blah.jpg' ,
302
- message : 'File is too awesome'
303
- } ) ;
304
- } ) ;
305
- } ) ;
306
-
307
- describe ( 'onBeforeFileUpload method' , function ( ) {
308
- it ( 'should call aggregateError when allowed.passed is false' , function ( ) {
309
- var fakeEvent = {
310
- target : document . createElement ( 'input' )
311
- } ,
312
- file = {
313
- name : 'hello.jpg'
314
- } ,
315
- data = {
316
- files : [ file ] ,
317
- originalFiles : [ file ]
318
- } ;
319
-
320
- spyOn ( component , 'aggregateError' ) ;
321
- spyOn ( component . uploaderConfig , 'stop' ) ;
322
- spyOn ( component , 'isFileAllowed' ) . and . callFake ( function ( fileArg ) {
323
- expect ( fileArg ) . toBe ( file ) ;
324
-
325
- return {
326
- passed : false ,
327
- message : 'Not awesome enough'
328
- } ;
329
- } ) ;
330
-
331
- component . onBeforeFileUpload ( fakeEvent , data ) ;
332
-
333
- expect ( component . aggregateError ) . toHaveBeenCalledWith ( file . name , 'Not awesome enough' ) ;
334
- } ) ;
335
-
336
- it ( 'should not call aggregateError when allowed.passed is true' , function ( ) {
337
- var targetInputEl = document . createElement ( 'input' ) ,
338
- fakeEvent = {
339
- target : targetInputEl
340
- } ,
341
- file = {
342
- name : 'hello.jpg'
343
- } ,
344
- data = {
345
- files : [ file ] ,
346
- originalFiles : [ file ]
347
- } ,
348
- spies = {
349
- fileupload : jasmine . createSpy ( ) . and . callFake ( function ( ) {
350
- return spies ;
351
- } ) ,
352
- done : jasmine . createSpy ( )
353
- } ;
354
-
355
- $ . fn . init = jasmine . createSpy ( ) . and . callFake ( function ( elArg ) {
356
- expect ( elArg ) . toBe ( targetInputEl ) ;
357
-
358
- return spies ;
359
- } ) ;
360
-
361
- spyOn ( component , 'aggregateError' ) ;
362
-
363
- spyOn ( component , 'isFileAllowed' ) . and . callFake ( function ( fileArg ) {
364
- expect ( fileArg ) . toBe ( file ) ;
365
-
366
- return {
367
- passed : true
368
- } ;
369
- } ) ;
370
-
371
- component . onBeforeFileUpload ( fakeEvent , data ) ;
372
- expect ( component . aggregateError ) . not . toHaveBeenCalled ( ) ;
373
- expect ( spies . fileupload ) . toHaveBeenCalledWith ( 'process' , data ) ;
374
- expect ( spies . done ) . toHaveBeenCalled ( ) ;
375
- } ) ;
376
-
377
254
it ( 'should call uploaderConfig.stop when number of errors is equal to number of files' , function ( ) {
378
255
var fakeEvent = {
379
256
target : document . createElement ( 'input' )
@@ -386,7 +263,6 @@ define([
386
263
originalFiles : [ file ]
387
264
} ;
388
265
389
- spyOn ( component . uploaderConfig , 'stop' ) ;
390
266
spyOn ( component , 'isFileAllowed' ) . and . callFake ( function ( fileArg ) {
391
267
expect ( fileArg ) . toBe ( file ) ;
392
268
@@ -395,11 +271,12 @@ define([
395
271
message : 'Not awesome enough'
396
272
} ;
397
273
} ) ;
398
-
274
+ component . initUploader ( ) ;
275
+ spyOn ( component . uploaderConfig , 'done' ) ;
276
+ spyOn ( component . uploaderConfig , 'stop' ) ;
399
277
component . onBeforeFileUpload ( fakeEvent , data ) ;
400
278
expect ( component . uploaderConfig . stop ) . toHaveBeenCalled ( ) ;
401
279
} ) ;
402
-
403
280
it ( 'should not call uploaderConfig.stop when number of errors is unequal to number of files' , function ( ) {
404
281
var fakeEvent = {
405
282
target : document . createElement ( 'input' )
@@ -415,6 +292,8 @@ define([
415
292
originalFiles : [ file , otherFileInQueue ]
416
293
} ;
417
294
295
+ component . initUploader ( ) ;
296
+ spyOn ( component . uploaderConfig , 'done' ) ;
418
297
spyOn ( component . uploaderConfig , 'stop' ) ;
419
298
spyOn ( component , 'isFileAllowed' ) . and . callFake ( function ( fileArg ) {
420
299
expect ( fileArg ) . toBe ( file ) ;
@@ -430,92 +309,28 @@ define([
430
309
} ) ;
431
310
} ) ;
432
311
433
- describe ( 'onLoadingStop method' , function ( ) {
434
- it ( 'should not call notifyError if aggregatedErrors is empty' , function ( ) {
435
- spyOn ( component , 'notifyError' ) ;
436
-
437
- component . onLoadingStop ( ) ;
438
-
439
- expect ( component . notifyError ) . not . toHaveBeenCalled ( ) ;
440
- } ) ;
441
-
442
- it ( 'should call notifyError with first aggregated error if !this.isMultipleFiles' , function ( ) {
443
- component . isMultipleFiles = false ;
444
- component . aggregatedErrors = [ {
445
- filename : 'blah.jpg' ,
446
- message : 'Not awesome enough'
447
- } ] ;
448
-
449
- spyOn ( component , 'notifyError' ) ;
450
-
451
- component . onLoadingStop ( ) ;
452
-
453
- expect ( component . notifyError ) . toHaveBeenCalledWith ( 'Not awesome enough' ) ;
454
- } ) ;
455
-
456
- it ( 'should call notifyError with first aggregated error if !this.isMultipleFiles' , function ( ) {
457
- component . isMultipleFiles = false ;
458
- component . aggregatedErrors = [ {
459
- filename : 'blah.jpg' ,
460
- message : 'Not awesome enough'
461
- } ] ;
462
-
463
- spyOn ( component , 'notifyError' ) ;
464
-
465
- component . onLoadingStop ( ) ;
466
-
467
- expect ( component . notifyError ) . toHaveBeenCalledWith ( 'Not awesome enough' ) ;
468
- } ) ;
469
-
470
- it ( 'should call notifyError once with all error messages if this.isMultipleFiles' , function ( ) {
471
- component . isMultipleFiles = true ;
472
- component . aggregatedErrors = [
473
- {
474
- filename : 'blah.jpg' ,
475
- message : 'Not awesome enough'
476
- } ,
477
- {
478
- filename : 'widget.png' ,
479
- message : 'Too awesome for storage'
480
- }
481
- ] ;
312
+ describe ( 'onElementRender handler' , function ( ) {
313
+ it ( 'invokes initUploader method' , function ( ) {
314
+ var input = document . createElement ( 'input' ) ;
482
315
483
- spyOn ( component , 'notifyError' ) . and . callFake ( function ( constructedMessage ) {
484
- _ . each ( component . aggregatedErrors , function ( error ) {
485
- // assert filename in constructedMessage
486
- expect ( constructedMessage . indexOf ( error . filename ) !== - 1 ) . toBe ( true ) ;
316
+ spyOn ( component , 'initUploader' ) ;
487
317
488
- // assert error message in constructedMessage
489
- expect ( constructedMessage . indexOf ( error . message ) !== - 1 ) . toBe ( true ) ;
490
- } ) ;
491
- } ) ;
318
+ component . onElementRender ( input ) ;
492
319
493
- component . onLoadingStop ( ) ;
494
- expect ( component . notifyError ) . toHaveBeenCalledTimes ( 1 ) ;
320
+ expect ( component . initUploader ) . toHaveBeenCalledWith ( input ) ;
495
321
} ) ;
496
322
} ) ;
497
323
498
- describe ( 'onFilesChoosed method' , function ( ) {
499
- it ( 'should splice files to length of 1 if isMultipleFiles is false' , function ( ) {
500
- var data = {
501
- files : [ { } , { } ] // 2 files
502
- } ;
503
-
504
- component . isMultipleFiles = false ;
505
-
506
- expect ( component . onFilesChoosed ( { } , data ) ) . toBe ( undefined ) ;
507
- expect ( data . files . length ) . toBe ( 1 ) ;
508
- } ) ;
509
-
510
- it ( 'should not splice files to length of 1 if isMultipleFiles is true' , function ( ) {
511
- var data = {
512
- files : [ { } , { } ] // 1 file
513
- } ;
324
+ describe ( 'aggregateError method' , function ( ) {
325
+ it ( 'should append onto aggregatedErrors array when called' , function ( ) {
326
+ spyOn ( component . aggregatedErrors , 'push' ) ;
514
327
515
- component . isMultipleFiles = true ;
328
+ component . aggregateError ( 'blah.jpg' , 'File is too awesome' ) ;
516
329
517
- expect ( component . onFilesChoosed ( { } , data ) ) . toBe ( undefined ) ;
518
- expect ( data . files . length ) . toBe ( 2 ) ;
330
+ expect ( component . aggregatedErrors . push ) . toHaveBeenCalledWith ( {
331
+ filename : 'blah.jpg' ,
332
+ message : 'File is too awesome'
333
+ } ) ;
519
334
} ) ;
520
335
} ) ;
521
336
} ) ;
0 commit comments