Skip to content

Commit 304a23b

Browse files
committed
MAGETWO-90917: Build stabilization Pull request 2
Fix file-uploader.test.js
1 parent 57eab04 commit 304a23b

File tree

1 file changed

+21
-206
lines changed

1 file changed

+21
-206
lines changed

dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/file-uploader.test.js

Lines changed: 21 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,17 @@
77

88
define([
99
'jquery',
10-
'underscore',
1110
'Magento_Ui/js/form/element/file-uploader'
12-
], function ($, _, FileUploader) {
11+
], function ($, FileUploader) {
1312
'use strict';
1413

1514
describe('Magento_Ui/js/form/element/file-uploader', function () {
16-
var component,
17-
originalJQueryFnInit;
15+
var component;
1816

1917
beforeEach(function () {
20-
originalJQueryFnInit = $.fn.init;
21-
2218
component = new FileUploader({
2319
dataScope: 'abstract'
2420
});
25-
26-
component.initUploader();
27-
});
28-
29-
afterEach(function () {
30-
$.fn.init = originalJQueryFnInit;
3121
});
3222

3323
describe('initUploader method', function () {
@@ -261,119 +251,6 @@ define([
261251
expect(component.addFile).toHaveBeenCalled();
262252
});
263253

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-
377254
it('should call uploaderConfig.stop when number of errors is equal to number of files', function () {
378255
var fakeEvent = {
379256
target: document.createElement('input')
@@ -386,7 +263,6 @@ define([
386263
originalFiles: [file]
387264
};
388265

389-
spyOn(component.uploaderConfig, 'stop');
390266
spyOn(component, 'isFileAllowed').and.callFake(function (fileArg) {
391267
expect(fileArg).toBe(file);
392268

@@ -395,11 +271,12 @@ define([
395271
message: 'Not awesome enough'
396272
};
397273
});
398-
274+
component.initUploader();
275+
spyOn(component.uploaderConfig, 'done');
276+
spyOn(component.uploaderConfig, 'stop');
399277
component.onBeforeFileUpload(fakeEvent, data);
400278
expect(component.uploaderConfig.stop).toHaveBeenCalled();
401279
});
402-
403280
it('should not call uploaderConfig.stop when number of errors is unequal to number of files', function () {
404281
var fakeEvent = {
405282
target: document.createElement('input')
@@ -415,6 +292,8 @@ define([
415292
originalFiles: [file, otherFileInQueue]
416293
};
417294

295+
component.initUploader();
296+
spyOn(component.uploaderConfig, 'done');
418297
spyOn(component.uploaderConfig, 'stop');
419298
spyOn(component, 'isFileAllowed').and.callFake(function (fileArg) {
420299
expect(fileArg).toBe(file);
@@ -430,92 +309,28 @@ define([
430309
});
431310
});
432311

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');
482315

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');
487317

488-
// assert error message in constructedMessage
489-
expect(constructedMessage.indexOf(error.message) !== -1).toBe(true);
490-
});
491-
});
318+
component.onElementRender(input);
492319

493-
component.onLoadingStop();
494-
expect(component.notifyError).toHaveBeenCalledTimes(1);
320+
expect(component.initUploader).toHaveBeenCalledWith(input);
495321
});
496322
});
497323

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');
514327

515-
component.isMultipleFiles = true;
328+
component.aggregateError('blah.jpg', 'File is too awesome');
516329

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+
});
519334
});
520335
});
521336
});

0 commit comments

Comments
 (0)