diff options
Diffstat (limited to 'apps/files/tests/js')
-rw-r--r-- | apps/files/tests/js/favoritesfilelistspec.js | 3 | ||||
-rw-r--r-- | apps/files/tests/js/fileUploadSpec.js | 2 | ||||
-rw-r--r-- | apps/files/tests/js/fileactionsSpec.js | 2 | ||||
-rw-r--r-- | apps/files/tests/js/fileactionsmenuSpec.js | 7 | ||||
-rw-r--r-- | apps/files/tests/js/filelistSpec.js | 843 | ||||
-rw-r--r-- | apps/files/tests/js/filesSpec.js | 6 |
6 files changed, 394 insertions, 469 deletions
diff --git a/apps/files/tests/js/favoritesfilelistspec.js b/apps/files/tests/js/favoritesfilelistspec.js index 608ddaca18b..1c833d334e2 100644 --- a/apps/files/tests/js/favoritesfilelistspec.js +++ b/apps/files/tests/js/favoritesfilelistspec.js @@ -100,8 +100,7 @@ describe('OCA.Files.FavoritesFileList tests', function() { expect($tr.attr('data-mtime')).toEqual('11111000'); expect($tr.find('a.name').attr('href')).toEqual( OC.webroot + - '/index.php/apps/files/ajax/download.php' + - '?dir=%2Fsomedir&files=test.txt' + '/remote.php/webdav/somedir/test.txt' ); expect($tr.find('.nametext').text().trim()).toEqual('test.txt'); }); diff --git a/apps/files/tests/js/fileUploadSpec.js b/apps/files/tests/js/fileUploadSpec.js index a49a5d4e2e0..8a0d6b01952 100644 --- a/apps/files/tests/js/fileUploadSpec.js +++ b/apps/files/tests/js/fileUploadSpec.js @@ -19,6 +19,8 @@ * */ +/* global FileList */ + describe('OC.Upload tests', function() { var $dummyUploader; var testFile; diff --git a/apps/files/tests/js/fileactionsSpec.js b/apps/files/tests/js/fileactionsSpec.js index d29164c5136..a905a4d969d 100644 --- a/apps/files/tests/js/fileactionsSpec.js +++ b/apps/files/tests/js/fileactionsSpec.js @@ -584,7 +584,7 @@ describe('OCA.Files.FileActions tests', function() { expect(busyStub.calledWith('testName.txt', true)).toEqual(true); expect(handleDownloadStub.calledOnce).toEqual(true); expect(handleDownloadStub.getCall(0).args[0]).toEqual( - OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=testName.txt' + OC.webroot + '/remote.php/webdav/subdir/testName.txt' ); busyStub.reset(); handleDownloadStub.yield(); diff --git a/apps/files/tests/js/fileactionsmenuSpec.js b/apps/files/tests/js/fileactionsmenuSpec.js index dee542458b6..747a746a602 100644 --- a/apps/files/tests/js/fileactionsmenuSpec.js +++ b/apps/files/tests/js/fileactionsmenuSpec.js @@ -237,8 +237,8 @@ describe('OCA.Files.FileActionsMenu tests', function() { expect(redirectStub.calledOnce).toEqual(true); expect(redirectStub.getCall(0).args[0]).toContain( OC.webroot + - '/index.php/apps/files/ajax/download.php' + - '?dir=%2Fsubdir&files=testName.txt'); + '/remote.php/webdav/subdir/testName.txt' + ); redirectStub.restore(); }); it('takes the file\'s path into account when clicking download', function() { @@ -269,8 +269,7 @@ describe('OCA.Files.FileActionsMenu tests', function() { expect(redirectStub.calledOnce).toEqual(true); expect(redirectStub.getCall(0).args[0]).toContain( - OC.webroot + '/index.php/apps/files/ajax/download.php' + - '?dir=%2Fanotherpath%2Fthere&files=testName.txt' + OC.webroot + '/remote.php/webdav/anotherpath/there/testName.txt' ); redirectStub.restore(); }); diff --git a/apps/files/tests/js/filelistSpec.js b/apps/files/tests/js/filelistSpec.js index 05e6fcc6122..9f7ad50bc60 100644 --- a/apps/files/tests/js/filelistSpec.js +++ b/apps/files/tests/js/filelistSpec.js @@ -20,8 +20,11 @@ */ describe('OCA.Files.FileList tests', function() { - var testFiles, alertStub, notificationStub, fileList, pageSizeStub; + var FileInfo = OC.Files.FileInfo; + var testFiles, testRoot, notificationStub, fileList, pageSizeStub; var bcResizeStub; + var filesClient; + var redirectStub; /** * Generate test file data @@ -38,21 +41,29 @@ describe('OCA.Files.FileList tests', function() { name += '0'; } name += i + '.txt'; - files.push({ + files.push(new FileInfo({ id: i, type: 'file', name: name, mimetype: 'text/plain', size: i * 2, etag: 'abc' - }); + })); } return files; } beforeEach(function() { - alertStub = sinon.stub(OC.dialogs, 'alert'); - notificationStub = sinon.stub(OC.Notification, 'show'); + filesClient = new OC.Files.Client({ + host: 'localhost', + port: 80, + // FIXME: uncomment after fixing the test OC.webroot + //root: OC.webroot + '/remote.php/webdav', + root: '/remote.php/webdav', + useHTTPS: false + }); + redirectStub = sinon.stub(OC, 'redirect'); + notificationStub = sinon.stub(OC.Notification, 'showTemporary'); // prevent resize algo to mess up breadcrumb order while // testing bcResizeStub = sinon.stub(OCA.Files.BreadCrumb.prototype, '_resize'); @@ -93,7 +104,17 @@ describe('OCA.Files.FileList tests', function() { '</div>' ); - testFiles = [{ + testRoot = new FileInfo({ + // root entry + id: 99, + type: 'dir', + name: '/subdir', + mimetype: 'httpd/unix-directory', + size: 1200000, + etag: 'a0b0c0d0', + permissions: OC.PERMISSION_ALL + }); + testFiles = [new FileInfo({ id: 1, type: 'file', name: 'One.txt', @@ -102,7 +123,7 @@ describe('OCA.Files.FileList tests', function() { size: 12, etag: 'abc', permissions: OC.PERMISSION_ALL - }, { + }), new FileInfo({ id: 2, type: 'file', name: 'Two.jpg', @@ -111,7 +132,7 @@ describe('OCA.Files.FileList tests', function() { size: 12049, etag: 'def', permissions: OC.PERMISSION_ALL - }, { + }), new FileInfo({ id: 3, type: 'file', name: 'Three.pdf', @@ -120,7 +141,7 @@ describe('OCA.Files.FileList tests', function() { size: 58009, etag: '123', permissions: OC.PERMISSION_ALL - }, { + }), new FileInfo({ id: 4, type: 'dir', name: 'somedir', @@ -129,9 +150,11 @@ describe('OCA.Files.FileList tests', function() { size: 250, etag: '456', permissions: OC.PERMISSION_ALL - }]; + })]; pageSizeStub = sinon.stub(OCA.Files.FileList.prototype, 'pageSize').returns(20); - fileList = new OCA.Files.FileList($('#app-content-files')); + fileList = new OCA.Files.FileList($('#app-content-files'), { + filesClient: filesClient + }); }); afterEach(function() { testFiles = undefined; @@ -141,9 +164,9 @@ describe('OCA.Files.FileList tests', function() { fileList = undefined; notificationStub.restore(); - alertStub.restore(); bcResizeStub.restore(); pageSizeStub.restore(); + redirectStub.restore(); }); describe('Getters', function() { it('Returns the current directory', function() { @@ -166,15 +189,14 @@ describe('OCA.Files.FileList tests', function() { clock.restore(); }); it('generates file element with correct attributes when calling add() with file data', function() { - var fileData = { + var fileData = new FileInfo({ id: 18, - type: 'file', name: 'testName.txt', mimetype: 'text/plain', - size: '1234', + size: 1234, etag: 'a01234c', - mtime: '123456' - }; + mtime: 123456 + }); var $tr = fileList.add(fileData); expect($tr).toBeDefined(); @@ -188,7 +210,7 @@ describe('OCA.Files.FileList tests', function() { expect($tr.attr('data-mime')).toEqual('text/plain'); expect($tr.attr('data-mtime')).toEqual('123456'); expect($tr.find('a.name').attr('href')) - .toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=testName.txt'); + .toEqual(OC.webroot + '/remote.php/webdav/subdir/testName.txt'); expect($tr.find('.nametext').text().trim()).toEqual('testName.txt'); expect($tr.find('.filesize').text()).toEqual('1 kB'); @@ -196,15 +218,14 @@ describe('OCA.Files.FileList tests', function() { expect(fileList.findFileEl('testName.txt')[0]).toEqual($tr[0]); }); it('generates dir element with correct attributes when calling add() with dir data', function() { - var fileData = { + var fileData = new FileInfo({ id: 19, - type: 'dir', name: 'testFolder', mimetype: 'httpd/unix-directory', - size: '1234', + size: 1234, etag: 'a01234c', - mtime: '123456' - }; + mtime: 123456 + }); var $tr = fileList.add(fileData); expect($tr).toBeDefined(); @@ -297,7 +318,6 @@ describe('OCA.Files.FileList tests', function() { expect($tr.index()).toEqual(4); }); it('inserts files in a sorted manner when insert option is enabled', function() { - var $tr; for (var i = 0; i < testFiles.length; i++) { fileList.add(testFiles[i]); } @@ -423,28 +443,31 @@ describe('OCA.Files.FileList tests', function() { }); }); describe('Deleting files', function() { + var deferredDelete; + var deleteStub; + + beforeEach(function() { + deferredDelete = $.Deferred(); + deleteStub = sinon.stub(filesClient, 'remove').returns(deferredDelete.promise()); + }); + afterEach(function() { + deleteStub.restore(); + }); + function doDelete() { - var request, query; // note: normally called from FileActions fileList.do_delete(['One.txt', 'Two.jpg']); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/delete.php'); - - query = fakeServer.requests[0].requestBody; - expect(OC.parseQueryString(query)).toEqual({'dir': '/subdir', files: '["One.txt","Two.jpg"]'}); + expect(deleteStub.calledTwice).toEqual(true); + expect(deleteStub.getCall(0).args[0]).toEqual('/subdir/One.txt'); + expect(deleteStub.getCall(1).args[0]).toEqual('/subdir/Two.jpg'); } it('calls delete.php, removes the deleted entries and updates summary', function() { var $summary; fileList.setFiles(testFiles); doDelete(); - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({status: 'success'}) - ); + deferredDelete.resolve(200); expect(fileList.findFileEl('One.txt').length).toEqual(0); expect(fileList.findFileEl('Two.jpg').length).toEqual(0); @@ -482,11 +505,7 @@ describe('OCA.Files.FileList tests', function() { fileList.setFiles([testFiles[0], testFiles[1]]); doDelete(); - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({status: 'success'}) - ); + deferredDelete.resolve(200); expect(fileList.$fileList.find('tr').length).toEqual(0); @@ -501,21 +520,41 @@ describe('OCA.Files.FileList tests', function() { fileList.setFiles(testFiles); doDelete(); - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({status: 'error', data: {message: 'WOOT'}}) - ); + deferredDelete.reject(403); // files are still in the list expect(fileList.findFileEl('One.txt').length).toEqual(1); expect(fileList.findFileEl('Two.jpg').length).toEqual(1); expect(fileList.$fileList.find('tr').length).toEqual(4); - expect(notificationStub.calledOnce).toEqual(true); + expect(notificationStub.calledTwice).toEqual(true); + }); + it('remove file from list if delete call returned 404 not found', function() { + fileList.setFiles(testFiles); + doDelete(); + + deferredDelete.reject(404); + + // files are still in the list + expect(fileList.findFileEl('One.txt').length).toEqual(0); + expect(fileList.findFileEl('Two.jpg').length).toEqual(0); + expect(fileList.$fileList.find('tr').length).toEqual(2); + + expect(notificationStub.notCalled).toEqual(true); }); }); describe('Renaming files', function() { + var deferredRename; + var renameStub; + + beforeEach(function() { + deferredRename = $.Deferred(); + renameStub = sinon.stub(filesClient, 'move').returns(deferredRename.promise()); + }); + afterEach(function() { + renameStub.restore(); + }); + function doCancelRename() { var $input; for (var i = 0; i < testFiles.length; i++) { @@ -530,10 +569,10 @@ describe('OCA.Files.FileList tests', function() { // trigger submit because triggering blur doesn't work in all browsers $input.closest('form').trigger('submit'); - expect(fakeServer.requests.length).toEqual(0); + expect(renameStub.notCalled).toEqual(true); } function doRename() { - var $input, request; + var $input; for (var i = 0; i < testFiles.length; i++) { var file = testFiles[i]; @@ -548,83 +587,61 @@ describe('OCA.Files.FileList tests', function() { // trigger submit because triggering blur doesn't work in all browsers $input.closest('form').trigger('submit'); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url.substr(0, request.url.indexOf('?'))).toEqual(OC.webroot + '/index.php/apps/files/ajax/rename.php'); - expect(OC.parseQueryString(request.url)).toEqual({'dir': '/some/subdir', newname: 'Tu_after_three.txt', file: 'One.txt'}); + expect(renameStub.calledOnce).toEqual(true); + expect(renameStub.getCall(0).args[0]).toEqual('/some/subdir/One.txt'); + expect(renameStub.getCall(0).args[1]).toEqual('/some/subdir/Tu_after_three.txt'); } it('Inserts renamed file entry at correct position if rename ajax call suceeded', function() { doRename(); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'success', - data: { - name: 'Tu_after_three.txt', - type: 'file' - } - })); + deferredRename.resolve(201); // element stays renamed expect(fileList.findFileEl('One.txt').length).toEqual(0); expect(fileList.findFileEl('Tu_after_three.txt').length).toEqual(1); - expect(fileList.findFileEl('Tu_after_three.txt').index()).toEqual(2); // after Two.txt + expect(fileList.findFileEl('Tu_after_three.txt').index()).toEqual(2); // after Two.jpg - expect(alertStub.notCalled).toEqual(true); + expect(notificationStub.notCalled).toEqual(true); }); it('Reverts file entry if rename ajax call failed', function() { doRename(); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'error', - data: { - message: 'Something went wrong' - } - })); + deferredRename.reject(403); // element was reverted expect(fileList.findFileEl('One.txt').length).toEqual(1); expect(fileList.findFileEl('One.txt').index()).toEqual(1); // after somedir expect(fileList.findFileEl('Tu_after_three.txt').length).toEqual(0); - expect(alertStub.calledOnce).toEqual(true); + expect(notificationStub.calledOnce).toEqual(true); }); it('Correctly updates file link after rename', function() { var $tr; doRename(); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'success', - data: { - name: 'Tu_after_three.txt' - } - })); + deferredRename.resolve(201); $tr = fileList.findFileEl('Tu_after_three.txt'); - expect($tr.find('a.name').attr('href')).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=Tu_after_three.txt'); + expect($tr.find('a.name').attr('href')) + .toEqual(OC.webroot + '/remote.php/webdav/some/subdir/Tu_after_three.txt'); }); it('Triggers "fileActionsReady" event after rename', function() { var handler = sinon.stub(); fileList.$fileList.on('fileActionsReady', handler); doRename(); expect(handler.notCalled).toEqual(true); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'success', - data: { - name: 'Tu_after_three.txt' - } - })); + + deferredRename.resolve(201); + expect(handler.calledOnce).toEqual(true); expect(fileList.$fileList.find('.test').length).toEqual(0); }); it('Leaves the summary alone when reinserting renamed element', function() { var $summary = $('#filestable .summary'); doRename(); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'success', - data: { - name: 'Tu_after_three.txt' - } - })); + + deferredRename.resolve(201); + expect($summary.find('.info').text()).toEqual('1 folder and 3 files'); }); it('Leaves the summary alone when cancel renaming', function() { @@ -668,7 +685,7 @@ describe('OCA.Files.FileList tests', function() { // trigger submit does not send server request $input.closest('form').trigger('submit'); - expect(fakeServer.requests.length).toEqual(0); + expect(renameStub.notCalled).toEqual(true); // simulate escape key $input.trigger(new $.Event('keyup', {keyCode: 27})); @@ -694,12 +711,7 @@ describe('OCA.Files.FileList tests', function() { expect(OC.TestUtil.getImageUrl(fileList.findFileEl('Tu_after_three.txt').find('.thumbnail'))) .toEqual(OC.imagePath('core', 'loading.gif')); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'error', - data: { - message: 'Something went wrong' - } - })); + deferredRename.reject(409); expect(fileList.findFileEl('One.txt').length).toEqual(1); expect(OC.TestUtil.getImageUrl(fileList.findFileEl('One.txt').find('.thumbnail'))) @@ -707,25 +719,27 @@ describe('OCA.Files.FileList tests', function() { }); }); describe('Moving files', function() { + var deferredMove; + var moveStub; + beforeEach(function() { + deferredMove = $.Deferred(); + moveStub = sinon.stub(filesClient, 'move').returns(deferredMove.promise()); + fileList.setFiles(testFiles); }); + afterEach(function() { + moveStub.restore(); + }); + it('Moves single file to target folder', function() { - var request; fileList.move('One.txt', '/somedir'); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/move.php'); - expect(OC.parseQueryString(request.requestBody)).toEqual({dir: '/subdir', file: 'One.txt', target: '/somedir'}); + expect(moveStub.calledOnce).toEqual(true); + expect(moveStub.getCall(0).args[0]).toEqual('/subdir/One.txt'); + expect(moveStub.getCall(0).args[1]).toEqual('/somedir/One.txt'); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'success', - data: { - name: 'One.txt', - type: 'file' - } - })); + deferredMove.resolve(201); expect(fileList.findFileEl('One.txt').length).toEqual(0); @@ -736,39 +750,28 @@ describe('OCA.Files.FileList tests', function() { expect(notificationStub.notCalled).toEqual(true); }); it('Moves list of files to target folder', function() { - var request; + var deferredMove1 = $.Deferred(); + var deferredMove2 = $.Deferred(); + moveStub.onCall(0).returns(deferredMove1.promise()); + moveStub.onCall(1).returns(deferredMove2.promise()); + fileList.move(['One.txt', 'Two.jpg'], '/somedir'); - expect(fakeServer.requests.length).toEqual(2); - request = fakeServer.requests[0]; - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/move.php'); - expect(OC.parseQueryString(request.requestBody)).toEqual({dir: '/subdir', file: 'One.txt', target: '/somedir'}); + expect(moveStub.calledTwice).toEqual(true); + expect(moveStub.getCall(0).args[0]).toEqual('/subdir/One.txt'); + expect(moveStub.getCall(0).args[1]).toEqual('/somedir/One.txt'); + expect(moveStub.getCall(1).args[0]).toEqual('/subdir/Two.jpg'); + expect(moveStub.getCall(1).args[1]).toEqual('/somedir/Two.jpg'); - request = fakeServer.requests[1]; - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/move.php'); - expect(OC.parseQueryString(request.requestBody)).toEqual({dir: '/subdir', file: 'Two.jpg', target: '/somedir'}); - - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'success', - data: { - name: 'One.txt', - type: 'file' - } - })); + deferredMove1.resolve(201); expect(fileList.findFileEl('One.txt').length).toEqual(0); - // folder size has increased + // folder size has increased during move expect(fileList.findFileEl('somedir').data('size')).toEqual(262); expect(fileList.findFileEl('somedir').find('.filesize').text()).toEqual('262 B'); - fakeServer.requests[1].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'success', - data: { - name: 'Two.jpg', - type: 'file' - } - })); + deferredMove2.resolve(201); expect(fileList.findFileEl('Two.jpg').length).toEqual(0); @@ -779,47 +782,31 @@ describe('OCA.Files.FileList tests', function() { expect(notificationStub.notCalled).toEqual(true); }); it('Shows notification if a file could not be moved', function() { - var request; fileList.move('One.txt', '/somedir'); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/move.php'); - expect(OC.parseQueryString(request.requestBody)).toEqual({dir: '/subdir', file: 'One.txt', target: '/somedir'}); + expect(moveStub.calledOnce).toEqual(true); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'error', - data: { - message: 'Error while moving file' - } - })); + deferredMove.reject(409); expect(fileList.findFileEl('One.txt').length).toEqual(1); expect(notificationStub.calledOnce).toEqual(true); - expect(notificationStub.getCall(0).args[0]).toEqual('Error while moving file'); + expect(notificationStub.getCall(0).args[0]).toEqual('Could not move "One.txt"'); }); it('Restores thumbnail if a file could not be moved', function() { - var request; fileList.move('One.txt', '/somedir'); expect(OC.TestUtil.getImageUrl(fileList.findFileEl('One.txt').find('.thumbnail'))) .toEqual(OC.imagePath('core', 'loading.gif')); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; + expect(moveStub.calledOnce).toEqual(true); - fakeServer.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify({ - status: 'error', - data: { - message: 'Error while moving file' - } - })); + deferredMove.reject(409); expect(fileList.findFileEl('One.txt').length).toEqual(1); expect(notificationStub.calledOnce).toEqual(true); - expect(notificationStub.getCall(0).args[0]).toEqual('Error while moving file'); + expect(notificationStub.getCall(0).args[0]).toEqual('Could not move "One.txt"'); expect(OC.TestUtil.getImageUrl(fileList.findFileEl('One.txt').find('.thumbnail'))) .toEqual(OC.imagePath('core', 'filetypes/text.svg')); @@ -878,7 +865,7 @@ describe('OCA.Files.FileList tests', function() { name: 'testFile.txt', directory: '/current dir' }; - var $tr = fileList.add(fileData); + fileList.add(fileData); expect(fileList.findFileEl('testFile.txt').length).toEqual(1); }); it('triggers "fileActionsReady" event after update', function() { @@ -1143,69 +1130,85 @@ describe('OCA.Files.FileList tests', function() { afterEach(function() { previewLoadStub.restore(); }); - it('renders default icon for file when none provided and no preview is available', function() { + it('renders default file icon when none provided and no mime type is set', function() { var fileData = { - type: 'file', name: 'testFile.txt' }; var $tr = fileList.add(fileData); var $imgDiv = $tr.find('td.filename .thumbnail'); expect(OC.TestUtil.getImageUrl($imgDiv)).toEqual(OC.webroot + '/core/img/filetypes/file.svg'); - expect(previewLoadStub.notCalled).toEqual(true); + // tries to load preview + expect(previewLoadStub.calledOnce).toEqual(true); }); - it('renders default icon for dir when none provided and no preview is available', function() { + it('renders default icon for folder when none provided', function() { var fileData = { - type: 'dir', - name: 'test dir' + name: 'test dir', + mimetype: 'httpd/unix-directory' }; + var $tr = fileList.add(fileData); var $imgDiv = $tr.find('td.filename .thumbnail'); expect(OC.TestUtil.getImageUrl($imgDiv)).toEqual(OC.webroot + '/core/img/filetypes/folder.svg'); + // no preview since it's a directory expect(previewLoadStub.notCalled).toEqual(true); }); it('renders provided icon for file when provided', function() { - var fileData = { + var fileData = new FileInfo({ type: 'file', name: 'test file', icon: OC.webroot + '/core/img/filetypes/application-pdf.svg', mimetype: 'application/pdf' - }; + }); var $tr = fileList.add(fileData); var $imgDiv = $tr.find('td.filename .thumbnail'); expect(OC.TestUtil.getImageUrl($imgDiv)).toEqual(OC.webroot + '/core/img/filetypes/application-pdf.svg'); + // try loading preview + expect(previewLoadStub.calledOnce).toEqual(true); + }); + it('renders provided icon for file when provided', function() { + var fileData = new FileInfo({ + name: 'somefile.pdf', + icon: OC.webroot + '/core/img/filetypes/application-pdf.svg' + }); + + var $tr = fileList.add(fileData); + var $imgDiv = $tr.find('td.filename .thumbnail'); + expect(OC.TestUtil.getImageUrl($imgDiv)).toEqual(OC.webroot + '/core/img/filetypes/application-pdf.svg'); + // try loading preview + expect(previewLoadStub.calledOnce).toEqual(true); + }); + it('renders provided icon for folder when provided', function() { + var fileData = new FileInfo({ + name: 'some folder', + mimetype: 'httpd/unix-directory', + icon: OC.webroot + '/core/img/filetypes/folder-alt.svg' + }); + + var $tr = fileList.add(fileData); + var $imgDiv = $tr.find('td.filename .thumbnail'); + expect(OC.TestUtil.getImageUrl($imgDiv)).toEqual(OC.webroot + '/core/img/filetypes/folder-alt.svg'); + // do not load preview for folders expect(previewLoadStub.notCalled).toEqual(true); }); - it('renders preview when no icon was provided and preview is available', function() { + it('renders preview when no icon was provided', function() { var fileData = { type: 'file', - name: 'test file', - isPreviewAvailable: true + name: 'test file' }; var $tr = fileList.add(fileData); var $td = $tr.find('td.filename'); - expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))).toEqual(OC.webroot + '/core/img/filetypes/file.svg'); + expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))) + .toEqual(OC.webroot + '/core/img/filetypes/file.svg'); expect(previewLoadStub.calledOnce).toEqual(true); // third argument is callback previewLoadStub.getCall(0).args[0].callback(OC.webroot + '/somepath.png'); expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))).toEqual(OC.webroot + '/somepath.png'); }); - it('renders default file type icon when no icon was provided and no preview is available', function() { - var fileData = { - type: 'file', - name: 'test file', - isPreviewAvailable: false - }; - var $tr = fileList.add(fileData); - var $imgDiv = $tr.find('td.filename .thumbnail'); - expect(OC.TestUtil.getImageUrl($imgDiv)).toEqual(OC.webroot + '/core/img/filetypes/file.svg'); - expect(previewLoadStub.notCalled).toEqual(true); - }); it('does not render preview for directories', function() { var fileData = { type: 'dir', mimetype: 'httpd/unix-directory', - name: 'test dir', - isPreviewAvailable: true + name: 'test dir' }; var $tr = fileList.add(fileData); var $td = $tr.find('td.filename'); @@ -1217,7 +1220,6 @@ describe('OCA.Files.FileList tests', function() { type: 'dir', mimetype: 'httpd/unix-directory', name: 'test dir', - isPreviewAvailable: true, mountType: 'external-root' }; var $tr = fileList.add(fileData); @@ -1230,7 +1232,6 @@ describe('OCA.Files.FileList tests', function() { type: 'dir', mimetype: 'httpd/unix-directory', name: 'test dir', - isPreviewAvailable: true, mountType: 'external' }; var $tr = fileList.add(fileData); @@ -1278,75 +1279,47 @@ describe('OCA.Files.FileList tests', function() { }); }); describe('loading file list', function() { + var deferredList; + var getFolderContentsStub; + beforeEach(function() { - var data = { - status: 'success', - data: { - files: testFiles, - permissions: 31 - } - }; - fakeServer.respondWith(/\/index\.php\/apps\/files\/ajax\/list.php\?dir=%2F(subdir|anothersubdir)/, [ - 200, { - "Content-Type": "application/json" - }, - JSON.stringify(data) - ]); + deferredList = $.Deferred(); + getFolderContentsStub = sinon.stub(filesClient, 'getFolderContents').returns(deferredList.promise()); + }); + afterEach(function() { + getFolderContentsStub.restore(); }); it('fetches file list from server and renders it when reload() is called', function() { fileList.reload(); - expect(fakeServer.requests.length).toEqual(1); - var url = fakeServer.requests[0].url; - var query = url.substr(url.indexOf('?') + 1); - expect(OC.parseQueryString(query)).toEqual({'dir': '/subdir', sort: 'name', sortdirection: 'asc'}); - fakeServer.respond(); + expect(getFolderContentsStub.calledOnce).toEqual(true); + expect(getFolderContentsStub.calledWith('/subdir')).toEqual(true); + deferredList.resolve(200, [testRoot].concat(testFiles)); expect($('#fileList tr').length).toEqual(4); expect(fileList.findFileEl('One.txt').length).toEqual(1); }); it('switches dir and fetches file list when calling changeDirectory()', function() { fileList.changeDirectory('/anothersubdir'); expect(fileList.getCurrentDirectory()).toEqual('/anothersubdir'); - expect(fakeServer.requests.length).toEqual(1); - var url = fakeServer.requests[0].url; - var query = url.substr(url.indexOf('?') + 1); - expect(OC.parseQueryString(query)).toEqual({'dir': '/anothersubdir', sort: 'name', sortdirection: 'asc'}); - fakeServer.respond(); + expect(getFolderContentsStub.calledOnce).toEqual(true); + expect(getFolderContentsStub.calledWith('/anothersubdir')).toEqual(true); }); it('converts backslashes to slashes when calling changeDirectory()', function() { fileList.changeDirectory('/another\\subdir'); expect(fileList.getCurrentDirectory()).toEqual('/another/subdir'); }); it('switches to root dir when current directory does not exist', function() { - fakeServer.respondWith(/\/index\.php\/apps\/files\/ajax\/list.php\?dir=%2funexist/, [ - 404, { - "Content-Type": "application/json" - }, - '' - ]); fileList.changeDirectory('/unexist'); - fakeServer.respond(); + deferredList.reject(404); expect(fileList.getCurrentDirectory()).toEqual('/'); }); it('switches to root dir when current directory is forbidden', function() { - fakeServer.respondWith(/\/index\.php\/apps\/files\/ajax\/list.php\?dir=%2funexist/, [ - 403, { - "Content-Type": "application/json" - }, - '' - ]); fileList.changeDirectory('/unexist'); - fakeServer.respond(); + deferredList.reject(403); expect(fileList.getCurrentDirectory()).toEqual('/'); }); it('switches to root dir when current directory is unavailable', function() { - fakeServer.respondWith(/\/index\.php\/apps\/files\/ajax\/list.php\?dir=%2funexist/, [ - 500, { - "Content-Type": "application/json" - }, - '' - ]); fileList.changeDirectory('/unexist'); - fakeServer.respond(); + deferredList.reject(500); expect(fileList.getCurrentDirectory()).toEqual('/'); }); it('shows mask before loading file list then hides it at the end', function() { @@ -1355,7 +1328,7 @@ describe('OCA.Files.FileList tests', function() { fileList.changeDirectory('/anothersubdir'); expect(showMaskStub.calledOnce).toEqual(true); expect(hideMaskStub.calledOnce).toEqual(false); - fakeServer.respond(); + deferredList.resolve(200, [testRoot].concat(testFiles)); expect(showMaskStub.calledOnce).toEqual(true); expect(hideMaskStub.calledOnce).toEqual(true); showMaskStub.restore(); @@ -1365,6 +1338,7 @@ describe('OCA.Files.FileList tests', function() { var handler = sinon.stub(); $('#app-content-files').on('changeDirectory', handler); fileList.changeDirectory('/somedir'); + deferredList.resolve(200, [testRoot].concat(testFiles)); expect(handler.calledOnce).toEqual(true); expect(handler.getCall(0).args[0].dir).toEqual('/somedir'); }); @@ -1375,31 +1349,27 @@ describe('OCA.Files.FileList tests', function() { it('refreshes breadcrumb after update', function() { var setDirSpy = sinon.spy(fileList.breadcrumb, 'setDirectory'); fileList.changeDirectory('/anothersubdir'); - fakeServer.respond(); + deferredList.resolve(200, [testRoot].concat(testFiles)); expect(fileList.breadcrumb.setDirectory.calledOnce).toEqual(true); expect(fileList.breadcrumb.setDirectory.calledWith('/anothersubdir')).toEqual(true); setDirSpy.restore(); + getFolderContentsStub.restore(); }); }); describe('breadcrumb events', function() { + var deferredList; + var getFolderContentsStub; + beforeEach(function() { - var data = { - status: 'success', - data: { - files: testFiles, - permissions: 31 - } - }; - fakeServer.respondWith(/\/index\.php\/apps\/files\/ajax\/list.php\?dir=%2Fsubdir/, [ - 200, { - "Content-Type": "application/json" - }, - JSON.stringify(data) - ]); + deferredList = $.Deferred(); + getFolderContentsStub = sinon.stub(filesClient, 'getFolderContents').returns(deferredList.promise()); + }); + afterEach(function() { + getFolderContentsStub.restore(); }); it('clicking on root breadcrumb changes directory to root', function() { fileList.changeDirectory('/subdir/two/three with space/four/five'); - fakeServer.respond(); + deferredList.resolve(200, [testRoot].concat(testFiles)); var changeDirStub = sinon.stub(fileList, 'changeDirectory'); fileList.breadcrumb.$el.find('.crumb:eq(0)').trigger({type: 'click', which: 1}); @@ -1409,7 +1379,7 @@ describe('OCA.Files.FileList tests', function() { }); it('clicking on breadcrumb changes directory', function() { fileList.changeDirectory('/subdir/two/three with space/four/five'); - fakeServer.respond(); + deferredList.resolve(200, [testRoot].concat(testFiles)); var changeDirStub = sinon.stub(fileList, 'changeDirectory'); fileList.breadcrumb.$el.find('.crumb:eq(3)').trigger({type: 'click', which: 1}); @@ -1418,9 +1388,10 @@ describe('OCA.Files.FileList tests', function() { changeDirStub.restore(); }); it('dropping files on breadcrumb calls move operation', function() { - var request, query, testDir = '/subdir/two/three with space/four/five'; + var testDir = '/subdir/two/three with space/four/five'; + var moveStub = sinon.stub(filesClient, 'move').returns($.Deferred().promise()); fileList.changeDirectory(testDir); - fakeServer.respond(); + deferredList.resolve(200, [testRoot].concat(testFiles)); var $crumb = fileList.breadcrumb.$el.find('.crumb:eq(3)'); // no idea what this is but is required by the handler var ui = { @@ -1436,33 +1407,18 @@ describe('OCA.Files.FileList tests', function() { // simulate drop event fileList._onDropOnBreadCrumb(new $.Event('drop', {target: $crumb}), ui); - // will trigger two calls to move.php (first one was previous list.php) - expect(fakeServer.requests.length).toEqual(3); - - request = fakeServer.requests[1]; - expect(request.method).toEqual('POST'); - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/move.php'); - query = OC.parseQueryString(request.requestBody); - expect(query).toEqual({ - target: '/subdir/two/three with space', - dir: testDir, - file: 'One.txt' - }); - - request = fakeServer.requests[2]; - expect(request.method).toEqual('POST'); - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/move.php'); - query = OC.parseQueryString(request.requestBody); - expect(query).toEqual({ - target: '/subdir/two/three with space', - dir: testDir, - file: 'Two.jpg' - }); + expect(moveStub.callCount).toEqual(2); + expect(moveStub.getCall(0).args[0]).toEqual(testDir + '/One.txt'); + expect(moveStub.getCall(0).args[1]).toEqual('/subdir/two/three with space/One.txt'); + expect(moveStub.getCall(1).args[0]).toEqual(testDir + '/Two.jpg'); + expect(moveStub.getCall(1).args[1]).toEqual('/subdir/two/three with space/Two.jpg'); + moveStub.restore(); }); it('dropping files on same dir breadcrumb does nothing', function() { var testDir = '/subdir/two/three with space/four/five'; + var moveStub = sinon.stub(filesClient, 'move').returns($.Deferred().promise()); fileList.changeDirectory(testDir); - fakeServer.respond(); + deferredList.resolve(200, [testRoot].concat(testFiles)); var $crumb = fileList.breadcrumb.$el.find('.crumb:last'); // no idea what this is but is required by the handler var ui = { @@ -1479,21 +1435,26 @@ describe('OCA.Files.FileList tests', function() { fileList._onDropOnBreadCrumb(new $.Event('drop', {target: $crumb}), ui); // no extra server request - expect(fakeServer.requests.length).toEqual(1); + expect(moveStub.notCalled).toEqual(true); }); }); describe('Download Url', function() { it('returns correct download URL for single files', function() { - expect(fileList.getDownloadUrl('some file.txt')).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=some%20file.txt'); - expect(fileList.getDownloadUrl('some file.txt', '/anotherpath/abc')).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fanotherpath%2Fabc&files=some%20file.txt'); + expect(fileList.getDownloadUrl('some file.txt')) + .toEqual(OC.webroot + '/remote.php/webdav/subdir/some%20file.txt'); + expect(fileList.getDownloadUrl('some file.txt', '/anotherpath/abc')) + .toEqual(OC.webroot + '/remote.php/webdav/anotherpath/abc/some%20file.txt'); $('#dir').val('/'); - expect(fileList.getDownloadUrl('some file.txt')).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2F&files=some%20file.txt'); + expect(fileList.getDownloadUrl('some file.txt')) + .toEqual(OC.webroot + '/remote.php/webdav/some%20file.txt'); }); it('returns correct download URL for multiple files', function() { - expect(fileList.getDownloadUrl(['a b c.txt', 'd e f.txt'])).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=%5B%22a%20b%20c.txt%22%2C%22d%20e%20f.txt%22%5D'); + expect(fileList.getDownloadUrl(['a b c.txt', 'd e f.txt'])) + .toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=%5B%22a%20b%20c.txt%22%2C%22d%20e%20f.txt%22%5D'); }); it('returns the correct ajax URL', function() { - expect(fileList.getAjaxUrl('test', {a:1, b:'x y'})).toEqual(OC.webroot + '/index.php/apps/files/ajax/test.php?a=1&b=x%20y'); + expect(fileList.getAjaxUrl('test', {a:1, b:'x y'})) + .toEqual(OC.webroot + '/index.php/apps/files/ajax/test.php?a=1&b=x%20y'); }); }); describe('File selection', function() { @@ -1672,24 +1633,17 @@ describe('OCA.Files.FileList tests', function() { }); it('Selection is cleared when switching dirs', function() { $('.select-all').click(); - var data = { - status: 'success', - data: { - files: testFiles, - permissions: 31 - } - }; - fakeServer.respondWith(/\/index\.php\/apps\/files\/ajax\/list.php/, [ - 200, { - "Content-Type": "application/json" - }, - JSON.stringify(data) - ] - ); + var deferredList = $.Deferred(); + var getFolderContentsStub = sinon.stub(filesClient, 'getFolderContents').returns(deferredList.promise()); + fileList.changeDirectory('/'); - fakeServer.respond(); + + deferredList.resolve(200, [testRoot].concat(testFiles)); + expect($('.select-all').prop('checked')).toEqual(false); expect(_.pluck(fileList.getSelectedFiles(), 'name')).toEqual([]); + + getFolderContentsStub.restore(); }); it('getSelectedFiles returns the selected files even when they are on the next page', function() { var selectedFiles; @@ -1796,6 +1750,12 @@ describe('OCA.Files.FileList tests', function() { etag: '456', permissions: OC.PERMISSION_ALL }); + expect(files[0].id).toEqual(1); + expect(files[0].name).toEqual('One.txt'); + expect(files[1].id).toEqual(3); + expect(files[1].name).toEqual('Three.pdf'); + expect(files[2].id).toEqual(4); + expect(files[2].name).toEqual('somedir'); }); it('Removing a file removes it from the selection', function() { fileList.remove('Three.pdf'); @@ -1824,7 +1784,6 @@ describe('OCA.Files.FileList tests', function() { }); describe('Download', function() { it('Opens download URL when clicking "Download"', function() { - var redirectStub = sinon.stub(OC, 'redirect'); $('.selectedActions .download').click(); expect(redirectStub.calledOnce).toEqual(true); expect(redirectStub.getCall(0).args[0]).toContain(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=%5B%22One.txt%22%2C%22Three.pdf%22%2C%22somedir%22%5D'); @@ -1833,54 +1792,53 @@ describe('OCA.Files.FileList tests', function() { it('Downloads root folder when all selected in root folder', function() { $('#dir').val('/'); $('.select-all').click(); - var redirectStub = sinon.stub(OC, 'redirect'); $('.selectedActions .download').click(); expect(redirectStub.calledOnce).toEqual(true); expect(redirectStub.getCall(0).args[0]).toContain(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2F&files='); - redirectStub.restore(); }); it('Downloads parent folder when all selected in subfolder', function() { $('.select-all').click(); - var redirectStub = sinon.stub(OC, 'redirect'); $('.selectedActions .download').click(); expect(redirectStub.calledOnce).toEqual(true); expect(redirectStub.getCall(0).args[0]).toContain(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2F&files=subdir'); - redirectStub.restore(); }); }); describe('Delete', function() { + var deleteStub, deferredDelete; + beforeEach(function() { + deferredDelete = $.Deferred(); + deleteStub = sinon.stub(filesClient, 'remove').returns(deferredDelete.promise()); + }); + afterEach(function() { + deleteStub.restore(); + }); it('Deletes selected files when "Delete" clicked', function() { - var request; $('.selectedActions .delete-selected').click(); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/delete.php'); - expect(OC.parseQueryString(request.requestBody)) - .toEqual({'dir': '/subdir', files: '["One.txt","Three.pdf","somedir"]'}); - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({status: 'success'}) - ); + + expect(deleteStub.callCount).toEqual(3); + expect(deleteStub.getCall(0).args[0]).toEqual('/subdir/One.txt'); + expect(deleteStub.getCall(1).args[0]).toEqual('/subdir/Three.pdf'); + expect(deleteStub.getCall(2).args[0]).toEqual('/subdir/somedir'); + + deferredDelete.resolve(204); + expect(fileList.findFileEl('One.txt').length).toEqual(0); expect(fileList.findFileEl('Three.pdf').length).toEqual(0); expect(fileList.findFileEl('somedir').length).toEqual(0); expect(fileList.findFileEl('Two.jpg').length).toEqual(1); }); it('Deletes all files when all selected when "Delete" clicked', function() { - var request; $('.select-all').click(); $('.selectedActions .delete-selected').click(); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual(OC.webroot + '/index.php/apps/files/ajax/delete.php'); - expect(OC.parseQueryString(request.requestBody)) - .toEqual({'dir': '/subdir', allfiles: 'true'}); - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({status: 'success'}) - ); + + expect(deleteStub.callCount).toEqual(4); + expect(deleteStub.getCall(0).args[0]).toEqual('/subdir/One.txt'); + expect(deleteStub.getCall(1).args[0]).toEqual('/subdir/Two.jpg'); + expect(deleteStub.getCall(2).args[0]).toEqual('/subdir/Three.pdf'); + expect(deleteStub.getCall(3).args[0]).toEqual('/subdir/somedir'); + + deferredDelete.resolve(204); + expect(fileList.isEmpty).toEqual(true); }); }); @@ -2118,30 +2076,6 @@ describe('OCA.Files.FileList tests', function() { }); }); describe('Sorting files', function() { - it('Sorts by name by default', function() { - fileList.reload(); - expect(fakeServer.requests.length).toEqual(1); - var url = fakeServer.requests[0].url; - var query = OC.parseQueryString(url.substr(url.indexOf('?') + 1)); - expect(query.sort).toEqual('name'); - expect(query.sortdirection).toEqual('asc'); - }); - it('Reloads file list with a different sort when clicking on column header of unsorted column', function() { - fileList.$el.find('.column-size .columntitle').click(); - expect(fakeServer.requests.length).toEqual(1); - var url = fakeServer.requests[0].url; - var query = OC.parseQueryString(url.substr(url.indexOf('?') + 1)); - expect(query.sort).toEqual('size'); - expect(query.sortdirection).toEqual('desc'); - }); - it('Toggles sort direction when clicking on already sorted column', function() { - fileList.$el.find('.column-name .columntitle').click(); - expect(fakeServer.requests.length).toEqual(1); - var url = fakeServer.requests[0].url; - var query = OC.parseQueryString(url.substr(url.indexOf('?') + 1)); - expect(query.sort).toEqual('name'); - expect(query.sortdirection).toEqual('desc'); - }); it('Toggles the sort indicator when clicking on a column header', function() { var ASC_CLASS = fileList.SORT_INDICATOR_ASC_CLASS; var DESC_CLASS = fileList.SORT_INDICATOR_DESC_CLASS; @@ -2191,28 +2125,15 @@ describe('OCA.Files.FileList tests', function() { it('Uses correct sort comparator when inserting files', function() { testFiles.sort(OCA.Files.FileList.Comparators.size); testFiles.reverse(); //default is descending - // this will make it reload the testFiles with the correct sorting + fileList.setFiles(testFiles); fileList.$el.find('.column-size .columntitle').click(); - expect(fakeServer.requests.length).toEqual(1); - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({ - status: 'success', - data: { - files: testFiles, - permissions: 31 - } - }) - ); - var newFileData = { + var newFileData = new FileInfo({ id: 999, - type: 'file', name: 'new file.txt', mimetype: 'text/plain', size: 40001, etag: '999' - }; + }); fileList.add(newFileData); expect(fileList.findFileEl('Three.pdf').index()).toEqual(0); expect(fileList.findFileEl('new file.txt').index()).toEqual(1); @@ -2224,41 +2145,18 @@ describe('OCA.Files.FileList tests', function() { }); it('Uses correct reversed sort comparator when inserting files', function() { testFiles.sort(OCA.Files.FileList.Comparators.size); - // this will make it reload the testFiles with the correct sorting + fileList.setFiles(testFiles); fileList.$el.find('.column-size .columntitle').click(); - expect(fakeServer.requests.length).toEqual(1); - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({ - status: 'success', - data: { - files: testFiles, - permissions: 31 - } - }) - ); + // reverse sort fileList.$el.find('.column-size .columntitle').click(); - fakeServer.requests[1].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({ - status: 'success', - data: { - files: testFiles, - permissions: 31 - } - }) - ); - var newFileData = { + var newFileData = new FileInfo({ id: 999, - type: 'file', name: 'new file.txt', mimetype: 'text/plain', size: 40001, etag: '999' - }; + }); fileList.add(newFileData); expect(fileList.findFileEl('One.txt').index()).toEqual(0); expect(fileList.findFileEl('somedir').index()).toEqual(1); @@ -2290,87 +2188,96 @@ describe('OCA.Files.FileList tests', function() { }); describe('create file', function() { var deferredCreate; + var deferredInfo; + var createStub; + var getFileInfoStub; beforeEach(function() { deferredCreate = $.Deferred(); + deferredInfo = $.Deferred(); + createStub = sinon.stub(filesClient, 'putFileContents') + .returns(deferredCreate.promise()); + getFileInfoStub = sinon.stub(filesClient, 'getFileInfo') + .returns(deferredInfo.promise()); + }); + afterEach(function() { + createStub.restore(); + getFileInfoStub.restore(); }); it('creates file with given name and adds it to the list', function() { - var deferred = fileList.createFile('test file.txt'); - var successStub = sinon.stub(); - var failureStub = sinon.stub(); + fileList.createFile('test.txt'); - deferred.done(successStub); - deferred.fail(failureStub); + expect(createStub.calledOnce).toEqual(true); + expect(createStub.getCall(0).args[0]).toEqual('/subdir/test.txt'); + expect(createStub.getCall(0).args[2]).toEqual({ + contentType: 'text/plain', + overwrite: true + }); - expect(fakeServer.requests.length).toEqual(1); - expect(fakeServer.requests[0].url).toEqual(OC.generateUrl('/apps/files/ajax/newfile.php')); + deferredCreate.resolve(200); - var query = fakeServer.requests[0].requestBody; - expect(OC.parseQueryString(query)).toEqual({ - dir: '/subdir', - filename: 'test file.txt' - }); + expect(getFileInfoStub.calledOnce).toEqual(true); + expect(getFileInfoStub.getCall(0).args[0]).toEqual('/subdir/test.txt'); - fakeServer.requests[0].respond( + deferredInfo.resolve( 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({ - status: 'success', - data: { - path: '/subdir', - name: 'test file.txt', - mimetype: 'text/plain' - } + new FileInfo({ + path: '/subdir', + name: 'test.txt', + mimetype: 'text/plain' }) ); - var $tr = fileList.findFileEl('test file.txt'); + var $tr = fileList.findFileEl('test.txt'); expect($tr.length).toEqual(1); expect($tr.attr('data-mime')).toEqual('text/plain'); - - expect(successStub.calledOnce).toEqual(true); - expect(failureStub.notCalled).toEqual(true); }); // TODO: error cases // TODO: unique name cases }); - describe('create directory', function() { - it('creates directory with given name and adds it to the list', function() { - var deferred = fileList.createDirectory('test directory'); - var successStub = sinon.stub(); - var failureStub = sinon.stub(); - - deferred.done(successStub); - deferred.fail(failureStub); - - expect(fakeServer.requests.length).toEqual(1); - expect(fakeServer.requests[0].url).toEqual(OC.generateUrl('/apps/files/ajax/newfolder.php')); - var query = fakeServer.requests[0].requestBody; - expect(OC.parseQueryString(query)).toEqual({ - dir: '/subdir', - foldername: 'test directory' - }); + describe('create folder', function() { + var deferredCreate; + var deferredInfo; + var createStub; + var getFileInfoStub; + + beforeEach(function() { + deferredCreate = $.Deferred(); + deferredInfo = $.Deferred(); + createStub = sinon.stub(filesClient, 'createDirectory') + .returns(deferredCreate.promise()); + getFileInfoStub = sinon.stub(filesClient, 'getFileInfo') + .returns(deferredInfo.promise()); + }); + afterEach(function() { + createStub.restore(); + getFileInfoStub.restore(); + }); + + it('creates folder with given name and adds it to the list', function() { + fileList.createDirectory('sub dir'); + + expect(createStub.calledOnce).toEqual(true); + expect(createStub.getCall(0).args[0]).toEqual('/subdir/sub dir'); - fakeServer.requests[0].respond( + deferredCreate.resolve(200); + + expect(getFileInfoStub.calledOnce).toEqual(true); + expect(getFileInfoStub.getCall(0).args[0]).toEqual('/subdir/sub dir'); + + deferredInfo.resolve( 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({ - status: 'success', - data: { - path: '/subdir', - name: 'test directory', - mimetype: 'httpd/unix-directory' - } + new FileInfo({ + path: '/subdir', + name: 'sub dir', + mimetype: 'httpd/unix-directory' }) ); - var $tr = fileList.findFileEl('test directory'); + var $tr = fileList.findFileEl('sub dir'); expect($tr.length).toEqual(1); expect($tr.attr('data-mime')).toEqual('httpd/unix-directory'); - - expect(successStub.calledOnce).toEqual(true); - expect(failureStub.notCalled).toEqual(true); }); // TODO: error cases // TODO: unique name cases @@ -2481,14 +2388,14 @@ describe('OCA.Files.FileList tests', function() { expect(ev.result).not.toEqual(false); }); it('drop on a folder row inside the table triggers upload to target folder', function() { - var ev, formData; + var ev; ev = dropOn(fileList.findFileEl('somedir').find('td:eq(2)'), uploadData); expect(ev.result).not.toEqual(false); expect(uploadData.targetDir).toEqual('/subdir/somedir'); }); it('drop on a breadcrumb inside the table triggers upload to target folder', function() { - var ev, formData; + var ev; fileList.changeDirectory('a/b/c/d'); ev = dropOn(fileList.$el.find('.crumb:eq(2)'), uploadData); @@ -2497,32 +2404,50 @@ describe('OCA.Files.FileList tests', function() { }); }); }); - describe('Handeling errors', function () { - var redirectStub; + describe('Handling errors', function () { + var deferredList; + var getFolderContentsStub; - beforeEach(function () { - redirectStub = sinon.stub(OC, 'redirect'); - - fileList = new OCA.Files.FileList($('#app-content-files')); + beforeEach(function() { + deferredList = $.Deferred(); + getFolderContentsStub = + sinon.stub(filesClient, 'getFolderContents'); + getFolderContentsStub.onCall(0).returns(deferredList.promise()); + getFolderContentsStub.onCall(1).returns($.Deferred().promise()); + fileList.reload(); }); - afterEach(function () { + afterEach(function() { + getFolderContentsStub.restore(); fileList = undefined; + }); + it('redirects to files app in case of auth error', function () { + deferredList.reject(401, 'Authentication error'); - redirectStub.restore(); + expect(redirectStub.calledOnce).toEqual(true); + expect(redirectStub.getCall(0).args[0]).toEqual(OC.webroot + '/index.php/apps/files'); + expect(getFolderContentsStub.calledOnce).toEqual(true); }); - it('reloads the page on authentication errors', function () { - fileList.reload(); - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({ - status: 'error', - data: { - 'error': 'authentication_error' - } - }) - ); - expect(redirectStub.calledWith(OC.generateUrl('apps/files'))).toEqual(true); + it('redirects to root folder in case of forbidden access', function () { + deferredList.reject(403); + + expect(fileList.getCurrentDirectory()).toEqual('/'); + expect(getFolderContentsStub.calledTwice).toEqual(true); + }); + it('redirects to root folder and shows notification in case of internal server error', function () { + expect(notificationStub.notCalled).toEqual(true); + deferredList.reject(500); + + expect(fileList.getCurrentDirectory()).toEqual('/'); + expect(getFolderContentsStub.calledTwice).toEqual(true); + expect(notificationStub.calledOnce).toEqual(true); + }); + it('redirects to root folder and shows notification in case of storage not available', function () { + expect(notificationStub.notCalled).toEqual(true); + deferredList.reject(503, 'Storage not available'); + + expect(fileList.getCurrentDirectory()).toEqual('/'); + expect(getFolderContentsStub.calledTwice).toEqual(true); + expect(notificationStub.calledOnce).toEqual(true); }); }); describe('showFileBusyState', function() { diff --git a/apps/files/tests/js/filesSpec.js b/apps/files/tests/js/filesSpec.js index 30e6675c155..b7627d59fdf 100644 --- a/apps/files/tests/js/filesSpec.js +++ b/apps/files/tests/js/filesSpec.js @@ -76,11 +76,11 @@ describe('OCA.Files.Files tests', function() { describe('getDownloadUrl', function() { it('returns the ajax download URL when filename and dir specified', function() { var url = Files.getDownloadUrl('test file.txt', '/subdir'); - expect(url).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=test%20file.txt'); + expect(url).toEqual(OC.webroot + '/remote.php/webdav/subdir/test%20file.txt'); }); - it('returns the ajax download URL when filename and root dir specific', function() { + it('returns the webdav download URL when filename and root dir specified', function() { var url = Files.getDownloadUrl('test file.txt', '/'); - expect(url).toEqual(OC.webroot + '/index.php/apps/files/ajax/download.php?dir=%2F&files=test%20file.txt'); + expect(url).toEqual(OC.webroot + '/remote.php/webdav/test%20file.txt'); }); it('returns the ajax download URL when multiple files specified', function() { var url = Files.getDownloadUrl(['test file.txt', 'abc.txt'], '/subdir'); |