123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447 |
- /**
- * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
- *
- * @author Abijeet <abijeetpatro@gmail.com>
- * @author Azul <azul@riseup.net>
- * @author Bernd Stellwag <burned@zerties.org>
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Clark Tomlinson <fallen013@gmail.com>
- * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
- * @author Hasso Tepper <hasso@zone.ee>
- * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
- * @author Joas Schilling <coding@schilljs.com>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Michael Weimann <mail@michael-weimann.eu>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Nazar Mokrynskyi <nazar@mokrynskyi.com>
- * @author noveens <noveen.sachdeva@research.iiit.ac.in>
- * @author Remco Brenninkmeijer <requist1@starmail.nl>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Robin McCorkell <robin@mccorkell.me.uk>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Roland Tapken <roland@bitarbeiter.net>
- * @author Thomas Citharel <nextcloud@tcit.fr>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Tomasz Grobelny <tomasz@grobelny.net>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @license AGPL-3.0-or-later
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- describe('OCA.Files.FileList tests', function() {
- var FileInfo = OC.Files.FileInfo;
- var testFiles, testRoot, notificationStub, fileList, pageSizeStub;
- var bcResizeStub;
- var filesClient;
- var filesConfig;
- var redirectStub;
- /**
- * Generate test file data
- */
- function generateFiles(startIndex, endIndex) {
- var files = [];
- var name;
- for (var i = startIndex; i <= endIndex; i++) {
- name = 'File with index ';
- if (i < 10) {
- // do not rely on localeCompare here
- // and make the sorting predictable
- // cross-browser
- name += '0';
- }
- name += i + '.txt';
- files.push(new FileInfo({
- id: i,
- type: 'file',
- name: name,
- mimetype: 'text/plain',
- size: i * 2,
- etag: 'abc'
- }));
- }
- return files;
- }
- beforeEach(function() {
- filesConfig = new OC.Backbone.Model({
- showhidden: true
- });
- filesClient = new OC.Files.Client({
- host: 'localhost',
- port: 80,
- // FIXME: uncomment after fixing the test OC.getRootPath()
- //root: OC.getRootPath() + '/remote.php/webdav',
- root: '/remote.php/webdav',
- useHTTPS: false
- });
- redirectStub = sinon.stub(OC, 'redirect');
- notificationStub = sinon.stub(OC.Notification, 'show');
- // prevent resize algo to mess up breadcrumb order while
- // testing
- bcResizeStub = sinon.stub(OCA.Files.BreadCrumb.prototype, '_resize');
- // init parameters and test table elements
- $('#testArea').append(
- '<div id="app-content-files">' +
- // init horrible parameters
- '<input type="hidden" id="permissions" value="31"/>' +
- // dummy controls
- '<div class="files-controls">' +
- ' <div class="actions creatable"></div>' +
- ' <div class="notCreatable"></div>' +
- '</div>' +
- // uploader
- '<input type="file" id="file_upload_start" name="files[]" multiple="multiple">' +
- // dummy table
- // TODO: at some point this will be rendered by the fileList class itself!
- '<table class="files-filestable list-container view-grid">' +
- '<thead><tr>' +
- '<th class="hidden column-name">' +
- '<input type="checkbox" id="select_all_files" class="select-all checkbox">' +
- '<a class="name columntitle" href="#" onclick="event.preventDefault()" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' +
- '<span class="selectedActions hidden">' +
- '<a class="actions-selected" href="#" onclick="event.preventDefault()"><span class="icon icon-more"></span><span>Actions</span></a>' +
- '</th>' +
- '<th class="hidden column-size"><a class="columntitle" href="#" onclick="event.preventDefault()" data-sort="size"><span class="sort-indicator"></span></a></th>' +
- '<th class="hidden column-mtime"><a class="columntitle" href="#" onclick="event.preventDefault()" data-sort="mtime"><span class="sort-indicator"></span></a></th>' +
- '</tr></thead>' +
- '<tbody class="files-fileList"></tbody>' +
- '<tfoot></tfoot>' +
- '</table>' +
- // TODO: move to handlebars template
- '<div class="emptyfilelist emptycontent"><h2>Empty content message</h2><p class="uploadmessage">Upload message</p></div>' +
- '<div class="nofilterresults hidden"></div>' +
- '</div>'
- );
- 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',
- mimetype: 'text/plain',
- mtime: 123456789,
- size: 12,
- etag: 'abc',
- permissions: OC.PERMISSION_ALL
- }), new FileInfo({
- id: 2,
- type: 'file',
- name: 'Two.jpg',
- mimetype: 'image/jpeg',
- mtime: 234567890,
- size: 12049,
- etag: 'def',
- permissions: OC.PERMISSION_ALL
- }), new FileInfo({
- id: 3,
- type: 'file',
- name: 'Three.pdf',
- mimetype: 'application/pdf',
- mtime: 234560000,
- size: 58009,
- etag: '123',
- permissions: OC.PERMISSION_ALL
- }), new FileInfo({
- id: 4,
- type: 'dir',
- name: 'somedir',
- mimetype: 'httpd/unix-directory',
- mtime: 134560000,
- 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'), {
- filesClient: filesClient,
- config: filesConfig,
- dir: '/subdir',
- enableUpload: true,
- multiSelectMenu: [{
- name: 'copyMove',
- displayName: t('files', 'Move or copy'),
- iconClass: 'icon-external',
- },
- {
- name: 'download',
- displayName: t('files', 'Download'),
- iconClass: 'icon-download',
- },
- {
- name: 'delete',
- displayName: t('files', 'Delete'),
- iconClass: 'icon-delete',
- }]
- });
- });
- afterEach(function() {
- testFiles = undefined;
- if (fileList) {
- fileList.destroy();
- }
- fileList = undefined;
- notificationStub.restore();
- bcResizeStub.restore();
- pageSizeStub.restore();
- redirectStub.restore();
- });
- describe('Getters', function() {
- it('Returns the current directory', function() {
- fileList.changeDirectory('/one/two/three', false, true);
- expect(fileList.getCurrentDirectory()).toEqual('/one/two/three');
- });
- it('Returns the directory permissions as int', function() {
- $('#permissions').val('23');
- expect(fileList.getDirectoryPermissions()).toEqual(23);
- });
- });
- describe('Adding files', function() {
- it('generates file element with correct attributes when calling add() with file data', function() {
- var fileData = new FileInfo({
- id: 18,
- name: 'testName.txt',
- mimetype: 'text/plain',
- size: 1234,
- etag: 'a01234c',
- mtime: 123456
- });
- var $tr = fileList.add(fileData);
- expect($tr).toBeDefined();
- expect($tr[0].tagName.toLowerCase()).toEqual('tr');
- expect($tr.attr('data-id')).toEqual('18');
- expect($tr.attr('data-type')).toEqual('file');
- expect($tr.attr('data-file')).toEqual('testName.txt');
- expect($tr.attr('data-size')).toEqual('1234');
- expect($tr.attr('data-etag')).toEqual('a01234c');
- expect($tr.attr('data-permissions')).toEqual('31');
- expect($tr.attr('data-mime')).toEqual('text/plain');
- expect($tr.attr('data-mtime')).toEqual('123456');
- expect($tr.attr('data-e2eencrypted')).toEqual('false');
- expect($tr.find('a.name').attr('href'))
- .toEqual(OC.getRootPath() + '/remote.php/webdav/subdir/testName.txt');
- expect($tr.find('.nametext').text().trim()).toEqual('testName.txt');
- expect($tr.find('.filesize').text()).toEqual('1 KB');
- expect($tr.find('.date').text()).not.toEqual('?');
- expect(fileList.findFileEl('testName.txt')[0]).toEqual($tr[0]);
- });
- it('generates file element with url for default action when one is defined', function() {
- var actionStub = sinon.stub();
- fileList.setFiles(testFiles);
- fileList.fileActions.registerAction({
- mime: 'text/plain',
- name: 'Test',
- type: OCA.Files.FileActions.TYPE_INLINE,
- permissions: OC.PERMISSION_ALL,
- icon: function() {
- // Specify icon for history button
- return OC.imagePath('core','actions/history');
- },
- actionHandler: actionStub
- });
- fileList.fileActions.setDefault('text/plain', 'Test');
- var fileData = new FileInfo({
- id: 18,
- name: 'testName.txt',
- mimetype: 'text/plain',
- size: 1234,
- etag: 'a01234c',
- mtime: 123456
- });
- var $tr = fileList.add(fileData);
- expect($tr.find('a.name').attr('href'))
- .toEqual(OC.getRootPath() + '/index.php/apps/files?dir=&openfile=18');
- });
- it('generates dir element with correct attributes when calling add() with dir data', function() {
- var fileData = new FileInfo({
- id: 19,
- name: 'testFolder',
- mimetype: 'httpd/unix-directory',
- size: 1234,
- etag: 'a01234c',
- mtime: 123456
- });
- var $tr = fileList.add(fileData);
- expect($tr).toBeDefined();
- expect($tr[0].tagName.toLowerCase()).toEqual('tr');
- expect($tr.attr('data-id')).toEqual('19');
- expect($tr.attr('data-type')).toEqual('dir');
- expect($tr.attr('data-file')).toEqual('testFolder');
- expect($tr.attr('data-size')).toEqual('1234');
- expect($tr.attr('data-etag')).toEqual('a01234c');
- expect($tr.attr('data-permissions')).toEqual('31');
- expect($tr.attr('data-mime')).toEqual('httpd/unix-directory');
- expect($tr.attr('data-mtime')).toEqual('123456');
- expect($tr.attr('data-e2eencrypted')).toEqual('false');
- expect($tr.find('.filesize').text()).toEqual('1 KB');
- expect($tr.find('.date').text()).not.toEqual('?');
- expect(fileList.findFileEl('testFolder')[0]).toEqual($tr[0]);
- });
- it('generates file element with default attributes when calling add() with minimal data', function() {
- var fileData = {
- type: 'file',
- name: 'testFile.txt'
- };
- var $tr = fileList.add(fileData);
- expect($tr).toBeDefined();
- expect($tr[0].tagName.toLowerCase()).toEqual('tr');
- expect($tr.attr('data-id')).toBeUndefined();
- expect($tr.attr('data-type')).toEqual('file');
- expect($tr.attr('data-file')).toEqual('testFile.txt');
- expect($tr.attr('data-size')).toBeUndefined();
- expect($tr.attr('data-etag')).toBeUndefined();
- expect($tr.attr('data-permissions')).toEqual('31');
- expect($tr.attr('data-mime')).toBeUndefined();
- expect($tr.attr('data-e2eencrypted')).toEqual('false');
- expect($tr.find('.filesize').text()).toEqual('Pending');
- expect($tr.find('.date').text()).not.toEqual('?');
- });
- it('generates dir element with default attributes when calling add() with minimal data', function() {
- var fileData = {
- type: 'dir',
- name: 'testFolder'
- };
- var $tr = fileList.add(fileData);
- expect($tr).toBeDefined();
- expect($tr[0].tagName.toLowerCase()).toEqual('tr');
- expect($tr.attr('data-id')).toBeUndefined();
- expect($tr.attr('data-type')).toEqual('dir');
- expect($tr.attr('data-file')).toEqual('testFolder');
- expect($tr.attr('data-size')).toBeUndefined();
- expect($tr.attr('data-etag')).toBeUndefined();
- expect($tr.attr('data-permissions')).toEqual('31');
- expect($tr.attr('data-mime')).toEqual('httpd/unix-directory');
- expect($tr.attr('data-e2eencrypted')).toEqual('false');
- expect($tr.find('.filesize').text()).toEqual('Pending');
- expect($tr.find('.date').text()).not.toEqual('?');
- });
- it('generates dir element with true e2eencrypted attribute when calling add() with minimal data including isEncrypted', function() {
- var fileData = {
- type: 'dir',
- name: 'testFolder',
- isEncrypted: true
- };
- var $tr = fileList.add(fileData);
- expect($tr.attr('data-e2eencrypted')).toEqual('true');
- });
- it('generates file element with no permissions when permissions are explicitly none', function() {
- var fileData = {
- type: 'dir',
- name: 'testFolder',
- permissions: OC.PERMISSION_NONE
- };
- var $tr = fileList.add(fileData);
- expect($tr.attr('data-permissions')).toEqual('0');
- });
- it('generates file element with zero size when size is explicitly zero', function() {
- var fileData = {
- type: 'dir',
- name: 'testFolder',
- size: '0'
- };
- var $tr = fileList.add(fileData);
- expect($tr.find('.filesize').text()).toEqual('0 KB');
- });
- it('generates file element with unknown date when mtime invalid', function() {
- var fileData = {
- type: 'dir',
- name: 'testFolder',
- mtime: -1
- };
- var $tr = fileList.add(fileData);
- expect($tr.find('.date .modified').text()).toEqual('?');
- });
- it('adds new file to the end of the list', function() {
- var $tr;
- var fileData = {
- type: 'file',
- name: 'ZZZ.txt'
- };
- fileList.setFiles(testFiles);
- $tr = fileList.add(fileData);
- expect($tr.index()).toEqual(4);
- });
- it('inserts files in a sorted manner when insert option is enabled', function() {
- for (var i = 0; i < testFiles.length; i++) {
- fileList.add(testFiles[i]);
- }
- expect(fileList.files[0].name).toEqual('somedir');
- expect(fileList.files[1].name).toEqual('One.txt');
- expect(fileList.files[2].name).toEqual('Three.pdf');
- expect(fileList.files[3].name).toEqual('Two.jpg');
- });
- it('inserts new file at correct position', function() {
- var $tr;
- var fileData = {
- type: 'file',
- name: 'P comes after O.txt'
- };
- for (var i = 0; i < testFiles.length; i++) {
- fileList.add(testFiles[i]);
- }
- $tr = fileList.add(fileData);
- // after "One.txt"
- expect($tr.index()).toEqual(2);
- expect(fileList.files[2]).toEqual(fileData);
- });
- it('inserts new folder at correct position in insert mode', function() {
- var $tr;
- var fileData = {
- type: 'dir',
- name: 'somedir2 comes after somedir'
- };
- for (var i = 0; i < testFiles.length; i++) {
- fileList.add(testFiles[i]);
- }
- $tr = fileList.add(fileData);
- expect($tr.index()).toEqual(1);
- expect(fileList.files[1]).toEqual(fileData);
- });
- it('inserts new file at the end correctly', function() {
- var $tr;
- var fileData = {
- type: 'file',
- name: 'zzz.txt'
- };
- for (var i = 0; i < testFiles.length; i++) {
- fileList.add(testFiles[i]);
- }
- $tr = fileList.add(fileData);
- expect($tr.index()).toEqual(4);
- expect(fileList.files[4]).toEqual(fileData);
- });
- it('removes empty content message and shows summary when adding first file', function() {
- var $summary;
- var fileData = {
- type: 'file',
- name: 'first file.txt',
- size: 12
- };
- fileList.setFiles([]);
- expect(fileList.isEmpty).toEqual(true);
- fileList.add(fileData);
- $summary = $('.files-filestable .summary');
- expect($summary.hasClass('hidden')).toEqual(false);
- // yes, ugly...
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(true);
- expect($summary.find('.connector').hasClass('hidden')).toEqual(true);
- expect($summary.find('.fileinfo').hasClass('hidden')).toEqual(false);
- expect($summary.find('.filesize').text()).toEqual('12 B');
- expect($('.files-filestable thead th').hasClass('hidden')).toEqual(false);
- expect($('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(true);
- expect(fileList.isEmpty).toEqual(false);
- });
- it('correctly adds the extension markup and show hidden files completely in gray', function() {
- var $tr;
- var testDataAndExpectedResult = [
- {file: {type: 'file', name: 'ZZZ.txt'}, extension: '.txt'},
- {file: {type: 'file', name: 'ZZZ.tar.gz'}, extension: '.gz'},
- {file: {type: 'file', name: 'test.with.some.dots.in.it.txt'}, extension: '.txt'},
- // we render hidden files completely in gray
- {file: {type: 'file', name: '.test.with.some.dots.in.it.txt'}, extension: '.test.with.some.dots.in.it.txt'},
- {file: {type: 'file', name: '.hidden'}, extension: '.hidden'},
- ];
- fileList.setFiles(testFiles);
- for(var i = 0; i < testDataAndExpectedResult.length; i++) {
- var testSet = testDataAndExpectedResult[i];
- var fileData = testSet['file'];
- $tr = fileList.add(fileData);
- expect($tr.find('.nametext .extension').text()).toEqual(testSet['extension']);
- }
- });
- });
- describe('Hidden files', function() {
- it('sets the class hidden-file for hidden files', function() {
- var fileData = {
- type: 'dir',
- name: '.testFolder'
- };
- var $tr = fileList.add(fileData);
- expect($tr).toBeDefined();
- expect($tr.hasClass('hidden-file')).toEqual(true);
- });
- it('does not set the class hidden-file for visible files', function() {
- var fileData = {
- type: 'dir',
- name: 'testFolder'
- };
- var $tr = fileList.add(fileData);
- expect($tr).toBeDefined();
- expect($tr.hasClass('hidden-file')).toEqual(false);
- });
- it('toggles the list\'s class when toggling hidden files', function() {
- expect(fileList.$el.hasClass('hide-hidden-files')).toEqual(false);
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: false });
- expect(fileList.$el.hasClass('hide-hidden-files')).toEqual(true);
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: true })
- expect(fileList.$el.hasClass('hide-hidden-files')).toEqual(false);
- });
- });
- describe('Removing files from the list', function() {
- it('Removes file from list when calling remove() and updates summary', function() {
- var $summary;
- var $removedEl;
- fileList.setFiles(testFiles);
- $removedEl = fileList.remove('One.txt');
- expect($removedEl).toBeDefined();
- expect($removedEl.attr('data-file')).toEqual('One.txt');
- expect($('.files-fileList tr').length).toEqual(3);
- expect(fileList.files.length).toEqual(3);
- expect(fileList.findFileEl('One.txt').length).toEqual(0);
- $summary = $('.files-filestable .summary');
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').text()).toEqual('1 folder');
- expect($summary.find('.fileinfo').text()).toEqual('2 files');
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(false);
- expect($summary.find('.fileinfo').hasClass('hidden')).toEqual(false);
- expect($summary.find('.filesize').text()).toEqual('69 KB');
- expect(fileList.isEmpty).toEqual(false);
- });
- it('Shows empty content when removing last file', function() {
- var $summary;
- fileList.setFiles([testFiles[0]]);
- fileList.remove('One.txt');
- expect($('.files-fileList tr').length).toEqual(0);
- expect(fileList.files.length).toEqual(0);
- expect(fileList.findFileEl('One.txt').length).toEqual(0);
- $summary = $('.files-filestable .summary');
- expect($summary.hasClass('hidden')).toEqual(true);
- expect($('.files-filestable thead th').hasClass('hidden')).toEqual(true);
- expect($('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(false);
- expect(fileList.isEmpty).toEqual(true);
- });
- });
- describe('Deleting files', function() {
- var deferredDelete;
- var deleteStub;
- beforeEach(function() {
- deferredDelete = $.Deferred();
- deleteStub = sinon.stub(filesClient, 'remove');
- });
- afterEach(function() {
- deleteStub.restore();
- });
- function doDelete() {
- // note: normally called from FileActions
- return fileList.do_delete(['One.txt', 'Two.jpg']).then(function(){
- 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(done) {
- var $summary;
- fileList.setFiles(testFiles);
- deferredDelete1 = $.Deferred();
- deferredDelete2 = $.Deferred();
- deleteStub.onCall(0).callsFake(function(src){
- expect(deleteStub.calledOnce).toEqual(true);
- expect(src).toEqual('/subdir/One.txt');
- return deferredDelete1.promise();
- });
- deleteStub.onCall(1).callsFake(function(src){
- expect(deleteStub.calledTwice).toEqual(true);
- expect(src).toEqual('/subdir/Two.jpg');
- return deferredDelete2.promise();
- });
- var promise = fileList.do_delete(['One.txt', 'Two.jpg']);
- deferredDelete1.resolve(200);
- deferredDelete2.resolve(200);
- return promise.then(function(){
- expect(fileList.findFileEl('One.txt').length).toEqual(0);
- expect(fileList.findFileEl('Two.jpg').length).toEqual(0);
- expect(fileList.findFileEl('Three.pdf').length).toEqual(1);
- expect(fileList.$fileList.find('tr').length).toEqual(2);
- $summary = $('.files-filestable .summary');
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').text()).toEqual('1 folder');
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(false);
- expect($summary.find('.fileinfo').hasClass('hidden')).toEqual(false);
- expect($summary.find('.filesize').text()).toEqual('57 KB');
- expect(fileList.isEmpty).toEqual(false);
- expect($('.files-filestable thead th').hasClass('hidden')).toEqual(false);
- expect($('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(true);
- expect(notificationStub.notCalled).toEqual(true);
- }).then(done, done);
- });
- it('shows busy state on files to be deleted', function(done) {
- fileList.setFiles(testFiles);
- deferredDelete1 = $.Deferred();
- deferredDelete2 = $.Deferred();
- deleteStub.onCall(0).callsFake(function(src){
- expect(fileList.findFileEl('One.txt').hasClass('busy')).toEqual(true);
- expect(fileList.findFileEl('Three.pdf').hasClass('busy')).toEqual(false);
- expect(deleteStub.calledOnce).toEqual(true);
- expect(src).toEqual('/subdir/One.txt');
- return deferredDelete1.promise();
- });
- deleteStub.onCall(1).callsFake(function(src){
- expect(fileList.findFileEl('Two.jpg').hasClass('busy')).toEqual(true);
- expect(fileList.findFileEl('Three.pdf').hasClass('busy')).toEqual(false);
- expect(deleteStub.calledTwice).toEqual(true);
- expect(src).toEqual('/subdir/Two.jpg');
- return deferredDelete2.promise();
- });
- var promise = fileList.do_delete(['One.txt', 'Two.jpg']).then(function(){
- expect(deleteStub.calledTwice).toEqual(true);
- });
- deferredDelete1.resolve(200);
- deferredDelete2.resolve(200);
- return promise.then(function(){
- expect(fileList.findFileEl('One.txt').hasClass('busy')).toEqual(false);
- expect(fileList.findFileEl('Two.jpg').hasClass('busy')).toEqual(false);
- }).then(done, done);
- });
- it('shows busy state on all files when deleting all', function(done) {
- fileList.setFiles(testFiles);
- var deferredDeleteArray = [];
- var count = 0;
- for (var i = 0; i < 4; i++) {
- (function(i, fn){
- deferredDeleteArray.push($.Deferred());
- deleteStub.onCall(i).callsFake(function(src){
- expect(fileList.findFileEl(fn).hasClass('busy')).toEqual(true);
- count++;
- return deferredDeleteArray[i].promise();
- });
- })(i, testFiles[i].name);
- }
- var promise = fileList.do_delete();
- for (var i = 0; i < 4; i++) {
- deferredDeleteArray[i].resolve(200);
- }
- return promise.then(function(){
- expect(count).toEqual(4);
- }).then(done, done);
- });
- it('updates summary when deleting last file', function(done) {
- var $summary;
- fileList.setFiles([testFiles[0], testFiles[1]]);
- deleteStub.returns(deferredDelete.promise());
- deferredDelete.resolve(200);
- return doDelete().then(function(){
- expect(fileList.$fileList.find('tr').length).toEqual(0);
- $summary = $('.files-filestable .summary');
- expect($summary.hasClass('hidden')).toEqual(true);
- expect(fileList.isEmpty).toEqual(true);
- expect(fileList.files.length).toEqual(0);
- expect($('.files-filestable thead th').hasClass('hidden')).toEqual(true);
- expect($('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(false);
- }).then(done, done);
- });
- it('bring back deleted item when delete call failed', function(done) {
- fileList.setFiles(testFiles);
- deleteStub.returns(deferredDelete.promise());
- var promise = doDelete();
- deferredDelete.reject(403);
- return promise.then(function(){
- // 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.calledTwice).toEqual(true);
- }).then(done, done);
- });
- it('remove file from list if delete call returned 404 not found', function(done) {
- fileList.setFiles(testFiles);
- deleteStub.returns(deferredDelete.promise());
- var promise = doDelete();
- deferredDelete.reject(404);
- return promise.then(function(){
- 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);
- }).then(done, done);
- });
- });
- describe('Renaming files', function() {
- var deferredRename;
- var renameStub;
- beforeEach(function() {
- deferredRename = $.Deferred();
- renameStub = sinon.stub(filesClient, 'move').returns(deferredRename.promise());
- for (var i = 0; i < testFiles.length; i++) {
- var file = testFiles[i];
- file.path = '/some/subdir';
- fileList.add(file, {silent: true});
- }
- });
- afterEach(function() {
- renameStub.restore();
- });
- function doCancelRename() {
- var $input;
- // trigger rename prompt
- fileList.rename('One.txt');
- $input = fileList.$fileList.find('input.filename');
- // keep same name
- $input.val('One.txt');
- // trigger submit because triggering blur doesn't work in all browsers
- $input.closest('form').trigger('submit');
- expect(renameStub.notCalled).toEqual(true);
- }
- function doRename() {
- var $input;
- // trigger rename prompt
- fileList.rename('One.txt');
- $input = fileList.$fileList.find('input.filename');
- $input.val('Tu_after_three.txt');
- // trigger submit because triggering blur doesn't work in all browsers
- $input.closest('form').trigger('submit');
- 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();
- 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.jpg
- expect(notificationStub.notCalled).toEqual(true);
- });
- it('Reverts file entry if rename ajax call failed', function() {
- doRename();
- 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(notificationStub.calledOnce).toEqual(true);
- });
- it('Correctly updates file link after rename', function() {
- var $tr;
- doRename();
- deferredRename.resolve(201);
- $tr = fileList.findFileEl('Tu_after_three.txt');
- expect($tr.find('a.name').attr('href'))
- .toEqual(OC.getRootPath() + '/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);
- 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 = $('.files-filestable .summary');
- doRename();
- deferredRename.resolve(201);
- expect($summary.find('.dirinfo').text()).toEqual('1 folder');
- expect($summary.find('.fileinfo').text()).toEqual('3 files');
- });
- it('Leaves the summary alone when cancel renaming', function() {
- var $summary = $('.files-filestable .summary');
- doCancelRename();
- expect($summary.find('.dirinfo').text()).toEqual('1 folder');
- expect($summary.find('.fileinfo').text()).toEqual('3 files');
- });
- it('Shows busy state while rename in progress', function() {
- var $tr;
- doRename();
- // element is renamed before the request finishes
- $tr = fileList.findFileEl('Tu_after_three.txt');
- expect($tr.length).toEqual(1);
- expect(fileList.findFileEl('One.txt').length).toEqual(0);
- // file actions are hidden
- expect($tr.hasClass('busy')).toEqual(true);
- // input and form are gone
- expect(fileList.$fileList.find('input.filename').length).toEqual(0);
- expect(fileList.$fileList.find('form').length).toEqual(0);
- });
- it('Validates the file name', function() {
- var $input, $tr;
- $tr = fileList.findFileEl('One.txt');
- expect($tr.find('a.name').css('display')).not.toEqual('none');
- // trigger rename prompt
- fileList.rename('One.txt');
- expect($tr.find('a.name .thumbnail-wrapper').css('display')).not.toEqual('none');
- expect($tr.find('a.name .nametext').css('display')).toEqual('none');
- $input = fileList.$fileList.find('input.filename');
- $input.val('Two.jpg');
- // simulate key to trigger validation
- $input.trigger(new $.Event('keyup', {keyCode: 97}));
- // input is still there with error
- expect(fileList.$fileList.find('input.filename').length).toEqual(1);
- expect(fileList.$fileList.find('input.filename').hasClass('error')).toEqual(true);
- // trigger submit does not send server request
- $input.closest('form').trigger('submit');
- expect(renameStub.notCalled).toEqual(true);
- // simulate escape key
- $input.trigger(new $.Event('keyup', {keyCode: 27}));
- // element is added back with the correct name
- $tr = fileList.findFileEl('One.txt');
- expect($tr.length).toEqual(1);
- expect($tr.find('a .nametext').text().trim()).toEqual('One.txt');
- expect($tr.find('a.name').css('display')).not.toEqual('none');
- $tr = fileList.findFileEl('Two.jpg');
- expect($tr.length).toEqual(1);
- expect($tr.find('a .nametext').text().trim()).toEqual('Two.jpg');
- expect($tr.find('a.name').css('display')).not.toEqual('none');
- // input and form are gone
- expect(fileList.$fileList.find('input.filename').length).toEqual(0);
- expect(fileList.$fileList.find('form').length).toEqual(0);
- });
- it('Restores thumbnail when rename was cancelled', function(done) {
- doRename();
- expect(fileList.findFileEl('Tu_after_three.txt').find('.thumbnail').parent().attr('class'))
- .toContain('icon-loading-small');
- deferredRename.reject(409);
- return Promise.resolve().then(function() {
- expect(fileList.findFileEl('One.txt').length).toEqual(1);
- expect(OC.TestUtil.getImageUrl(fileList.findFileEl('One.txt').find('.thumbnail')))
- .toEqual(OC.imagePath('core', 'filetypes/text.svg'));
- }).then(done, done);
- });
- });
- describe('Moving files', function() {
- var deferredMove;
- var moveStub;
- beforeEach(function() {
- deferredMove = $.Deferred();
- moveStub = sinon.stub(filesClient, 'move');
- fileList.setFiles(testFiles);
- });
- afterEach(function() {
- moveStub.restore();
- });
- it('Moves single file to target folder', function(done) {
- var promise = fileList.move('One.txt', '/somedir');
- moveStub.callsFake(function(src, dst){
- expect(moveStub.calledOnce).toEqual(true);
- expect(src).toEqual('/subdir/One.txt');
- expect(dst).toEqual('/somedir/One.txt');
- return deferredMove.promise();
- });
- deferredMove.resolve(201);
- return promise.then(function(){
- expect(fileList.findFileEl('One.txt').length).toEqual(0);
- // folder size has increased
- expect(fileList.findFileEl('somedir').data('size')).toEqual(262);
- expect(fileList.findFileEl('somedir').find('.filesize').text()).toEqual('262 B');
- expect(notificationStub.notCalled).toEqual(true);
- }).then(done, done);
- });
- it('Moves list of files to target folder', function(done) {
- var deferredMove1 = $.Deferred();
- var deferredMove2 = $.Deferred();
- moveStub.onCall(0).callsFake(function(src, dst){
- expect(moveStub.calledOnce).toEqual(true);
- expect(src).toEqual('/subdir/One.txt');
- expect(dst).toEqual('/somedir/One.txt');
- return deferredMove1.promise();
- });
- moveStub.onCall(1).callsFake(function(src, dst){
- expect(fileList.findFileEl('One.txt').length).toEqual(0);
- // folder size has increased during move
- expect(fileList.findFileEl('somedir').data('size')).toEqual(262);
- expect(fileList.findFileEl('somedir').find('.filesize').text()).toEqual('262 B');
- expect(src).toEqual('/subdir/Two.jpg');
- expect(dst).toEqual('/somedir/Two.jpg');
- return deferredMove2.promise();
- });
- var promise = fileList.move(['One.txt', 'Two.jpg'], '/somedir');
- deferredMove1.resolve(201);
- deferredMove2.resolve(201);
- return promise.then(function(){
- expect(moveStub.calledTwice).toEqual(true);
- expect(fileList.findFileEl('Two.jpg').length).toEqual(0);
- // folder size has increased
- expect(fileList.findFileEl('somedir').data('size')).toEqual(12311);
- expect(fileList.findFileEl('somedir').find('.filesize').text()).toEqual('12 KB');
- expect(notificationStub.notCalled).toEqual(true);
- }).then(done, done);
- });
- it('Shows notification if a file could not be moved', function(done) {
- moveStub.callsFake(function(){
- expect(moveStub.calledOnce).toEqual(true);
- return deferredMove.promise();
- });
- var promise = fileList.move('One.txt', '/somedir');
- deferredMove.reject(409);
- return promise.then(function(){
- expect(fileList.findFileEl('One.txt').length).toEqual(1);
- expect(notificationStub.calledOnce).toEqual(true);
- expect(notificationStub.getCall(0).args[0]).toEqual('Could not move "One.txt"');
- }).then(done, done);
- });
- it('Restores thumbnail if a file could not be moved', function(done) {
- moveStub.callsFake(function(){
- expect(fileList.findFileEl('One.txt').find('.thumbnail').parent().attr('class'))
- .toContain('icon-loading-small');
- expect(moveStub.calledOnce).toEqual(true);
- return deferredMove.promise();
- });
- var promise = fileList.move('One.txt', '/somedir');
- deferredMove.reject(409);
- return promise.then(function(){
- expect(fileList.findFileEl('One.txt').length).toEqual(1);
- expect(notificationStub.calledOnce).toEqual(true);
- 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'));
- }).then(done, done);
- });
- });
- describe('Copying files', function() {
- var deferredCopy;
- var copyStub;
- beforeEach(function() {
- deferredCopy = $.Deferred();
- copyStub = sinon.stub(filesClient, 'copy');
- fileList.setFiles(testFiles);
- });
- afterEach(function() {
- copyStub.restore();
- });
- it('Copies single file to target folder', function(done) {
- copyStub.callsFake(function(){
- expect(copyStub.calledOnce).toEqual(true);
- expect(copyStub.getCall(0).args[0]).toEqual('/subdir/One.txt');
- expect(copyStub.getCall(0).args[1]).toEqual('/somedir/One.txt');
- return deferredCopy.promise();
- });
- var promise = fileList.copy('One.txt', '/somedir');
- deferredCopy.resolve(201);
- return promise.then(function(){
- // File is still here
- expect(fileList.findFileEl('One.txt').length).toEqual(1);
- // folder size has increased
- expect(fileList.findFileEl('somedir').data('size')).toEqual(262);
- expect(fileList.findFileEl('somedir').find('.filesize').text()).toEqual('262 B');
- // Copying sents a notification to tell that we've successfully copied file
- expect(notificationStub.notCalled).toEqual(false);
- }).then(done, done);
- });
- it('Copies list of files to target folder', function(done) {
- var deferredCopy1 = $.Deferred();
- var deferredCopy2 = $.Deferred();
- copyStub.onCall(0).callsFake(function(src, dst){
- expect(src).toEqual('/subdir/One.txt');
- expect(dst).toEqual('/somedir/One.txt');
- return deferredCopy1.promise();
- });
- copyStub.onCall(1).callsFake(function(src, dst){
- // folder size has increased during copy
- expect(fileList.findFileEl('somedir').data('size')).toEqual(262);
- expect(fileList.findFileEl('somedir').find('.filesize').text()).toEqual('262 B');
- expect(src).toEqual('/subdir/Two.jpg');
- expect(dst).toEqual('/somedir/Two.jpg');
- return deferredCopy2.promise();
- });
- var promise = fileList.copy(['One.txt', 'Two.jpg'], '/somedir');
- deferredCopy1.resolve(201);
- deferredCopy2.resolve(201);
- return promise.then(function(){
- expect(copyStub.calledTwice).toEqual(true);
- expect(fileList.findFileEl('Two.jpg').length).toEqual(1);
- expect(fileList.findFileEl('One.txt').length).toEqual(1);
- // folder size has increased
- expect(fileList.findFileEl('somedir').data('size')).toEqual(12311);
- expect(fileList.findFileEl('somedir').find('.filesize').text()).toEqual('12 KB');
- expect(notificationStub.notCalled).toEqual(false);
- }).then(done, done);
- });
- it('Shows notification if a file could not be copied', function(done) {
- copyStub.callsFake(function(){
- expect(copyStub.calledOnce).toEqual(true);
- return deferredCopy.promise();
- });
- var promise = fileList.copy('One.txt', '/somedir');
- deferredCopy.reject(409);
- return promise.then(function(){
- expect(fileList.findFileEl('One.txt').length).toEqual(1);
- expect(notificationStub.calledOnce).toEqual(true);
- expect(notificationStub.getCall(0).args[0]).toEqual('Could not copy "One.txt"');
- }).then(done, done);
- });
- it('Restores thumbnail if a file could not be copied', function(done) {
- copyStub.callsFake(function(){
- expect(fileList.findFileEl('One.txt').find('.thumbnail').parent().attr('class'))
- .toContain('icon-loading-small');
- expect(copyStub.calledOnce).toEqual(true);
- return deferredCopy.promise();
- });
- var promise = fileList.copy('One.txt', '/somedir');
- deferredCopy.reject(409);
- return promise.then(function(){
- expect(fileList.findFileEl('One.txt').length).toEqual(1);
- expect(notificationStub.calledOnce).toEqual(true);
- expect(notificationStub.getCall(0).args[0]).toEqual('Could not copy "One.txt"');
- expect(OC.TestUtil.getImageUrl(fileList.findFileEl('One.txt').find('.thumbnail')))
- .toEqual(OC.imagePath('core', 'filetypes/text.svg'));
- }).then(done, done);
- });
- });
- describe('Update file', function() {
- it('does not change summary', function() {
- var $summary = $('.files-filestable .summary');
- var fileData = new FileInfo({
- type: 'file',
- name: 'test file',
- });
- var $tr = fileList.add(fileData);
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(true);
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- var model = fileList.getModelForFile('test file');
- model.set({size: '100'});
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(true);
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- });
- })
- describe('List rendering', function() {
- it('renders a list of files using add()', function() {
- expect(fileList.files.length).toEqual(0);
- expect(fileList.files).toEqual([]);
- fileList.setFiles(testFiles);
- expect($('.files-fileList tr').length).toEqual(4);
- expect(fileList.files.length).toEqual(4);
- expect(fileList.files).toEqual(testFiles);
- });
- it('updates summary using the file sizes', function() {
- var $summary;
- fileList.setFiles(testFiles);
- $summary = $('.files-filestable .summary');
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').text()).toEqual('1 folder');
- expect($summary.find('.fileinfo').text()).toEqual('3 files');
- expect($summary.find('.filesize').text()).toEqual('69 KB');
- });
- it('shows headers, summary and hide empty content message after setting files', function(){
- fileList.setFiles(testFiles);
- expect($('.files-filestable thead th').hasClass('hidden')).toEqual(false);
- expect($('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(true);
- expect(fileList.$el.find('.summary').hasClass('hidden')).toEqual(false);
- });
- it('hides headers, summary and show empty content message after setting empty file list', function(){
- fileList.setFiles([]);
- expect($('.files-filestable thead th').hasClass('hidden')).toEqual(true);
- expect($('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(false);
- expect($('.emptyfilelist.emptycontent .uploadmessage').hasClass('hidden')).toEqual(false);
- expect(fileList.$el.find('.summary').hasClass('hidden')).toEqual(true);
- });
- it('hides headers, upload message, and summary when list is empty and user has no creation permission', function(){
- $('#permissions').val(0);
- fileList.setFiles([]);
- expect($('.files-filestable thead th').hasClass('hidden')).toEqual(true);
- expect($('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(false);
- expect($('.emptyfilelist.emptycontent .uploadmessage').hasClass('hidden')).toEqual(true);
- expect(fileList.$el.find('.summary').hasClass('hidden')).toEqual(true);
- });
- it('calling findFileEl() can find existing file element', function() {
- fileList.setFiles(testFiles);
- expect(fileList.findFileEl('Two.jpg').length).toEqual(1);
- });
- it('calling findFileEl() returns empty when file not found in file', function() {
- fileList.setFiles(testFiles);
- expect(fileList.findFileEl('unexist.dat').length).toEqual(0);
- });
- it('only add file if in same current directory', function() {
- fileList.changeDirectory('/current dir', false, true);
- var fileData = {
- type: 'file',
- name: 'testFile.txt',
- directory: '/current dir'
- };
- fileList.add(fileData);
- expect(fileList.findFileEl('testFile.txt').length).toEqual(1);
- });
- it('triggers "fileActionsReady" event after update', function() {
- var handler = sinon.stub();
- fileList.$fileList.on('fileActionsReady', handler);
- fileList.setFiles(testFiles);
- expect(handler.calledOnce).toEqual(true);
- expect(handler.getCall(0).args[0].$files.length).toEqual(testFiles.length);
- });
- it('triggers "fileActionsReady" event after single add', function() {
- var handler = sinon.stub();
- var $tr;
- fileList.setFiles(testFiles);
- fileList.$fileList.on('fileActionsReady', handler);
- $tr = fileList.add({name: 'test.txt'});
- expect(handler.calledOnce).toEqual(true);
- expect(handler.getCall(0).args[0].$files.is($tr)).toEqual(true);
- });
- it('triggers "fileActionsReady" event after next page load with the newly appended files', function() {
- var handler = sinon.stub();
- fileList.setFiles(generateFiles(0, 64));
- fileList.$fileList.on('fileActionsReady', handler);
- fileList._nextPage();
- expect(handler.calledOnce).toEqual(true);
- expect(handler.getCall(0).args[0].$files.length).toEqual(fileList.pageSize());
- });
- it('does not trigger "fileActionsReady" event after single add with silent argument', function() {
- var handler = sinon.stub();
- fileList.setFiles(testFiles);
- fileList.$fileList.on('fileActionsReady', handler);
- fileList.add({name: 'test.txt'}, {silent: true});
- expect(handler.notCalled).toEqual(true);
- });
- it('triggers "updated" event after update', function() {
- var handler = sinon.stub();
- fileList.$fileList.on('updated', handler);
- fileList.setFiles(testFiles);
- expect(handler.calledOnce).toEqual(true);
- });
- it('does not update summary when removing non-existing files', function() {
- var $summary;
- // single file
- fileList.setFiles([testFiles[0]]);
- $summary = $('.files-filestable .summary');
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(true);
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- fileList.remove('unexist.txt');
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(true);
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- });
- });
- describe('Filtered list rendering', function() {
- it('filters the list of files using filter()', function() {
- expect(fileList.files.length).toEqual(0);
- expect(fileList.files).toEqual([]);
- fileList.setFiles(testFiles);
- var $summary = $('.files-filestable .summary');
- var $nofilterresults = fileList.$el.find(".nofilterresults");
- expect($nofilterresults.length).toEqual(1);
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(4);
- expect(fileList.files.length).toEqual(4);
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- fileList.setFilter('e');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(3);
- expect(fileList.files.length).toEqual(4);
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').text()).toEqual('1 folder');
- expect($summary.find('.fileinfo').text()).toEqual('2 files');
- expect($summary.find('.filter').text()).toEqual(" match \"e\"");
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- fileList.setFilter('ee');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(1);
- expect(fileList.files.length).toEqual(4);
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(true);
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- expect($summary.find('.filter').text()).toEqual(" matches \"ee\"");
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- fileList.setFilter('eee');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(0);
- expect(fileList.files.length).toEqual(4);
- expect($summary.hasClass('hidden')).toEqual(true);
- expect($nofilterresults.hasClass('hidden')).toEqual(false);
- fileList.setFilter('ee');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(1);
- expect(fileList.files.length).toEqual(4);
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(true);
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- expect($summary.find('.filter').text()).toEqual(" matches \"ee\"");
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- fileList.setFilter('e');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(3);
- expect(fileList.files.length).toEqual(4);
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').text()).toEqual('1 folder');
- expect($summary.find('.fileinfo').text()).toEqual('2 files');
- expect($summary.find('.filter').text()).toEqual(" match \"e\"");
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- fileList.setFilter('');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(4);
- expect(fileList.files.length).toEqual(4);
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').text()).toEqual('1 folder');
- expect($summary.find('.fileinfo').text()).toEqual('3 files');
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- });
- it('filters the list of non-rendered rows using filter()', function() {
- var $summary = $('.files-filestable .summary');
- var $nofilterresults = fileList.$el.find(".nofilterresults");
- fileList.setFiles(generateFiles(0, 64));
- fileList.setFilter('63');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(1);
- expect($summary.hasClass('hidden')).toEqual(false);
- expect($summary.find('.dirinfo').hasClass('hidden')).toEqual(true);
- expect($summary.find('.fileinfo').text()).toEqual('1 file');
- expect($summary.find('.filter').text()).toEqual(" matches \"63\"");
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- });
- it('hides the emptyfiles notice when using filter()', function() {
- expect(fileList.files.length).toEqual(0);
- expect(fileList.files).toEqual([]);
- fileList.setFiles([]);
- var $summary = $('.files-filestable .summary');
- var $emptycontent = fileList.$el.find(".emptyfilelist.emptycontent");
- var $nofilterresults = fileList.$el.find(".nofilterresults");
- expect($emptycontent.length).toEqual(1);
- expect($nofilterresults.length).toEqual(1);
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(0);
- expect(fileList.files.length).toEqual(0);
- expect($summary.hasClass('hidden')).toEqual(true);
- expect($emptycontent.hasClass('hidden')).toEqual(false);
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- fileList.setFilter('e');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(0);
- expect(fileList.files.length).toEqual(0);
- expect($summary.hasClass('hidden')).toEqual(true);
- expect($emptycontent.hasClass('hidden')).toEqual(true);
- expect($nofilterresults.hasClass('hidden')).toEqual(false);
- fileList.setFilter('');
- expect($('.files-fileList tr:not(.hidden)').length).toEqual(0);
- expect(fileList.files.length).toEqual(0);
- expect($summary.hasClass('hidden')).toEqual(true);
- expect($emptycontent.hasClass('hidden')).toEqual(false);
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- });
- it('does not show the emptyfiles or nofilterresults notice when the mask is active', function() {
- expect(fileList.files.length).toEqual(0);
- expect(fileList.files).toEqual([]);
- fileList.showMask();
- fileList.setFiles(testFiles);
- var $emptycontent = fileList.$el.find(".emptyfilelist.emptycontent");
- var $nofilterresults = fileList.$el.find(".nofilterresults");
- expect($emptycontent.length).toEqual(1);
- expect($nofilterresults.length).toEqual(1);
- expect($emptycontent.hasClass('hidden')).toEqual(true);
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- /*
- fileList.setFilter('e');
- expect($emptycontent.hasClass('hidden')).toEqual(true);
- expect($nofilterresults.hasClass('hidden')).toEqual(false);
- */
- fileList.setFilter('');
- expect($emptycontent.hasClass('hidden')).toEqual(true);
- expect($nofilterresults.hasClass('hidden')).toEqual(true);
- });
- });
- describe('Rendering next page on scroll', function() {
- beforeEach(function() {
- fileList.setFiles(generateFiles(0, 64));
- });
- it('renders only the first page', function() {
- expect(fileList.files.length).toEqual(65);
- expect($('.files-fileList tr').length).toEqual(20);
- });
- it('renders the full first page despite hidden rows', function() {
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: false });
- var files = _.map(generateFiles(0, 23), function(data) {
- return _.extend(data, {
- name: '.' + data.name
- });
- });
- // only hidden files + one visible
- files.push(testFiles[0]);
- fileList.setFiles(files);
- expect(fileList.files.length).toEqual(25);
- // render 24 hidden elements + the visible one
- expect($('.files-fileList tr').length).toEqual(25);
- });
- it('renders the full first page despite hidden rows', function() {
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: true });
- var files = _.map(generateFiles(0, 23), function(data) {
- return _.extend(data, {
- name: '.' + data.name
- });
- });
- // only hidden files + one visible
- files.push(testFiles[0]);
- fileList.setFiles(files);
- expect(fileList.files.length).toEqual(25);
- // render 20 first hidden elements as visible
- expect($('.files-fileList tr').length).toEqual(20);
- });
- it('renders the second page when scrolling down (trigger nextPage)', function() {
- // TODO: can't simulate scrolling here, so calling nextPage directly
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(40);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(60);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(65);
- fileList._nextPage(true);
- // stays at 65
- expect($('.files-fileList tr').length).toEqual(65);
- });
- it('inserts into the DOM if insertion point is in the visible page ', function() {
- fileList.add({
- id: 2000,
- type: 'file',
- name: 'File with index 15b.txt'
- });
- expect($('.files-fileList tr').length).toEqual(21);
- expect(fileList.findFileEl('File with index 15b.txt').index()).toEqual(16);
- });
- it('does not inserts into the DOM if insertion point is not the visible page ', function() {
- fileList.add({
- id: 2000,
- type: 'file',
- name: 'File with index 28b.txt'
- });
- expect($('.files-fileList tr').length).toEqual(20);
- expect(fileList.findFileEl('File with index 28b.txt').length).toEqual(0);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(40);
- expect(fileList.findFileEl('File with index 28b.txt').index()).toEqual(29);
- });
- it('appends into the DOM when inserting a file after the last visible element', function() {
- fileList.add({
- id: 2000,
- type: 'file',
- name: 'File with index 19b.txt'
- });
- expect($('.files-fileList tr').length).toEqual(21);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(41);
- });
- it('appends into the DOM when inserting a file on the last page when visible', function() {
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(40);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(60);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(65);
- fileList._nextPage(true);
- fileList.add({
- id: 2000,
- type: 'file',
- name: 'File with index 88.txt'
- });
- expect($('.files-fileList tr').length).toEqual(66);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(66);
- });
- it('shows additional page when appending a page of files and scrolling down', function() {
- var newFiles = generateFiles(66, 81);
- for (var i = 0; i < newFiles.length; i++) {
- fileList.add(newFiles[i]);
- }
- expect($('.files-fileList tr').length).toEqual(20);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(40);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(60);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(80);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(81);
- fileList._nextPage(true);
- expect($('.files-fileList tr').length).toEqual(81);
- });
- it('automatically renders next page when there are not enough elements visible', function() {
- // delete the 15 first elements
- for (var i = 0; i < 15; i++) {
- fileList.remove(fileList.files[0].name);
- }
- // still makes sure that there are 20 elements visible, if any
- expect($('.files-fileList tr').length).toEqual(25);
- });
- });
- describe('file previews', function() {
- var previewLoadStub;
- beforeEach(function() {
- previewLoadStub = sinon.stub(OCA.Files.FileList.prototype, 'lazyLoadPreview');
- });
- afterEach(function() {
- previewLoadStub.restore();
- });
- it('renders default file icon when none provided and no mime type is set', function() {
- var fileData = {
- name: 'testFile.txt'
- };
- var $tr = fileList.add(fileData);
- var $imgDiv = $tr.find('td.filename .thumbnail');
- expect(OC.TestUtil.getImageUrl($imgDiv)).toEqual(OC.getRootPath() + '/core/img/filetypes/file.svg');
- // tries to load preview
- expect(previewLoadStub.calledOnce).toEqual(true);
- });
- it('renders default icon for folder when none provided', function() {
- var fileData = {
- 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.getRootPath() + '/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 = new FileInfo({
- type: 'file',
- name: 'test file',
- icon: OC.getRootPath() + '/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.getRootPath() + '/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.getRootPath() + '/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.getRootPath() + '/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.getRootPath() + '/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.getRootPath() + '/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', function() {
- var fileData = {
- type: 'file',
- name: 'test file'
- };
- var $tr = fileList.add(fileData);
- var $td = $tr.find('td.filename');
- expect(OC.TestUtil.getImageUrl($td.find('.thumbnail')))
- .toEqual(OC.getRootPath() + '/core/img/filetypes/file.svg');
- expect(previewLoadStub.calledOnce).toEqual(true);
- // third argument is callback
- previewLoadStub.getCall(0).args[0].callback(OC.getRootPath() + '/somepath.png');
- expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))).toEqual(OC.getRootPath() + '/somepath.png');
- });
- it('does not render preview for directories', function() {
- var fileData = {
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- name: 'test dir'
- };
- var $tr = fileList.add(fileData);
- var $td = $tr.find('td.filename');
- expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))).toEqual(OC.getRootPath() + '/core/img/filetypes/folder.svg');
- expect(previewLoadStub.notCalled).toEqual(true);
- });
- it('render encrypted folder icon for encrypted root', function() {
- var fileData = {
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- name: 'test dir',
- isEncrypted: true
- };
- var $tr = fileList.add(fileData);
- var $td = $tr.find('td.filename');
- expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))).toEqual(OC.getRootPath() + '/core/img/filetypes/folder-encrypted.svg');
- expect(previewLoadStub.notCalled).toEqual(true);
- });
- it('render encrypted folder icon for encrypted subdir', function() {
- var fileData = {
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- name: 'test dir',
- isEncrypted: true
- };
- var $tr = fileList.add(fileData);
- var $td = $tr.find('td.filename');
- expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))).toEqual(OC.getRootPath() + '/core/img/filetypes/folder-encrypted.svg');
- expect(previewLoadStub.notCalled).toEqual(true);
- // default icon override
- expect($tr.attr('data-icon')).toEqual(OC.getRootPath() + '/core/img/filetypes/folder-encrypted.svg');
- });
- it('render external storage icon for external storage root', function() {
- var fileData = {
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- name: 'test dir',
- mountType: 'external-root'
- };
- var $tr = fileList.add(fileData);
- var $td = $tr.find('td.filename');
- expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))).toEqual(OC.getRootPath() + '/core/img/filetypes/folder-external.svg');
- expect(previewLoadStub.notCalled).toEqual(true);
- });
- it('render external storage icon for external storage subdir', function() {
- var fileData = {
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- name: 'test dir',
- mountType: 'external'
- };
- var $tr = fileList.add(fileData);
- var $td = $tr.find('td.filename');
- expect(OC.TestUtil.getImageUrl($td.find('.thumbnail'))).toEqual(OC.getRootPath() + '/core/img/filetypes/folder-external.svg');
- expect(previewLoadStub.notCalled).toEqual(true);
- // default icon override
- expect($tr.attr('data-icon')).toEqual(OC.getRootPath() + '/core/img/filetypes/folder-external.svg');
- });
- });
- describe('viewer mode', function() {
- it('enabling viewer mode hides files table and action buttons', function() {
- fileList.setViewerMode(true);
- expect($('.files-filestable').hasClass('hidden')).toEqual(true);
- expect($('.actions').hasClass('hidden')).toEqual(true);
- expect($('.notCreatable').hasClass('hidden')).toEqual(true);
- });
- it('disabling viewer mode restores files table and action buttons', function() {
- fileList.setViewerMode(true);
- fileList.setViewerMode(false);
- expect($('.files-filestable').hasClass('hidden')).toEqual(false);
- expect($('.actions').hasClass('hidden')).toEqual(false);
- expect($('.notCreatable').hasClass('hidden')).toEqual(true);
- });
- it('disabling viewer mode restores files table and action buttons with correct permissions', function() {
- $('#permissions').val(0);
- fileList.setViewerMode(true);
- fileList.setViewerMode(false);
- expect($('.files-filestable').hasClass('hidden')).toEqual(false);
- expect($('.actions').hasClass('hidden')).toEqual(true);
- expect($('.notCreatable').hasClass('hidden')).toEqual(false);
- });
- it('toggling viewer mode triggers event', function() {
- var handler = sinon.stub();
- fileList.$el.on('changeViewerMode', handler);
- fileList.setViewerMode(true);
- expect(handler.calledOnce).toEqual(true);
- expect(handler.getCall(0).args[0].viewerModeEnabled).toEqual(true);
- handler.reset();
- fileList.setViewerMode(false);
- expect(handler.calledOnce).toEqual(true);
- expect(handler.getCall(0).args[0].viewerModeEnabled).toEqual(false);
- });
- });
- describe('loading file list', function() {
- var deferredList;
- var getFolderContentsStub;
- beforeEach(function() {
- 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(done) {
- var reloading = fileList.reload();
- expect(getFolderContentsStub.calledOnce).toEqual(true);
- expect(getFolderContentsStub.calledWith('/subdir')).toEqual(true);
- deferredList.resolve(200, [testRoot].concat(testFiles));
- return reloading.then(function() {
- expect($('.files-fileList tr').length).toEqual(4);
- expect(fileList.findFileEl('One.txt').length).toEqual(1);
- }).then(done, done);
- });
- it('switches dir and fetches file list when calling changeDirectory()', function() {
- fileList.changeDirectory('/anothersubdir');
- expect(fileList.getCurrentDirectory()).toEqual('/anothersubdir');
- 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 is invalid', function() {
- _.each([
- '..',
- '/..',
- '../',
- '/../',
- '/../abc',
- '/abc/..',
- '/abc/../',
- '/../abc/',
- '/foo%0Abar/',
- '/foo%00bar/',
- '/another\\subdir/../foo\\../bar\\..\\file/..\\folder/../'
- ], function(path) {
- fileList.changeDirectory(decodeURI(path));
- expect(fileList.getCurrentDirectory()).toEqual('/');
- });
- });
- it('allows paths with dotdot at the beginning or end', function() {
- _.each([
- '/..abc',
- '/def..',
- '/...',
- '/abc../def'
- ], function(path) {
- fileList.changeDirectory(path);
- expect(fileList.getCurrentDirectory()).toEqual(path);
- });
- });
- it('switches to root dir when current directory does not exist', function(done) {
- var changing = fileList.changeDirectory('/unexist');
- deferredList.reject(404);
- return changing.then(function() {
- expect(fileList.getCurrentDirectory()).toEqual('/');
- }).then(done, done);
- });
- it('switches to root dir when current directory returns 400', function(done) {
- var changing = fileList.changeDirectory('/unexist');
- deferredList.reject(400);
- return changing.then(function() {
- expect(fileList.getCurrentDirectory()).toEqual('/');
- }).then(done, done);
- });
- it('switches to root dir when current directory returns 405', function(done) {
- var changing = fileList.changeDirectory('/unexist');
- deferredList.reject(405);
- return changing.then(function() {
- expect(fileList.getCurrentDirectory()).toEqual('/');
- }).then(done, done);
- });
- it('switches to root dir when current directory is forbidden', function(done) {
- var changing = fileList.changeDirectory('/unexist');
- deferredList.reject(403);
- return changing.then(function() {
- expect(fileList.getCurrentDirectory()).toEqual('/');
- }).then(done, done);
- });
- it('switches to root dir when current directory is unavailable', function(done) {
- var changing = fileList.changeDirectory('/unexist');
- deferredList.reject(500);
- return changing.then(function() {
- expect(fileList.getCurrentDirectory()).toEqual('/');
- }).then(done, done);
- });
- it('shows mask before loading file list then hides it at the end', function(done) {
- var showMaskStub = sinon.stub(fileList, 'showMask');
- var hideMaskStub = sinon.stub(fileList, 'hideMask');
- var changing = fileList.changeDirectory('/anothersubdir');
- expect(showMaskStub.calledOnce).toEqual(true);
- expect(hideMaskStub.calledOnce).toEqual(false);
- deferredList.resolve(200, [testRoot].concat(testFiles));
- return changing.then(function() {
- expect(showMaskStub.calledOnce).toEqual(true);
- expect(hideMaskStub.calledOnce).toEqual(true);
- showMaskStub.restore();
- hideMaskStub.restore();
- }).then(done, done);
- });
- it('triggers "changeDirectory" event when changing directory', function(done) {
- var handler = sinon.stub();
- $('#app-content-files').on('changeDirectory', handler);
- var changing = fileList.changeDirectory('/somedir');
- deferredList.resolve(200, [testRoot].concat(testFiles));
- return changing.then(function() {
- expect(handler.calledOnce).toEqual(true);
- expect(handler.getCall(0).args[0].dir).toEqual('/somedir');
- }).then(done, done);
- });
- it('triggers "afterChangeDirectory" event with fileid after changing directory', function(done) {
- var handler = sinon.stub();
- $('#app-content-files').on('afterChangeDirectory', handler);
- var changing = fileList.changeDirectory('/somedir');
- deferredList.resolve(200, [testRoot].concat(testFiles));
- return changing.then(function() {
- expect(handler.calledOnce).toEqual(true);
- expect(handler.getCall(0).args[0].dir).toEqual('/somedir');
- expect(handler.getCall(0).args[0].fileId).toEqual(99);
- }).then(done, done);
- });
- it('changes the directory when receiving "urlChanged" event', function() {
- $('#app-content-files').trigger(new $.Event('urlChanged', {view: 'files', dir: '/somedir'}));
- expect(fileList.getCurrentDirectory()).toEqual('/somedir');
- });
- it('refreshes breadcrumb after update', function() {
- var setDirSpy = sinon.spy(fileList.breadcrumb, 'setDirectory');
- fileList.changeDirectory('/anothersubdir');
- deferredList.resolve(200, [testRoot].concat(testFiles));
- // twice because setDirectory gets called by _setCurrentDir which
- // gets called directly by changeDirectory and via reload()
- expect(fileList.breadcrumb.setDirectory.calledTwice).toEqual(true);
- expect(fileList.breadcrumb.setDirectory.calledWith('/anothersubdir')).toEqual(true);
- setDirSpy.restore();
- getFolderContentsStub.restore();
- });
- it('prepends a slash to directory if none was given', function() {
- fileList.changeDirectory('');
- expect(fileList.getCurrentDirectory()).toEqual('/');
- fileList.changeDirectory('noslash');
- expect(fileList.getCurrentDirectory()).toEqual('/noslash');
- });
- });
- describe('breadcrumb events', function() {
- var deferredList;
- var getFolderContentsStub;
- beforeEach(function() {
- 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');
- deferredList.resolve(200, [testRoot].concat(testFiles));
- var changeDirStub = sinon.stub(fileList, 'changeDirectory');
- fileList.breadcrumb.$el.find('.crumb:eq(1) > a').trigger({type: 'click', which: 1});
- expect(changeDirStub.calledOnce).toEqual(true);
- expect(changeDirStub.getCall(0).args[0]).toEqual('/');
- changeDirStub.restore();
- });
- it('clicking on breadcrumb changes directory', function() {
- fileList.changeDirectory('/subdir/two/three with space/four/five');
- deferredList.resolve(200, [testRoot].concat(testFiles));
- var changeDirStub = sinon.stub(fileList, 'changeDirectory');
- fileList.breadcrumb.$el.find('.crumb:eq(4) > a').trigger({type: 'click', which: 1});
- expect(changeDirStub.calledOnce).toEqual(true);
- expect(changeDirStub.getCall(0).args[0]).toEqual('/subdir/two/three with space');
- changeDirStub.restore();
- });
- it('dropping files on breadcrumb calls move operation', function() {
- var testDir = '/subdir/two/three with space/four/five';
- var moveStub = sinon.stub(filesClient, 'move');
- var deferredMove1 = $.Deferred();
- var deferredMove2 = $.Deferred();
- moveStub.onCall(0).returns(deferredMove1.promise());
- moveStub.onCall(1).returns(deferredMove2.promise());
- fileList.changeDirectory(testDir);
- deferredList.resolve(200, [testRoot].concat(testFiles));
- var $crumb = fileList.breadcrumb.$el.find('.crumb:eq(4)');
- // no idea what this is but is required by the handler
- var ui = {
- helper: {
- find: sinon.stub()
- }
- };
- // returns a list of tr that were dragged
- ui.helper.find.returns([
- $('<tr data-file="One.txt" data-dir="' + testDir + '"></tr>'),
- $('<tr data-file="Two.jpg" data-dir="' + testDir + '"></tr>')
- ]);
- // simulate drop event
- var result = fileList._onDropOnBreadCrumb(new $.Event('drop', {target: $crumb}), ui).then(function(){
- 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();
- });
- deferredMove1.resolve(201);
- deferredMove2.resolve(201);
- return result;
- });
- 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);
- 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 = {
- helper: {
- find: sinon.stub()
- }
- };
- // returns a list of tr that were dragged
- ui.helper.find.returns([
- $('<tr data-file="One.txt" data-dir="' + testDir + '"></tr>'),
- $('<tr data-file="Two.jpg" data-dir="' + testDir + '"></tr>')
- ]);
- // simulate drop event
- fileList._onDropOnBreadCrumb(new $.Event('drop', {target: $crumb}), ui);
- // no extra server request
- 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.getRootPath() + '/remote.php/webdav/subdir/some%20file.txt');
- expect(fileList.getDownloadUrl('some file.txt', '/anotherpath/abc'))
- .toEqual(OC.getRootPath() + '/remote.php/webdav/anotherpath/abc/some%20file.txt');
- fileList.changeDirectory('/', false, true);
- expect(fileList.getDownloadUrl('some file.txt'))
- .toEqual(OC.getRootPath() + '/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.getRootPath() + '/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.getRootPath() + '/index.php/apps/files/ajax/test.php?a=1&b=x%20y');
- });
- });
- describe('File selection', function() {
- beforeEach(function() {
- fileList.setFiles(testFiles);
- });
- it('Selects a file when clicking its checkbox', function() {
- var $tr = fileList.findFileEl('One.txt');
- expect($tr.find('input:checkbox').prop('checked')).toEqual(false);
- $tr.find('td.selection input:checkbox').click();
- expect($tr.find('input:checkbox').prop('checked')).toEqual(true);
- });
- it('Selects a range when clicking on one file then Shift clicking on another one', function() {
- var $tr = fileList.findFileEl('One.txt');
- var $tr2 = fileList.findFileEl('Three.pdf');
- var e;
- $tr.find('td.selection input:checkbox').click();
- e = new $.Event('click');
- e.shiftKey = true;
- $tr2.find('td.filename .name').trigger(e);
- expect($tr.find('input:checkbox').prop('checked')).toEqual(true);
- expect($tr2.find('input:checkbox').prop('checked')).toEqual(true);
- expect(fileList.findFileEl('Two.jpg').find('input:checkbox').prop('checked')).toEqual(true);
- var selection = _.pluck(fileList.getSelectedFiles(), 'name');
- expect(selection.length).toEqual(3);
- expect(selection).toContain('One.txt');
- expect(selection).toContain('Two.jpg');
- expect(selection).toContain('Three.pdf');
- });
- it('Selects a range when clicking on one file then Shift clicking on another one that is above the first one', function() {
- var $tr = fileList.findFileEl('One.txt');
- var $tr2 = fileList.findFileEl('Three.pdf');
- var e;
- $tr2.find('td.selection input:checkbox').click();
- e = new $.Event('click');
- e.shiftKey = true;
- $tr.find('td.filename .name').trigger(e);
- expect($tr.find('input:checkbox').prop('checked')).toEqual(true);
- expect($tr2.find('input:checkbox').prop('checked')).toEqual(true);
- expect(fileList.findFileEl('Two.jpg').find('input:checkbox').prop('checked')).toEqual(true);
- var selection = _.pluck(fileList.getSelectedFiles(), 'name');
- expect(selection.length).toEqual(3);
- expect(selection).toContain('One.txt');
- expect(selection).toContain('Two.jpg');
- expect(selection).toContain('Three.pdf');
- });
- it('Selecting all files will automatically check "select all" checkbox', function() {
- expect($('.select-all').prop('checked')).toEqual(false);
- $('.files-fileList tr td.selection input:checkbox').click();
- expect($('.select-all').prop('checked')).toEqual(true);
- });
- it('Selecting all files on the first visible page will not automatically check "select all" checkbox', function() {
- fileList.setFiles(generateFiles(0, 41));
- expect($('.select-all').prop('checked')).toEqual(false);
- $('.files-fileList tr td.selection input:checkbox').click();
- expect($('.select-all').prop('checked')).toEqual(false);
- });
- it('Selecting all files also selects hidden files when invisible', function() {
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: false });
- var $tr = fileList.add(new FileInfo({
- name: '.hidden',
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- size: 150
- }));
- $('.select-all').click();
- expect($tr.find('td.selection input:checkbox').prop('checked')).toEqual(true);
- expect(_.pluck(fileList.getSelectedFiles(), 'name')).toContain('.hidden');
- });
- it('Clicking "select all" will select/deselect all files', function() {
- fileList.setFiles(generateFiles(0, 41));
- $('.select-all').click();
- expect($('.select-all').prop('checked')).toEqual(true);
- $('.files-fileList tr input:checkbox').each(function() {
- expect($(this).prop('checked')).toEqual(true);
- });
- expect(_.pluck(fileList.getSelectedFiles(), 'name').length).toEqual(42);
- $('.select-all').click();
- expect($('.select-all').prop('checked')).toEqual(false);
- $('.files-fileList tr input:checkbox').each(function() {
- expect($(this).prop('checked')).toEqual(false);
- });
- expect(_.pluck(fileList.getSelectedFiles(), 'name').length).toEqual(0);
- });
- it('Clicking "select all" then deselecting a file will uncheck "select all"', function() {
- $('.select-all').click();
- expect($('.select-all').prop('checked')).toEqual(true);
- var $tr = fileList.findFileEl('One.txt');
- $tr.find('input:checkbox').click();
- expect($('.select-all').prop('checked')).toEqual(false);
- expect(_.pluck(fileList.getSelectedFiles(), 'name').length).toEqual(3);
- });
- it('Updates the selection summary when doing a few manipulations with "Select all"', function() {
- $('.select-all').click();
- expect($('.select-all').prop('checked')).toEqual(true);
- var $tr = fileList.findFileEl('One.txt');
- // unselect one
- $tr.find('input:checkbox').click();
- expect($('.select-all').prop('checked')).toEqual(false);
- expect(_.pluck(fileList.getSelectedFiles(), 'name').length).toEqual(3);
- // select all
- $('.select-all').click();
- expect($('.select-all').prop('checked')).toEqual(true);
- expect(_.pluck(fileList.getSelectedFiles(), 'name').length).toEqual(4);
- // unselect one
- $tr.find('input:checkbox').click();
- expect($('.select-all').prop('checked')).toEqual(false);
- expect(_.pluck(fileList.getSelectedFiles(), 'name').length).toEqual(3);
- // re-select it
- $tr.find('input:checkbox').click();
- expect($('.select-all').prop('checked')).toEqual(true);
- expect(_.pluck(fileList.getSelectedFiles(), 'name').length).toEqual(4);
- });
- it('Auto-selects files on next page when "select all" is checked', function() {
- fileList.setFiles(generateFiles(0, 41));
- $('.select-all').click();
- expect(fileList.$fileList.find('tr input:checkbox:checked').length).toEqual(20);
- fileList._nextPage(true);
- expect(fileList.$fileList.find('tr input:checkbox:checked').length).toEqual(40);
- fileList._nextPage(true);
- expect(fileList.$fileList.find('tr input:checkbox:checked').length).toEqual(42);
- expect(_.pluck(fileList.getSelectedFiles(), 'name').length).toEqual(42);
- });
- it('Selecting files updates selection summary', function() {
- var $summary = $('.column-name a.name>span:first');
- expect($summary.text()).toEqual('Name');
- fileList.findFileEl('One.txt').find('input:checkbox').click();
- fileList.findFileEl('Three.pdf').find('input:checkbox').click();
- fileList.findFileEl('somedir').find('input:checkbox').click();
- expect($summary.text()).toEqual('1 folder and 2 files');
- });
- it('Unselecting files hides selection summary', function() {
- var $summary = $('.column-name a.name>span:first');
- fileList.findFileEl('One.txt').find('input:checkbox').click().click();
- expect($summary.text()).toEqual('Name');
- });
- it('Displays the number of hidden files in selection summary if hidden files are invisible', function() {
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: false });
- var $tr = fileList.add(new FileInfo({
- name: '.hidden',
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- size: 150
- }));
- $('.select-all').click();
- var $summary = $('.column-name a.name>span:first');
- expect($summary.text()).toEqual('2 folders and 3 files (including 1 hidden)');
- });
- it('Does not displays the number of hidden files in selection summary if hidden files are visible', function() {
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: true });
- var $tr = fileList.add(new FileInfo({
- name: '.hidden',
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- size: 150
- }));
- $('.select-all').click();
- var $summary = $('.column-name a.name>span:first');
- expect($summary.text()).toEqual('2 folders and 3 files');
- });
- it('Toggling hidden file visibility updates selection summary', function() {
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: false });
- var $tr = fileList.add(new FileInfo({
- name: '.hidden',
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- size: 150
- }));
- $('.select-all').click();
- var $summary = $('.column-name a.name>span:first');
- expect($summary.text()).toEqual('2 folders and 3 files (including 1 hidden)');
- window._nc_event_bus.emit('files:config:updated', { key: 'show_hidden', value: true });
- expect($summary.text()).toEqual('2 folders and 3 files');
- });
- it('Select/deselect files shows/hides file actions', function() {
- var $actions = $('.column-name .selectedActions');
- var $checkbox = fileList.findFileEl('One.txt').find('input:checkbox');
- expect($actions.hasClass('hidden')).toEqual(true);
- $checkbox.click();
- expect($actions.hasClass('hidden')).toEqual(false);
- $checkbox.click();
- expect($actions.hasClass('hidden')).toEqual(true);
- });
- it('Selection is cleared when switching dirs', function() {
- $('.select-all').click();
- var deferredList = $.Deferred();
- var getFolderContentsStub = sinon.stub(filesClient, 'getFolderContents').returns(deferredList.promise());
- fileList.changeDirectory('/');
- 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;
- fileList.setFiles(generateFiles(0, 41));
- $('.select-all').click();
- // unselect one to not have the "allFiles" case
- fileList.$fileList.find('tr input:checkbox:first').click();
- // only 20 files visible, must still return all the selected ones
- selectedFiles = _.pluck(fileList.getSelectedFiles(), 'name');
- expect(selectedFiles.length).toEqual(41);
- });
- describe('clearing the selection', function() {
- it('clears selected files selected individually calling setFiles()', function() {
- var selectedFiles;
- fileList.setFiles(generateFiles(0, 41));
- fileList.$fileList.find('tr:eq(5) input:checkbox:first').click();
- fileList.$fileList.find('tr:eq(7) input:checkbox:first').click();
- selectedFiles = _.pluck(fileList.getSelectedFiles(), 'name');
- expect(selectedFiles.length).toEqual(2);
- fileList.setFiles(generateFiles(0, 2));
- selectedFiles = _.pluck(fileList.getSelectedFiles(), 'name');
- expect(selectedFiles.length).toEqual(0);
- });
- it('clears selected files selected with select all when calling setFiles()', function() {
- var selectedFiles;
- fileList.setFiles(generateFiles(0, 41));
- $('.select-all').click();
- selectedFiles = _.pluck(fileList.getSelectedFiles(), 'name');
- expect(selectedFiles.length).toEqual(42);
- fileList.setFiles(generateFiles(0, 2));
- selectedFiles = _.pluck(fileList.getSelectedFiles(), 'name');
- expect(selectedFiles.length).toEqual(0);
- });
- });
- describe('Selection overlay', function() {
- it('show doesnt show the copy/move action if one or more files are not copiable/movable', function () {
- fileList.setFiles(testFiles);
- $('#permissions').val(OC.PERMISSION_READ | OC.PERMISSION_UPDATE);
- $('.select-all').click();
- expect(fileList.$el.find('.selectedActions .item-copyMove').hasClass('hidden')).toEqual(false);
- expect(fileList.$el.find('.selectedActions .item-copyMove .label').text()).toEqual('Move or copy');
- testFiles[0].permissions = OC.PERMISSION_READ;
- $('.select-all').click();
- fileList.setFiles(testFiles);
- $('.select-all').click();
- expect(fileList.$el.find('.selectedActions .item-copyMove').hasClass('hidden')).toEqual(false);
- expect(fileList.$el.find('.selectedActions .item-copyMove .label').text()).toEqual('Copy');
- testFiles[0].permissions = OC.PERMISSION_NONE;
- $('.select-all').click();
- fileList.setFiles(testFiles);
- $('.select-all').click();
- expect(fileList.$el.find('.selectedActions .item-copyMove').hasClass('hidden')).toEqual(true);
- });
- it('show doesnt show the download action if one or more files are not downloadable', function () {
- fileList.setFiles(testFiles);
- $('#permissions').val(OC.PERMISSION_READ | OC.PERMISSION_UPDATE);
- $('.select-all').click();
- expect(fileList.$el.find('.selectedActions .item-download').hasClass('hidden')).toEqual(false);
- testFiles[0].permissions = OC.PERMISSION_UPDATE;
- $('.select-all').click();
- fileList.setFiles(testFiles);
- $('.select-all').click();
- expect(fileList.$el.find('.selectedActions .item-download').hasClass('hidden')).toEqual(true);
- });
- it('show doesnt show the delete action if one or more files are not deletable', function () {
- fileList.setFiles(testFiles);
- $('#permissions').val(OC.PERMISSION_READ | OC.PERMISSION_DELETE);
- $('.select-all').click();
- expect(fileList.$el.find('.selectedActions .item-delete').hasClass('hidden')).toEqual(false);
- testFiles[0].permissions = OC.PERMISSION_READ;
- $('.select-all').click();
- fileList.setFiles(testFiles);
- $('.select-all').click();
- expect(fileList.$el.find('.selectedActions .item-delete').hasClass('hidden')).toEqual(true);
- });
- });
- describe('Actions', function() {
- beforeEach(function() {
- fileList.findFileEl('One.txt').find('input:checkbox').click();
- fileList.findFileEl('Three.pdf').find('input:checkbox').click();
- fileList.findFileEl('somedir').find('input:checkbox').click();
- });
- it('getSelectedFiles returns the selected file data', function() {
- var files = fileList.getSelectedFiles();
- expect(files.length).toEqual(3);
- expect(files[0]).toEqual({
- id: 1,
- name: 'One.txt',
- mimetype: 'text/plain',
- mtime: 123456789,
- type: 'file',
- size: 12,
- etag: 'abc',
- quotaAvailableBytes: '-1',
- permissions: OC.PERMISSION_ALL,
- hasPreview: true,
- isEncrypted: false
- });
- expect(files[1]).toEqual({
- id: 3,
- type: 'file',
- name: 'Three.pdf',
- mimetype: 'application/pdf',
- mtime: 234560000,
- size: 58009,
- quotaAvailableBytes: '-1',
- etag: '123',
- permissions: OC.PERMISSION_ALL,
- hasPreview: true,
- isEncrypted: false
- });
- expect(files[2]).toEqual({
- id: 4,
- type: 'dir',
- name: 'somedir',
- mimetype: 'httpd/unix-directory',
- mtime: 134560000,
- size: 250,
- quotaAvailableBytes: '-1',
- etag: '456',
- permissions: OC.PERMISSION_ALL,
- hasPreview: true,
- isEncrypted: false
- });
- 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');
- var files = fileList.getSelectedFiles();
- expect(files.length).toEqual(2);
- expect(files[0]).toEqual({
- id: 1,
- name: 'One.txt',
- mimetype: 'text/plain',
- mtime: 123456789,
- type: 'file',
- size: 12,
- quotaAvailableBytes: '-1',
- etag: 'abc',
- permissions: OC.PERMISSION_ALL,
- hasPreview: true,
- isEncrypted: false
- });
- expect(files[1]).toEqual({
- id: 4,
- type: 'dir',
- name: 'somedir',
- mimetype: 'httpd/unix-directory',
- mtime: 134560000,
- size: 250,
- quotaAvailableBytes: '-1',
- etag: '456',
- permissions: OC.PERMISSION_ALL,
- hasPreview: true,
- isEncrypted: false
- });
- });
- describe('Download', function() {
- beforeEach(function() {
- fileList.$el.find('.actions-selected').click();
- });
- it('Opens download URL when clicking "Download"', function() {
- $('.selectedActions .filesSelectMenu .download').click();
- expect(redirectStub.calledOnce).toEqual(true);
- expect(redirectStub.getCall(0).args[0]).toContain(OC.getRootPath() + '/index.php/apps/files/ajax/download.php?dir=%2Fsubdir&files=%5B%22One.txt%22%2C%22Three.pdf%22%2C%22somedir%22%5D');
- redirectStub.restore();
- });
- it('Downloads root folder when all selected in root folder', function() {
- fileList.changeDirectory('/', false, true);
- $('.select-all').click();
- $('.selectedActions .filesSelectMenu .download').click();
- expect(redirectStub.calledOnce).toEqual(true);
- expect(redirectStub.getCall(0).args[0]).toContain(OC.getRootPath() + '/index.php/apps/files/ajax/download.php?dir=%2F&files=');
- });
- it('Downloads parent folder when all selected in subfolder', function() {
- $('.select-all').click();
- $('.selectedActions .filesSelectMenu .download').click();
- expect(redirectStub.calledOnce).toEqual(true);
- expect(redirectStub.getCall(0).args[0]).toContain(OC.getRootPath() + '/index.php/apps/files/ajax/download.php?dir=%2F&files=subdir');
- });
- afterEach(function() {
- fileList.$el.find('.actions-selected').click();
- });
- });
- describe('Delete', function() {
- var deleteStub, deferredDelete;
- beforeEach(function() {
- deferredDelete = $.Deferred();
- deleteStub = sinon.stub(filesClient, 'remove');
- fileList.$el.find('.actions-selected').click();
- });
- afterEach(function() {
- fileList.$el.find('.actions-selected').click();
- deleteStub.restore();
- });
- it('Deletes selected files when "Delete" clicked', function(done) {
- var deferred = $.Deferred();
- deleteStub.returns(deferredDelete.promise());
- deleteStub.onCall(2).callsFake(function(){
- 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');
- return deferredDelete.promise();
- });
- stub = sinon.stub(fileList._operationProgressBar, 'hideProgressBar').callsFake(function(){
- 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);
- done();
- deferred.resolve();
- });
- $('.selectedActions .filesSelectMenu .delete').click();
- deferredDelete.resolve(204);
- return deferred.promise();
- });
- it('Deletes all files when all selected when "Delete" clicked', function(done) {
- var deferred = $.Deferred();
- deleteStub.returns(deferredDelete.promise());
- deleteStub.onCall(3).callsFake(function(){
- 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');
- return deferredDelete.promise();
- });
- stub = sinon.stub(fileList._operationProgressBar, 'hideProgressBar').callsFake(function(){
- expect(fileList.isEmpty).toEqual(true);
- expect(deleteStub.callCount).toEqual(4);
- done();
- deferred.resolve();
- });
- $('.select-all').click();
- $('.selectedActions .filesSelectMenu .delete').click();
- deferredDelete.resolve(204);
- return deferred.promise();
- });
- });
- });
- it('resets the file selection on reload', function() {
- fileList.$el.find('.select-all').click();
- fileList.reload();
- expect(fileList.$el.find('.select-all').prop('checked')).toEqual(false);
- expect(fileList.getSelectedFiles()).toEqual([]);
- });
- describe('Disabled selection', function() {
- beforeEach(function() {
- fileList._allowSelection = false;
- fileList.setFiles(testFiles);
- });
- it('Does not render checkboxes', function() {
- expect(fileList.$fileList.find('.selectCheckBox').length).toEqual(0);
- });
- it('Does not select a file with Ctrl or Shift if selection is not allowed', function() {
- var $tr = fileList.findFileEl('One.txt');
- var $tr2 = fileList.findFileEl('Three.pdf');
- var e;
- e = new $.Event('click');
- e.ctrlKey = true;
- $tr.find('td.filename .name').trigger(e);
- // click on second entry, does not clear the selection
- e = new $.Event('click');
- e.ctrlKey = true;
- $tr2.find('td.filename .name').trigger(e);
- expect(fileList.getSelectedFiles().length).toEqual(0);
- // deselect now
- e = new $.Event('click');
- e.shiftKey = true;
- $tr2.find('td.filename .name').trigger(e);
- expect(fileList.getSelectedFiles().length).toEqual(0);
- });
- });
- });
- describe('File actions', function() {
- it('Clicking on a file name will trigger default action', function() {
- var actionStub = sinon.stub();
- fileList.setFiles(testFiles);
- fileList.fileActions.registerAction({
- mime: 'text/plain',
- name: 'Test',
- type: OCA.Files.FileActions.TYPE_INLINE,
- permissions: OC.PERMISSION_ALL,
- icon: function() {
- // Specify icon for history button
- return OC.imagePath('core','actions/history');
- },
- actionHandler: actionStub
- });
- fileList.fileActions.setDefault('text/plain', 'Test');
- var $tr = fileList.findFileEl('One.txt');
- $tr.find('td.filename .nametext').click();
- expect(actionStub.calledOnce).toEqual(true);
- expect(actionStub.getCall(0).args[0]).toEqual('One.txt');
- var context = actionStub.getCall(0).args[1];
- expect(context.$file.is($tr)).toEqual(true);
- expect(context.fileList).toBeDefined();
- expect(context.fileActions).toBeDefined();
- expect(context.dir).toEqual('/subdir');
- });
- it('Clicking on an empty space of the file row will trigger the "Details" action', function() {
- var detailsActionStub = sinon.stub();
- fileList.setFiles(testFiles);
- // Override the "Details" action set internally by the FileList for
- // easier testing.
- fileList.fileActions.registerAction({
- mime: 'all',
- name: 'Details',
- permissions: OC.PERMISSION_NONE,
- actionHandler: detailsActionStub
- });
- // Ensure that the action works even if fileActions.currentFile is
- // not set.
- fileList.fileActions.currentFile = null;
- var $tr = fileList.findFileEl('One.txt');
- $tr.find('td.filesize').click();
- expect(detailsActionStub.calledOnce).toEqual(true);
- expect(detailsActionStub.getCall(0).args[0]).toEqual('One.txt');
- var context = detailsActionStub.getCall(0).args[1];
- expect(context.$file.is($tr)).toEqual(true);
- expect(context.fileList).toBe(fileList);
- expect(context.fileActions).toBe(fileList.fileActions);
- expect(context.dir).toEqual('/subdir');
- });
- it('redisplays actions when new actions have been registered', function() {
- var actionStub = sinon.stub();
- var readyHandler = sinon.stub();
- var clock = sinon.useFakeTimers();
- var debounceStub = sinon.stub(_, 'debounce').callsFake(function(callback) {
- return function() {
- // defer instead of debounce, to make it work with clock
- _.defer(callback);
- };
- });
- // need to reinit the list to make the debounce call
- fileList.destroy();
- fileList = new OCA.Files.FileList($('#app-content-files'));
- fileList.setFiles(testFiles);
- fileList.$fileList.on('fileActionsReady', readyHandler);
- fileList.fileActions.registerAction({
- mime: 'text/plain',
- name: 'Test',
- type: OCA.Files.FileActions.TYPE_INLINE,
- permissions: OC.PERMISSION_ALL,
- icon: function() {
- // Specify icon for history button
- return OC.imagePath('core','actions/history');
- },
- actionHandler: actionStub
- });
- var $tr = fileList.findFileEl('One.txt');
- expect($tr.find('.action-test').length).toEqual(0);
- expect(readyHandler.notCalled).toEqual(true);
- // update is delayed
- clock.tick(100);
- expect($tr.find('.action-test').length).toEqual(1);
- expect(readyHandler.calledOnce).toEqual(true);
- clock.restore();
- debounceStub.restore();
- });
- });
- describe('Sorting files', function() {
- var getCurrentUserStub;
- beforeEach(function() {
- getCurrentUserStub = sinon.stub(OC, 'getCurrentUser').returns({
- uid: 1,
- displayName: 'user1'
- });
- });
- afterEach(function() {
- getCurrentUserStub.restore();
- });
- 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;
- var request;
- var sortingUrl = OC.generateUrl('/apps/files/api/v1/sorting');
- fileList.$el.find('.column-size .columntitle').click();
- // moves triangle to size column, check indicator on name is hidden
- expect(
- fileList.$el.find('.column-name .sort-indicator').hasClass('hidden')
- ).toEqual(true);
- // check indicator on size is visible and defaults to descending
- expect(
- fileList.$el.find('.column-size .sort-indicator').hasClass('hidden')
- ).toEqual(false);
- expect(
- fileList.$el.find('.column-size .sort-indicator').hasClass(DESC_CLASS)
- ).toEqual(true);
- // check if changes are persisted
- expect(fakeServer.requests.length).toEqual(1);
- request = fakeServer.requests[0];
- expect(request.url).toEqual(sortingUrl);
- // click again on size column, reverses direction
- fileList.$el.find('.column-size .columntitle').click();
- expect(
- fileList.$el.find('.column-size .sort-indicator').hasClass('hidden')
- ).toEqual(false);
- expect(
- fileList.$el.find('.column-size .sort-indicator').hasClass(ASC_CLASS)
- ).toEqual(true);
- // check if changes are persisted
- expect(fakeServer.requests.length).toEqual(2);
- request = fakeServer.requests[1];
- expect(request.url).toEqual(sortingUrl);
- // click again on size column, reverses direction
- fileList.$el.find('.column-size .columntitle').click();
- expect(
- fileList.$el.find('.column-size .sort-indicator').hasClass('hidden')
- ).toEqual(false);
- expect(
- fileList.$el.find('.column-size .sort-indicator').hasClass(DESC_CLASS)
- ).toEqual(true);
- expect(fakeServer.requests.length).toEqual(3);
- request = fakeServer.requests[2];
- expect(request.url).toEqual(sortingUrl);
- // click on mtime column, moves indicator there
- fileList.$el.find('.column-mtime .columntitle').click();
- expect(
- fileList.$el.find('.column-size .sort-indicator').hasClass('hidden')
- ).toEqual(true);
- expect(
- fileList.$el.find('.column-mtime .sort-indicator').hasClass('hidden')
- ).toEqual(false);
- expect(
- fileList.$el.find('.column-mtime .sort-indicator').hasClass(DESC_CLASS)
- ).toEqual(true);
- expect(fakeServer.requests.length).toEqual(4);
- request = fakeServer.requests[3];
- expect(request.url).toEqual(sortingUrl);
- });
- it('Uses correct sort comparator when inserting files', function() {
- testFiles.sort(OCA.Files.FileList.Comparators.size);
- testFiles.reverse(); //default is descending
- fileList.setFiles(testFiles);
- fileList.$el.find('.column-size .columntitle').click();
- var newFileData = new FileInfo({
- id: 999,
- 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);
- expect(fileList.findFileEl('Two.jpg').index()).toEqual(2);
- expect(fileList.findFileEl('somedir').index()).toEqual(3);
- expect(fileList.findFileEl('One.txt').index()).toEqual(4);
- expect(fileList.files.length).toEqual(5);
- expect(fileList.$fileList.find('tr').length).toEqual(5);
- });
- it('Uses correct reversed sort comparator when inserting files', function() {
- testFiles.sort(OCA.Files.FileList.Comparators.size);
- fileList.setFiles(testFiles);
- fileList.$el.find('.column-size .columntitle').click();
- // reverse sort
- fileList.$el.find('.column-size .columntitle').click();
- var newFileData = new FileInfo({
- id: 999,
- 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);
- expect(fileList.findFileEl('Two.jpg').index()).toEqual(2);
- expect(fileList.findFileEl('new file.txt').index()).toEqual(3);
- expect(fileList.findFileEl('Three.pdf').index()).toEqual(4);
- expect(fileList.files.length).toEqual(5);
- expect(fileList.$fileList.find('tr').length).toEqual(5);
- });
- it('does not sort when clicking on header whenever multiselect is enabled', function() {
- var sortStub = sinon.stub(OCA.Files.FileList.prototype, 'setSort');
- fileList.setFiles(testFiles);
- fileList.findFileEl('One.txt').find('input:checkbox:first').click();
- fileList.$el.find('.column-size .columntitle').click();
- expect(sortStub.notCalled).toEqual(true);
- // can sort again after deselecting
- fileList.findFileEl('One.txt').find('input:checkbox:first').click();
- fileList.$el.find('.column-size .columntitle').click();
- expect(sortStub.calledOnce).toEqual(true);
- sortStub.restore();
- });
- describe('if no user logged in', function() {
- beforeEach(function() {
- getCurrentUserStub.returns({
- uid: null,
- displayName: 'Guest'
- });
- });
- it('shouldn\'t send an update sort order request', function() {
- OC.currentUser = false;
- fileList.$el.find('.column-size .columntitle').click();
- // check if there was no request
- expect(fakeServer.requests.length).toEqual(0);
- });
- });
- describe('with favorites', function() {
- it('shows favorite files on top', function() {
- testFiles.push(new FileInfo({
- id: 5,
- type: 'file',
- name: 'ZZY Before last file in ascending order',
- mimetype: 'text/plain',
- mtime: 999999998,
- size: 9999998,
- // Tags would be added by TagsPlugin
- tags: [OC.TAG_FAVORITE],
- }), new FileInfo({
- id: 6,
- type: 'file',
- name: 'ZZZ Last file in ascending order',
- mimetype: 'text/plain',
- mtime: 999999999,
- size: 9999999,
- // Tags would be added by TagsPlugin
- tags: [OC.TAG_FAVORITE],
- }));
- fileList.setFiles(testFiles);
- // Sort by name in ascending order (default sorting is by name
- // in ascending order, but setFiles does not trigger a sort, so
- // the files must be sorted before being set or a sort must be
- // triggered afterwards by clicking on the header).
- fileList.$el.find('.column-name .columntitle').click();
- fileList.$el.find('.column-name .columntitle').click();
- expect(fileList.findFileEl('ZZY Before last file in ascending order').index()).toEqual(0);
- expect(fileList.findFileEl('ZZZ Last file in ascending order').index()).toEqual(1);
- expect(fileList.findFileEl('somedir').index()).toEqual(2);
- expect(fileList.findFileEl('One.txt').index()).toEqual(3);
- expect(fileList.findFileEl('Three.pdf').index()).toEqual(4);
- expect(fileList.findFileEl('Two.jpg').index()).toEqual(5);
- // Sort by size in ascending order
- fileList.$el.find('.column-size .columntitle').click();
- fileList.$el.find('.column-size .columntitle').click();
- expect(fileList.findFileEl('ZZY Before last file in ascending order').index()).toEqual(0);
- expect(fileList.findFileEl('ZZZ Last file in ascending order').index()).toEqual(1);
- expect(fileList.findFileEl('One.txt').index()).toEqual(2);
- expect(fileList.findFileEl('somedir').index()).toEqual(3);
- expect(fileList.findFileEl('Two.jpg').index()).toEqual(4);
- expect(fileList.findFileEl('Three.pdf').index()).toEqual(5);
- // Sort by modification time in ascending order
- fileList.$el.find('.column-mtime .columntitle').click();
- fileList.$el.find('.column-mtime .columntitle').click();
- expect(fileList.findFileEl('ZZY Before last file in ascending order').index()).toEqual(0);
- expect(fileList.findFileEl('ZZZ Last file in ascending order').index()).toEqual(1);
- expect(fileList.findFileEl('One.txt').index()).toEqual(2);
- expect(fileList.findFileEl('somedir').index()).toEqual(3);
- expect(fileList.findFileEl('Three.pdf').index()).toEqual(4);
- expect(fileList.findFileEl('Two.jpg').index()).toEqual(5);
- });
- it('shows favorite files on top also when using descending order', function() {
- testFiles.push(new FileInfo({
- id: 5,
- type: 'file',
- name: 'AAB Before last file in descending order',
- mimetype: 'text/plain',
- mtime: 2,
- size: 2,
- // Tags would be added by TagsPlugin
- tags: [OC.TAG_FAVORITE],
- }), new FileInfo({
- id: 6,
- type: 'file',
- name: 'AAA Last file in descending order',
- mimetype: 'text/plain',
- mtime: 1,
- size: 1,
- // Tags would be added by TagsPlugin
- tags: [OC.TAG_FAVORITE],
- }));
- fileList.setFiles(testFiles);
- // Sort by name in descending order
- fileList.$el.find('.column-name .columntitle').click();
- expect(fileList.findFileEl('AAB Before last file in descending order').index()).toEqual(0);
- expect(fileList.findFileEl('AAA Last file in descending order').index()).toEqual(1);
- expect(fileList.findFileEl('Two.jpg').index()).toEqual(2);
- expect(fileList.findFileEl('Three.pdf').index()).toEqual(3);
- expect(fileList.findFileEl('One.txt').index()).toEqual(4);
- expect(fileList.findFileEl('somedir').index()).toEqual(5);
- // Sort by size in descending order
- fileList.$el.find('.column-size .columntitle').click();
- expect(fileList.findFileEl('AAB Before last file in descending order').index()).toEqual(0);
- expect(fileList.findFileEl('AAA Last file in descending order').index()).toEqual(1);
- expect(fileList.findFileEl('Three.pdf').index()).toEqual(2);
- expect(fileList.findFileEl('Two.jpg').index()).toEqual(3);
- expect(fileList.findFileEl('somedir').index()).toEqual(4);
- expect(fileList.findFileEl('One.txt').index()).toEqual(5);
- // Sort by modification time in descending order
- fileList.$el.find('.column-mtime .columntitle').click();
- expect(fileList.findFileEl('AAB Before last file in descending order').index()).toEqual(0);
- expect(fileList.findFileEl('AAA Last file in descending order').index()).toEqual(1);
- expect(fileList.findFileEl('Two.jpg').index()).toEqual(2);
- expect(fileList.findFileEl('Three.pdf').index()).toEqual(3);
- expect(fileList.findFileEl('somedir').index()).toEqual(4);
- expect(fileList.findFileEl('One.txt').index()).toEqual(5);
- });
- });
- });
- 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(done) {
- var creating = fileList.createFile('test.txt');
- 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
- });
- deferredCreate.resolve(200);
- expect(getFileInfoStub.calledOnce).toEqual(true);
- expect(getFileInfoStub.getCall(0).args[0]).toEqual('/subdir/test.txt');
- deferredInfo.resolve(
- 200,
- new FileInfo({
- path: '/subdir',
- name: 'test.txt',
- mimetype: 'text/plain'
- })
- );
- return creating.then(function() {
- var $tr = fileList.findFileEl('test.txt');
- expect($tr.length).toEqual(1);
- expect($tr.attr('data-mime')).toEqual('text/plain');
- }).then(done, done);
- });
- // TODO: error cases
- // TODO: unique name cases
- });
- 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(done) {
- var creating = fileList.createDirectory('sub dir');
- expect(createStub.calledOnce).toEqual(true);
- expect(createStub.getCall(0).args[0]).toEqual('/subdir/sub dir');
- deferredCreate.resolve(200);
- expect(getFileInfoStub.calledOnce).toEqual(true);
- expect(getFileInfoStub.getCall(0).args[0]).toEqual('/subdir/sub dir');
- deferredInfo.resolve(
- 200,
- new FileInfo({
- path: '/subdir',
- name: 'sub dir',
- mimetype: 'httpd/unix-directory'
- })
- );
- return creating.then(function() {
- var $tr = fileList.findFileEl('sub dir');
- expect($tr.length).toEqual(1);
- expect($tr.attr('data-mime')).toEqual('httpd/unix-directory');
- }).then(done, done);
- });
- // TODO: error cases
- // TODO: unique name cases
- });
- describe('addAndFetchFileInfo', function() {
- var getFileInfoStub;
- var getFileInfoDeferred;
- beforeEach(function() {
- getFileInfoDeferred = $.Deferred();
- getFileInfoStub = sinon.stub(OC.Files.Client.prototype, 'getFileInfo');
- getFileInfoStub.returns(getFileInfoDeferred.promise());
- });
- afterEach(function() {
- getFileInfoStub.restore();
- });
- it('does not fetch if the given folder is not the current one', function() {
- var promise = fileList.addAndFetchFileInfo('testfile.txt', '/another');
- expect(getFileInfoStub.notCalled).toEqual(true);
- expect(promise.state()).toEqual('resolved');
- });
- it('fetches info when folder is the current one', function(done) {
- fileList.addAndFetchFileInfo('testfile.txt', '/subdir');
- return Promise.resolve().then(function() {
- expect(getFileInfoStub.calledOnce).toEqual(true);
- expect(getFileInfoStub.getCall(0).args[0]).toEqual('/subdir/testfile.txt');
- }).then(done, done);
- });
- it('adds file data to list when fetching is done', function(done) {
- var adding = fileList.addAndFetchFileInfo('testfile.txt', '/subdir');
- getFileInfoDeferred.resolve(200, {
- name: 'testfile.txt',
- size: 100
- });
- return adding.then(function() {
- expect(fileList.findFileEl('testfile.txt').attr('data-size')).toEqual('100');
- }).then(done, done);
- });
- it('replaces file data to list when fetching is done', function(done) {
- var adding = fileList.addAndFetchFileInfo('testfile.txt', '/subdir', {replace: true});
- fileList.add({
- name: 'testfile.txt',
- size: 95
- });
- getFileInfoDeferred.resolve(200, {
- name: 'testfile.txt',
- size: 100
- });
- expect(fileList.findFileEl('testfile.txt').attr('data-size')).toEqual('95');
- return adding.then(function() {
- expect(fileList.findFileEl('testfile.txt').attr('data-size')).toEqual('100');
- }).then(done, done);
- });
- it('resolves promise with file data when fetching is done', function(done) {
- var promise = fileList.addAndFetchFileInfo('testfile.txt', '/subdir', {replace: true});
- getFileInfoDeferred.resolve(200, {
- name: 'testfile.txt',
- size: 100
- });
- expect(promise.state()).toEqual('pending');
- return promise.then(function(status, data) {
- expect(promise.state()).toEqual('resolved');
- expect(status).toEqual(200);
- expect(data.name).toEqual('testfile.txt');
- expect(data.size).toEqual(100);
- }).then(done, done);
- });
- });
- /**
- * Test upload mostly by testing the code inside the event handlers
- * that were registered on the magic upload object
- */
- describe('file upload', function() {
- var uploadData;
- var uploader;
- beforeEach(function() {
- fileList.setFiles(testFiles);
- uploader = fileList._uploader;
- // simulate data structure from jquery.upload
- uploadData = {
- files: [{
- name: 'upload.txt'
- }]
- };
- });
- afterEach(function() {
- uploader = null;
- uploadData = null;
- });
- describe('enableupload', function() {
- it('sets up uploader when enableUpload is true', function() {
- expect(fileList._uploader).toBeDefined();
- });
- it('does not sets up uploader when enableUpload is false', function() {
- fileList.destroy();
- fileList = new OCA.Files.FileList($('#app-content-files'), {
- filesClient: filesClient
- });
- expect(fileList._uploader).toBeFalsy();
- });
- });
- describe('adding files for upload', function() {
- /**
- * Simulate add event on the given target
- *
- * @return event object including the result
- */
- function addFile(data) {
- uploader.trigger('add', {}, data || {});
- }
- it('sets target dir to the current directory', function() {
- addFile(uploadData);
- expect(uploadData.targetDir).toEqual('/subdir');
- });
- });
- describe('dropping external files', function() {
- /**
- * Simulate drop event on the given target
- *
- * @param $target target element to drop on
- * @return event object including the result
- */
- function dropOn($target, data) {
- var eventData = {
- delegatedEvent: {
- target: $target
- },
- preventDefault: function () {
- },
- stopPropagation: function() {
- }
- };
- uploader.trigger('drop', eventData, data || {});
- return !!data.targetDir;
- }
- it('drop on a tr or crumb outside file list does not trigger upload', function() {
- var $anotherTable = $('<table><tbody><tr><td>outside<div class="crumb">crumb</div></td></tr></table>');
- var ev;
- $('#testArea').append($anotherTable);
- ev = dropOn($anotherTable.find('tr'), uploadData);
- expect(ev).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 = $('<div>outside</div>');
- var ev;
- $('#testArea').append($anotherEl);
- ev = dropOn($anotherEl, uploadData);
- 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).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).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'), uploadData);
- 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, uploadData);
- 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).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).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;
- fileList.changeDirectory('a/b/c/d');
- ev = dropOn(fileList.$el.find('.crumb:eq(3)'), uploadData);
- expect(ev).not.toEqual(false);
- expect(uploadData.targetDir).toEqual('/a/b');
- });
- it('renders upload indicator element for folders only', function() {
- fileList.add({
- name: 'afolder',
- type: 'dir',
- mime: 'httpd/unix-directory'
- });
- fileList.add({
- name: 'afile.txt',
- type: 'file',
- mime: 'text/plain'
- });
- expect(fileList.findFileEl('afolder').find('.uploadtext').length).toEqual(1);
- expect(fileList.findFileEl('afile.txt').find('.uploadtext').length).toEqual(0);
- });
- });
- describe('after folder creation due to folder upload', function() {
- it('fetches folder info', function() {
- var fetchInfoStub = sinon.stub(fileList, 'addAndFetchFileInfo');
- uploader.trigger('createdfolder', '/subdir/newfolder');
- expect(fetchInfoStub.calledOnce).toEqual(true);
- expect(fetchInfoStub.getCall(0).args[0]).toEqual('newfolder');
- expect(fetchInfoStub.getCall(0).args[1]).toEqual('/subdir');
- fetchInfoStub.restore();
- });
- });
- describe('after upload', function() {
- var fetchInfoStub;
- beforeEach(function() {
- fetchInfoStub = sinon.stub(fileList, 'addAndFetchFileInfo');
- });
- afterEach(function() {
- fetchInfoStub.restore();
- });
- function createUpload(name, dir) {
- var jqXHR = {
- status: 200
- };
- return {
- getFileName: sinon.stub().returns(name),
- getFullPath: sinon.stub().returns(dir),
- data: {
- jqXHR: jqXHR
- }
- };
- }
- /**
- * Simulate add event on the given target
- *
- * @return event object including the result
- */
- function addFile(data) {
- var ev = new $.Event('done', {
- jqXHR: {status: 200}
- });
- var deferred = $.Deferred();
- fetchInfoStub.returns(deferred.promise());
- uploader.trigger('done', ev, data || {});
- return deferred;
- }
- it('fetches file info', function() {
- addFile(createUpload('upload.txt', '/subdir'));
- expect(fetchInfoStub.calledOnce).toEqual(true);
- expect(fetchInfoStub.getCall(0).args[0]).toEqual('upload.txt');
- expect(fetchInfoStub.getCall(0).args[1]).toEqual('/subdir');
- });
- it('highlights all uploaded files after all fetches are done', function(done) {
- var highlightStub = sinon.stub(fileList, 'highlightFiles');
- var def1 = addFile(createUpload('upload.txt', '/subdir'));
- var def2 = addFile(createUpload('upload2.txt', '/subdir'));
- var def3 = addFile(createUpload('upload3.txt', '/another'));
- uploader.trigger('stop', {});
- expect(highlightStub.notCalled).toEqual(true);
- def1.resolve();
- expect(highlightStub.notCalled).toEqual(true);
- def2.resolve();
- def3.resolve();
- setTimeout(function() {
- expect(highlightStub.callCount).toEqual(1);
- expect(highlightStub.getCall(0).args[0]).toEqual(['upload.txt', 'upload2.txt']);
- highlightStub.restore();
- done();
- }, 5);
- });
- it('queries storage stats after all fetches are done', function(done) {
- var statStub = sinon.stub(fileList, 'updateStorageStatistics');
- var highlightStub = sinon.stub(fileList, 'highlightFiles');
- var def1 = addFile(createUpload('upload.txt', '/subdir'));
- var def2 = addFile(createUpload('upload2.txt', '/subdir'));
- var def3 = addFile(createUpload('upload3.txt', '/another'));
- uploader.trigger('stop', {});
- expect(statStub.notCalled).toEqual(true);
- def1.resolve();
- expect(statStub.notCalled).toEqual(true);
- def2.resolve();
- def3.resolve();
- setTimeout(function() {
- expect(statStub.calledOnce).toEqual(true);
- highlightStub.restore();
- done();
- }, 3);
- });
- });
- });
- describe('Handling errors', function () {
- var deferredList;
- var getFolderContentsStub;
- var reloading;
- beforeEach(function() {
- deferredList = $.Deferred();
- getFolderContentsStub =
- sinon.stub(filesClient, 'getFolderContents');
- getFolderContentsStub.onCall(0).returns(deferredList.promise());
- getFolderContentsStub.onCall(1).returns($.Deferred().promise());
- reloading = fileList.reload();
- });
- afterEach(function() {
- getFolderContentsStub.restore();
- fileList = undefined;
- });
- it('redirects to root folder in case of forbidden access', function (done) {
- deferredList.reject(403);
- return reloading.then(function() {
- expect(fileList.getCurrentDirectory()).toEqual('/');
- expect(getFolderContentsStub.calledTwice).toEqual(true);
- }).then(done, done);
- });
- it('redirects to root folder and shows notification in case of internal server error', function (done) {
- expect(notificationStub.notCalled).toEqual(true);
- deferredList.reject(500);
- return reloading.then(function() {
- expect(fileList.getCurrentDirectory()).toEqual('/');
- expect(getFolderContentsStub.calledTwice).toEqual(true);
- expect(notificationStub.calledOnce).toEqual(true);
- }).then(done, done);
- });
- it('redirects to root folder and shows notification in case of storage not available', function (done) {
- expect(notificationStub.notCalled).toEqual(true);
- deferredList.reject(503, 'Storage is temporarily not available');
- return reloading.then(function() {
- expect(fileList.getCurrentDirectory()).toEqual('/');
- expect(getFolderContentsStub.calledTwice).toEqual(true);
- expect(notificationStub.calledOnce).toEqual(true);
- }).then(done, done);
- });
- });
- describe('showFileBusyState', function() {
- var $tr;
- beforeEach(function() {
- fileList.setFiles(testFiles);
- $tr = fileList.findFileEl('Two.jpg');
- });
- it('shows spinner on busy rows', function() {
- fileList.showFileBusyState('Two.jpg', true);
- expect($tr.hasClass('busy')).toEqual(true);
- expect($tr.find('.thumbnail').parent().attr('class'))
- .toContain('icon-loading-small');
- fileList.showFileBusyState('Two.jpg', false);
- expect($tr.hasClass('busy')).toEqual(false);
- expect(OC.TestUtil.getImageUrl($tr.find('.thumbnail')))
- .toEqual(OC.imagePath('core', 'filetypes/image.svg'));
- });
- it('accepts multiple input formats', function() {
- _.each([
- 'Two.jpg',
- ['Two.jpg'],
- $tr,
- [$tr]
- ], function(testCase) {
- fileList.showFileBusyState(testCase, true);
- expect($tr.hasClass('busy')).toEqual(true);
- fileList.showFileBusyState(testCase, false);
- expect($tr.hasClass('busy')).toEqual(false);
- });
- });
- });
- describe('elementToFile', function() {
- var $tr;
- beforeEach(function() {
- fileList.setFiles(testFiles);
- $tr = fileList.findFileEl('One.txt');
- });
- it('converts data attributes to file info structure', function() {
- var fileInfo = fileList.elementToFile($tr);
- expect(fileInfo.id).toEqual(1);
- expect(fileInfo.name).toEqual('One.txt');
- expect(fileInfo.mtime).toEqual(123456789);
- expect(fileInfo.etag).toEqual('abc');
- expect(fileInfo.permissions).toEqual(OC.PERMISSION_ALL);
- expect(fileInfo.size).toEqual(12);
- expect(fileInfo.mimetype).toEqual('text/plain');
- expect(fileInfo.type).toEqual('file');
- expect(fileInfo.path).not.toBeDefined();
- expect(fileInfo.isEncrypted).toEqual(false);
- });
- it('sets isEncrypted attribute if data includes true e2eencrypted', function() {
- testFiles[3].isEncrypted = true;
- fileList.setFiles(testFiles);
- $tr = fileList.findFileEl('somedir');
- var fileInfo = fileList.elementToFile($tr);
- expect(fileInfo.isEncrypted).toEqual(true);
- });
- it('adds path attribute if available', function() {
- $tr.attr('data-path', '/subdir');
- var fileInfo = fileList.elementToFile($tr);
- expect(fileInfo.path).toEqual('/subdir');
- });
- });
- describe('new file menu', function() {
- var newFileMenuStub;
- beforeEach(function() {
- newFileMenuStub = sinon.stub(OCA.Files.NewFileMenu.prototype, 'showAt');
- });
- afterEach(function() {
- newFileMenuStub.restore();
- })
- it('renders new button when no legacy upload button exists', function() {
- expect(fileList.$el.find('.button.upload').length).toEqual(0);
- expect(fileList.$el.find('.button.new').length).toEqual(1);
- });
- it('does not render new button when no legacy upload button exists (public page)', function() {
- fileList.destroy();
- $('.files-controls').append('<input type="button" class="button upload" />');
- fileList = new OCA.Files.FileList($('#app-content-files'));
- expect(fileList.$el.find('.button.upload').length).toEqual(1);
- expect(fileList.$el.find('.button.new').length).toEqual(0);
- });
- it('opens the new file menu when clicking on the "New" button', function() {
- var $button = fileList.$el.find('.button.new');
- $button.click();
- expect(newFileMenuStub.calledOnce).toEqual(true);
- });
- it('does not open the new file menu when button is disabled', function() {
- var $button = fileList.$el.find('.button.new');
- $button.addClass('disabled');
- $button.click();
- expect(newFileMenuStub.notCalled).toEqual(true);
- });
- });
- describe('mount type detection', function() {
- function testMountType(dirInfoId, dirInfoMountType, inputMountType, expectedMountType) {
- var $tr;
- fileList.dirInfo.id = dirInfoId;
- fileList.dirInfo.mountType = dirInfoMountType;
- $tr = fileList.add({
- type: 'dir',
- mimetype: 'httpd/unix-directory',
- name: 'test dir',
- mountType: inputMountType
- });
- expect($tr.attr('data-mounttype')).toEqual(expectedMountType);
- }
- it('leaves mount type as is if no parent exists', function() {
- testMountType(null, null, 'external', 'external');
- testMountType(null, null, 'shared', 'shared');
- });
- it('detects share root if parent exists', function() {
- testMountType(123, null, 'shared', 'shared-root');
- testMountType(123, 'shared', 'shared', 'shared');
- testMountType(123, 'shared-root', 'shared', 'shared');
- });
- it('detects external storage root if parent exists', function() {
- testMountType(123, null, 'external', 'external-root');
- testMountType(123, 'external', 'external', 'external');
- testMountType(123, 'external-root', 'external', 'external');
- });
- });
- describe('file list should not refresh if url does not change', function() {
- var fileListStub;
- beforeEach(function() {
- fileListStub = sinon.stub(OCA.Files.FileList.prototype, 'changeDirectory');
- fileList._currentDirectory = '/subdir';
- });
- afterEach(function() {
- fileListStub.restore();
- });
- it('File list must not be refreshed', function() {
- $('#app-content-files').trigger(new $.Event('urlChanged', {dir: '/subdir'}));
- expect(fileListStub.notCalled).toEqual(true);
- });
- it('File list must be refreshed', function() {
- $('#app-content-files').trigger(new $.Event('urlChanged', {dir: '/'}));
- expect(fileListStub.notCalled).toEqual(false);
- });
- });
- });
|