From f72ffa2f119f0b2644380e5c92f50dd2f05b6531 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 31 Aug 2016 16:32:14 +0200 Subject: [PATCH] Fix js unit tests for webdav put upload changes Signed-off-by: Roeland Jago Douma --- apps/files/js/file-upload.js | 16 ++++- apps/files/js/filelist.js | 8 +-- apps/files/tests/js/fileUploadSpec.js | 2 +- apps/files/tests/js/filelistSpec.js | 84 ++++++++++++--------------- 4 files changed, 54 insertions(+), 56 deletions(-) diff --git a/apps/files/js/file-upload.js b/apps/files/js/file-upload.js index 3242ae7fa6..b75505c5df 100644 --- a/apps/files/js/file-upload.js +++ b/apps/files/js/file-upload.js @@ -34,7 +34,12 @@ OC.FileUpload = function(uploader, data) { this.uploader = uploader; this.data = data; - var path = OC.joinPaths(this.uploader.fileList.getCurrentDirectory(), this.getFile().name); + var path = ''; + if (this.uploader.fileList) { + path = OC.joinPaths(this.uploader.fileList.getCurrentDirectory(), this.getFile().name); + } else { + path = this.getFile().name; + } this.id = 'web-file-upload-' + md5(path) + '-' + (new Date()).getTime(); }; OC.FileUpload.CONFLICT_MODE_DETECT = 0; @@ -364,6 +369,9 @@ OC.Uploader.prototype = _.extend({ * also see article @link http://blog.new-bamboo.co.uk/2012/01/10/ridiculously-simple-ajax-uploads-with-formdata */ _supportAjaxUploadWithProgress: function() { + if (window.TESTING) { + return true; + } return supportFileAPI() && supportAjaxUploadProgressEvents() && supportFormData(); // Is the File API supported? @@ -719,7 +727,6 @@ OC.Uploader.prototype = _.extend({ dropZone: options.dropZone, // restrict dropZone to content div autoUpload: false, sequentialUploads: true, - maxChunkSize: 10000000, //singleFileUploads is on by default, so the data.files array will always have length 1 /** * on first add of every selection @@ -920,7 +927,10 @@ OC.Uploader.prototype = _.extend({ // HTTP connection problem or other error OC.Notification.showTemporary(data.errorThrown, {timeout: 10}); } - upload.deleteUpload(); + + if (upload) { + upload.deleteUpload(); + } }, /** * called for every successful upload diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index 2d6bca80f1..d4a223058b 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -2724,16 +2724,16 @@ // add target dir data.targetDir = dir; } else { - // we are dropping somewhere inside the file list, which will - // upload the file to the current directory - data.targetDir = self.getCurrentDirectory(); - // cancel uploads to current dir if no permission var isCreatable = (self.getDirectoryPermissions() & OC.PERMISSION_CREATE) !== 0; if (!isCreatable) { self._showPermissionDeniedNotification(); return false; } + + // we are dropping somewhere inside the file list, which will + // upload the file to the current directory + data.targetDir = self.getCurrentDirectory(); } }); uploader.on('add', function(e, data) { diff --git a/apps/files/tests/js/fileUploadSpec.js b/apps/files/tests/js/fileUploadSpec.js index bfaf0a9fe5..19f8cde7e4 100644 --- a/apps/files/tests/js/fileUploadSpec.js +++ b/apps/files/tests/js/fileUploadSpec.js @@ -48,7 +48,7 @@ describe('OC.Upload tests', function() { $dummyUploader = $('#file_upload_start'); uploader = new OC.Uploader($dummyUploader); failStub = sinon.stub(); - $dummyUploader.on('fileuploadfail', failStub); + uploader.on('fail', failStub); }); afterEach(function() { $dummyUploader = undefined; diff --git a/apps/files/tests/js/filelistSpec.js b/apps/files/tests/js/filelistSpec.js index 55ab2c535a..2064f570ca 100644 --- a/apps/files/tests/js/filelistSpec.js +++ b/apps/files/tests/js/filelistSpec.js @@ -2564,15 +2564,12 @@ describe('OCA.Files.FileList tests', function() { * that were registered on the magic upload object */ describe('file upload', function() { - var $uploader; var uploadData; + var uploader; beforeEach(function() { - // note: this isn't the real blueimp file uploader from jquery.fileupload - // but it makes it possible to simulate the event triggering to - // test the response of the handlers - $uploader = $('#file_upload_start'); fileList.setFiles(testFiles); + uploader = fileList._uploader; // simulate data structure from jquery.upload uploadData = { files: [{ @@ -2582,7 +2579,7 @@ describe('OCA.Files.FileList tests', function() { }); afterEach(function() { - $uploader = null; + uploader = null; uploadData = null; }); @@ -2606,11 +2603,7 @@ describe('OCA.Files.FileList tests', function() { * @return event object including the result */ function addFile(data) { - var ev = new $.Event('fileuploadadd', {}); - // using triggerHandler instead of trigger so we can pass - // extra data - $uploader.triggerHandler(ev, data || {}); - return ev; + uploader.trigger('add', {}, data || {}); } it('sets target dir to the current directory', function() { @@ -2632,9 +2625,8 @@ describe('OCA.Files.FileList tests', function() { target: $target } }; - var ev = new $.Event('fileuploaddrop', eventData); - $uploader.trigger(ev, data || {}); - return ev; + uploader.trigger('drop', eventData, data || {}); + return !!data.targetDir; } it('drop on a tr or crumb outside file list does not trigger upload', function() { @@ -2642,62 +2634,62 @@ describe('OCA.Files.FileList tests', function() { var ev; $('#testArea').append($anotherTable); ev = dropOn($anotherTable.find('tr'), uploadData); - expect(ev.result).toEqual(false); + expect(ev).toEqual(false); - ev = dropOn($anotherTable.find('.crumb')); - expect(ev.result).toEqual(false); + ev = dropOn($anotherTable.find('.crumb'), uploadData); + expect(ev).toEqual(false); }); it('drop on an element outside file list container does not trigger upload', function() { var $anotherEl = $('
outside
'); var ev; $('#testArea').append($anotherEl); - ev = dropOn($anotherEl); + ev = dropOn($anotherEl, uploadData); - expect(ev.result).toEqual(false); + expect(ev).toEqual(false); }); it('drop on an element inside the table triggers upload', function() { var ev; ev = dropOn(fileList.$fileList.find('th:first'), uploadData); - expect(ev.result).not.toEqual(false); + expect(ev).not.toEqual(false); expect(uploadData.targetDir).toEqual('/subdir'); }); it('drop on an element on the table container triggers upload', function() { var ev; ev = dropOn($('#app-content-files'), uploadData); - expect(ev.result).not.toEqual(false); + expect(ev).not.toEqual(false); expect(uploadData.targetDir).toEqual('/subdir'); }); it('drop on an element inside the table does not trigger upload if no upload permission', function() { $('#permissions').val(0); var ev; - ev = dropOn(fileList.$fileList.find('th:first')); + ev = dropOn(fileList.$fileList.find('th:first'), uploadData); - expect(ev.result).toEqual(false); + expect(ev).toEqual(false); expect(notificationStub.calledOnce).toEqual(true); }); it('drop on an folder does not trigger upload if no upload permission on that folder', function() { var $tr = fileList.findFileEl('somedir'); var ev; $tr.data('permissions', OC.PERMISSION_READ); - ev = dropOn($tr); + ev = dropOn($tr, uploadData); - expect(ev.result).toEqual(false); + expect(ev).toEqual(false); expect(notificationStub.calledOnce).toEqual(true); }); it('drop on a file row inside the table triggers upload to current folder', function() { var ev; ev = dropOn(fileList.findFileEl('One.txt').find('td:first'), uploadData); - expect(ev.result).not.toEqual(false); + expect(ev).not.toEqual(false); expect(uploadData.targetDir).toEqual('/subdir'); }); it('drop on a folder row inside the table triggers upload to target folder', function() { var ev; ev = dropOn(fileList.findFileEl('somedir').find('td:eq(2)'), uploadData); - expect(ev.result).not.toEqual(false); + expect(ev).not.toEqual(false); expect(uploadData.targetDir).toEqual('/subdir/somedir'); }); it('drop on a breadcrumb inside the table triggers upload to target folder', function() { @@ -2705,7 +2697,7 @@ describe('OCA.Files.FileList tests', function() { fileList.changeDirectory('a/b/c/d'); ev = dropOn(fileList.$el.find('.crumb:eq(2)'), uploadData); - expect(ev.result).not.toEqual(false); + expect(ev).not.toEqual(false); expect(uploadData.targetDir).toEqual('/a/b'); }); it('renders upload indicator element for folders only', function() { @@ -2729,8 +2721,7 @@ describe('OCA.Files.FileList tests', function() { it('fetches folder info', function() { var fetchInfoStub = sinon.stub(fileList, 'addAndFetchFileInfo'); - var ev = new $.Event('fileuploadcreatedfolder', {}); - $uploader.triggerHandler(ev, '/subdir/newfolder'); + uploader.trigger('createdfolder', {}, '/subdir/newfolder'); expect(fetchInfoStub.calledOnce).toEqual(true); expect(fetchInfoStub.getCall(0).args[0]).toEqual('newfolder'); @@ -2753,19 +2744,16 @@ describe('OCA.Files.FileList tests', function() { function createUpload(name, dir) { - var data = { - files: [{ - name: name - }], - upload: { - getFileName: sinon.stub().returns(name), - getFullPath: sinon.stub().returns(dir) - }, - jqXHR: { - status: 200 + var jqXHR = { + status: 200 + }; + return { + getFileName: sinon.stub().returns(name), + getFullPath: sinon.stub().returns(dir), + data: { + jqXHR: jqXHR } - } - return data; + }; } /** @@ -2774,12 +2762,12 @@ describe('OCA.Files.FileList tests', function() { * @return event object including the result */ function addFile(data) { - var ev = new $.Event('fileuploaddone', {}); - // using triggerHandler instead of trigger so we can pass - // extra data + var ev = new $.Event('done', { + jqXHR: {status: 200} + }); var deferred = $.Deferred(); fetchInfoStub.returns(deferred.promise()); - $uploader.triggerHandler(ev, data || {}); + uploader.trigger('done', ev, data || {}); return deferred; } @@ -2794,7 +2782,7 @@ describe('OCA.Files.FileList tests', function() { var def1 = addFile(createUpload('upload.txt', '/subdir')); var def2 = addFile(createUpload('upload2.txt', '/subdir')); var def3 = addFile(createUpload('upload3.txt', '/another')); - $uploader.triggerHandler(new $.Event('fileuploadstop')); + uploader.trigger('stop', {}); expect(highlightStub.notCalled).toEqual(true); def1.resolve(); @@ -2810,7 +2798,7 @@ describe('OCA.Files.FileList tests', function() { var statStub = sinon.stub(fileList, 'updateStorageStatistics'); addFile(createUpload('upload.txt', '/subdir')); expect(statStub.notCalled).toEqual(true); - $uploader.triggerHandler(new $.Event('fileuploadstop')); + uploader.trigger('stop', {}); expect(statStub.calledOnce).toEqual(true); statStub.restore(); });