From 58c204abb472f2efeda4b0386cbb7be5e3a4bafd Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 13 Jun 2014 10:02:19 +0200 Subject: [PATCH] Added JS unit tests for ext storage Added JS unit tests for the external storage file list extension. --- apps/files_external/tests/appSpec.js | 98 +++++++++++ .../tests/js/mountsfilelistSpec.js | 155 ++++++++++++++++++ tests/karma.config.js | 10 ++ 3 files changed, 263 insertions(+) create mode 100644 apps/files_external/tests/appSpec.js create mode 100644 apps/files_external/tests/js/mountsfilelistSpec.js diff --git a/apps/files_external/tests/appSpec.js b/apps/files_external/tests/appSpec.js new file mode 100644 index 0000000000..b00442384d --- /dev/null +++ b/apps/files_external/tests/appSpec.js @@ -0,0 +1,98 @@ +/** +* ownCloud +* +* @author Vincent Petry +* @copyright 2014 Vincent Petry +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see . +* +*/ + +describe('OCA.External.App tests', function() { + var App = OCA.External.App; + var fileList; + + beforeEach(function() { + $('#testArea').append( + '
' + + '
  • Files
  • ' + + '
  • ' + + '
  • ' + + '
' + + '
' + + '' + + '' + + '
' + + '' + ); + fileList = App.initList($('#app-content-extstoragemounts')); + }); + afterEach(function() { + App.fileList = null; + fileList = null; + }); + + describe('initialization', function() { + it('inits external mounts list on show', function() { + expect(App.fileList).toBeDefined(); + }); + }); + describe('file actions', function() { + it('provides default file actions', function() { + var fileActions = fileList.fileActions; + + expect(fileActions.actions.all).toBeDefined(); + expect(fileActions.actions.all.Delete).toBeDefined(); + expect(fileActions.actions.all.Rename).toBeDefined(); + expect(fileActions.actions.all.Download).toBeDefined(); + + expect(fileActions.defaults.dir).toEqual('Open'); + }); + it('redirects to files app when opening a directory', function() { + var oldList = OCA.Files.App.fileList; + // dummy new list to make sure it exists + OCA.Files.App.fileList = new OCA.Files.FileList($('
')); + + var setActiveViewStub = sinon.stub(OCA.Files.App, 'setActiveView'); + // create dummy table so we can click the dom + var $table = '
'; + $('#app-content-extstoragemounts').append($table); + + App._inFileList = null; + fileList = App.initList($('#app-content-extstoragemounts')); + + fileList.setFiles([{ + name: 'testdir', + type: 'dir', + path: '/somewhere/inside/subdir', + counterParts: ['user2'], + shareOwner: 'user2' + }]); + + fileList.findFileEl('testdir').find('td a.name').click(); + + expect(OCA.Files.App.fileList.getCurrentDirectory()).toEqual('/somewhere/inside/subdir/testdir'); + + expect(setActiveViewStub.calledOnce).toEqual(true); + expect(setActiveViewStub.calledWith('files')).toEqual(true); + + setActiveViewStub.restore(); + + // restore old list + OCA.Files.App.fileList = oldList; + }); + }); +}); diff --git a/apps/files_external/tests/js/mountsfilelistSpec.js b/apps/files_external/tests/js/mountsfilelistSpec.js new file mode 100644 index 0000000000..96a6b622a4 --- /dev/null +++ b/apps/files_external/tests/js/mountsfilelistSpec.js @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2014 Vincent Petry + * + * This file is licensed under the Affero General Public License version 3 + * or later. + * + * See the COPYING-README file. + * + */ + +describe('OCA.External.FileList tests', function() { + var testFiles, alertStub, notificationStub, fileList, fileActions; + var oldFileListPrototype; + + beforeEach(function() { + alertStub = sinon.stub(OC.dialogs, 'alert'); + notificationStub = sinon.stub(OC.Notification, 'show'); + + // init parameters and test table elements + $('#testArea').append( + '
' + + // init horrible parameters + '' + + '' + + // dummy controls + '
' + + '
' + + '
' + + '
' + + // dummy table + // TODO: at some point this will be rendered by the fileList class itself! + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '
Empty content message
' + + '
' + ); + fileActions = new OCA.Files.FileActions(); + }); + afterEach(function() { + OCA.Files.FileList.prototype = oldFileListPrototype; + testFiles = undefined; + fileList = undefined; + fileActions = undefined; + + notificationStub.restore(); + alertStub.restore(); + }); + + describe('loading file list for external storages', function() { + var ocsResponse; + + beforeEach(function() { + fileList = new OCA.External.FileList( + $('#app-content-container') + ); + + fileList.reload(); + + /* jshint camelcase: false */ + ocsResponse = { + ocs: { + meta: { + status: 'ok', + statuscode: 100, + message: null + }, + data: [{ + name: 'smb mount', + path: '/mount points', + type: 'dir', + backend: 'SMB', + scope: 'personal', + permissions: OC.PERMISSION_READ | OC.PERMISSION_DELETE + }, { + name: 'sftp mount', + path: '/another mount points', + type: 'dir', + backend: 'SFTP', + scope: 'system', + permissions: OC.PERMISSION_READ + }] + } + }; + }); + it('render storage list', function() { + var request; + var $rows; + var $tr; + + expect(fakeServer.requests.length).toEqual(1); + request = fakeServer.requests[0]; + expect(request.url).toEqual( + OC.linkToOCS('apps/files_external/api/v1') + + 'mounts?format=json' + ); + + fakeServer.requests[0].respond( + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify(ocsResponse) + ); + + $rows = fileList.$el.find('tbody tr'); + expect($rows.length).toEqual(2); + + $tr = $rows.eq(0); + expect($tr.attr('data-id')).not.toBeDefined(); + expect($tr.attr('data-type')).toEqual('dir'); + expect($tr.attr('data-file')).toEqual('sftp mount'); + expect($tr.attr('data-path')).toEqual('/another mount points'); + expect($tr.attr('data-size')).not.toBeDefined(); + expect($tr.attr('data-permissions')).toEqual('1'); // read only + expect($tr.find('a.name').attr('href')).toEqual( + OC.webroot + + '/index.php/apps/files' + + '?dir=/another%20mount%20points/sftp%20mount' + ); + expect($tr.find('.nametext').text().trim()).toEqual('sftp mount'); + expect($tr.find('.column-scope').text().trim()).toEqual('System'); + expect($tr.find('.column-backend').text().trim()).toEqual('SFTP'); + + $tr = $rows.eq(1); + expect($tr.attr('data-id')).not.toBeDefined(); + expect($tr.attr('data-type')).toEqual('dir'); + expect($tr.attr('data-file')).toEqual('smb mount'); + expect($tr.attr('data-path')).toEqual('/mount points'); + expect($tr.attr('data-size')).not.toBeDefined(); + expect($tr.attr('data-permissions')).toEqual('9'); // read and delete + expect($tr.find('a.name').attr('href')).toEqual( + OC.webroot + + '/index.php/apps/files' + + '?dir=/mount%20points/smb%20mount' + ); + expect($tr.find('.nametext').text().trim()).toEqual('smb mount'); + expect($tr.find('.column-scope').text().trim()).toEqual('Personal'); + expect($tr.find('.column-backend').text().trim()).toEqual('SMB'); + + }); + }); +}); diff --git a/tests/karma.config.js b/tests/karma.config.js index 1f903f5821..290790686b 100644 --- a/tests/karma.config.js +++ b/tests/karma.config.js @@ -56,6 +56,16 @@ module.exports = function(config) { 'apps/files_sharing/js/share.js' ], testFiles: ['apps/files_sharing/tests/js/*.js'] + }, + { + name: 'files_external', + srcFiles: [ + // only test these files, others are not ready and mess + // up with the global namespace/classes/state + 'apps/files_external/js/app.js', + 'apps/files_external/js/mountsfilelist.js' + ], + testFiles: ['apps/files_external/tests/js/*.js'] }]; }