filelistSpec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. describe('OCA.Trashbin.FileList tests', function() {
  22. var testFiles, alertStub, notificationStub, fileList;
  23. beforeEach(function() {
  24. alertStub = sinon.stub(OC.dialogs, 'alert');
  25. notificationStub = sinon.stub(OC.Notification, 'show');
  26. // init parameters and test table elements
  27. $('#testArea').append(
  28. '<div id="app-content-trashbin">' +
  29. // init horrible parameters
  30. '<input type="hidden" id="dir" value="/"></input>' +
  31. // set this but it shouldn't be used (could be the one from the
  32. // files app)
  33. '<input type="hidden" id="permissions" value="31"></input>' +
  34. // dummy controls
  35. '<div id="controls">' +
  36. ' <div class="actions creatable"></div>' +
  37. ' <div class="notCreatable"></div>' +
  38. '</div>' +
  39. // dummy table
  40. // TODO: at some point this will be rendered by the fileList class itself!
  41. '<table id="filestable">' +
  42. '<thead><tr><th id="headerName" class="hidden">' +
  43. '<input type="checkbox" id="select_all_trash" class="select-all">' +
  44. '<span class="name">Name</span>' +
  45. '<span class="selectedActions hidden">' +
  46. '<a href class="undelete">Restore</a>' +
  47. '<a href class="delete-selected">Delete</a></span>' +
  48. '</th></tr></thead>' +
  49. '<tbody id="fileList"></tbody>' +
  50. '<tfoot></tfoot>' +
  51. '</table>' +
  52. '<div id="emptycontent">Empty content message</div>' +
  53. '</div>'
  54. );
  55. testFiles = [{
  56. id: 1,
  57. type: 'file',
  58. name: 'One.txt',
  59. mtime: 11111000,
  60. mimetype: 'text/plain',
  61. etag: 'abc'
  62. }, {
  63. id: 2,
  64. type: 'file',
  65. name: 'Two.jpg',
  66. mtime: 22222000,
  67. mimetype: 'image/jpeg',
  68. etag: 'def',
  69. }, {
  70. id: 3,
  71. type: 'file',
  72. name: 'Three.pdf',
  73. mtime: 33333000,
  74. mimetype: 'application/pdf',
  75. etag: '123',
  76. }, {
  77. id: 4,
  78. type: 'dir',
  79. mtime: 99999000,
  80. name: 'somedir',
  81. mimetype: 'httpd/unix-directory',
  82. etag: '456'
  83. }];
  84. // register file actions like the trashbin App does
  85. var fileActions = OCA.Trashbin.App._createFileActions(fileList);
  86. fileList = new OCA.Trashbin.FileList(
  87. $('#app-content-trashbin'), {
  88. fileActions: fileActions
  89. }
  90. );
  91. });
  92. afterEach(function() {
  93. testFiles = undefined;
  94. fileList.destroy();
  95. fileList = undefined;
  96. $('#dir').remove();
  97. notificationStub.restore();
  98. alertStub.restore();
  99. });
  100. describe('Initialization', function() {
  101. it('Sorts by mtime by default', function() {
  102. expect(fileList._sort).toEqual('mtime');
  103. expect(fileList._sortDirection).toEqual('desc');
  104. });
  105. it('Always returns read and delete permission', function() {
  106. expect(fileList.getDirectoryPermissions()).toEqual(OC.PERMISSION_READ | OC.PERMISSION_DELETE);
  107. });
  108. });
  109. describe('Breadcrumbs', function() {
  110. beforeEach(function() {
  111. var data = {
  112. status: 'success',
  113. data: {
  114. files: testFiles,
  115. permissions: 1
  116. }
  117. };
  118. fakeServer.respondWith(/\/index\.php\/apps\/files_trashbin\/ajax\/list.php\?dir=%2Fsubdir/, [
  119. 200, {
  120. "Content-Type": "application/json"
  121. },
  122. JSON.stringify(data)
  123. ]);
  124. });
  125. it('links the breadcrumb to the trashbin view', function() {
  126. fileList.changeDirectory('/subdir', false, true);
  127. fakeServer.respond();
  128. var $crumbs = fileList.$el.find('#controls .crumb');
  129. expect($crumbs.length).toEqual(2);
  130. expect($crumbs.eq(0).find('a').text()).toEqual('');
  131. expect($crumbs.eq(0).find('a').attr('href'))
  132. .toEqual(OC.webroot + '/index.php/apps/files?view=trashbin&dir=/');
  133. expect($crumbs.eq(1).find('a').text()).toEqual('subdir');
  134. expect($crumbs.eq(1).find('a').attr('href'))
  135. .toEqual(OC.webroot + '/index.php/apps/files?view=trashbin&dir=/subdir');
  136. });
  137. });
  138. describe('Rendering rows', function() {
  139. it('renders rows with the correct data when in root', function() {
  140. // dir listing is false when in root
  141. $('#dir').val('/');
  142. fileList.setFiles(testFiles);
  143. var $rows = fileList.$el.find('tbody tr');
  144. var $tr = $rows.eq(0);
  145. expect($rows.length).toEqual(4);
  146. expect($tr.attr('data-id')).toEqual('1');
  147. expect($tr.attr('data-type')).toEqual('file');
  148. expect($tr.attr('data-file')).toEqual('One.txt.d11111');
  149. expect($tr.attr('data-size')).not.toBeDefined();
  150. expect($tr.attr('data-etag')).toEqual('abc');
  151. expect($tr.attr('data-permissions')).toEqual('9'); // read and delete
  152. expect($tr.attr('data-mime')).toEqual('text/plain');
  153. expect($tr.attr('data-mtime')).toEqual('11111000');
  154. expect($tr.find('a.name').attr('href')).toEqual('#');
  155. expect($tr.find('.nametext').text().trim()).toEqual('One.txt');
  156. expect(fileList.findFileEl('One.txt.d11111')[0]).toEqual($tr[0]);
  157. });
  158. it('renders rows with the correct data when in root after calling setFiles with the same data set', function() {
  159. // dir listing is false when in root
  160. $('#dir').val('/');
  161. fileList.setFiles(testFiles);
  162. fileList.setFiles(fileList.files);
  163. var $rows = fileList.$el.find('tbody tr');
  164. var $tr = $rows.eq(0);
  165. expect($rows.length).toEqual(4);
  166. expect($tr.attr('data-id')).toEqual('1');
  167. expect($tr.attr('data-type')).toEqual('file');
  168. expect($tr.attr('data-file')).toEqual('One.txt.d11111');
  169. expect($tr.attr('data-size')).not.toBeDefined();
  170. expect($tr.attr('data-etag')).toEqual('abc');
  171. expect($tr.attr('data-permissions')).toEqual('9'); // read and delete
  172. expect($tr.attr('data-mime')).toEqual('text/plain');
  173. expect($tr.attr('data-mtime')).toEqual('11111000');
  174. expect($tr.find('a.name').attr('href')).toEqual('#');
  175. expect($tr.find('.nametext').text().trim()).toEqual('One.txt');
  176. expect(fileList.findFileEl('One.txt.d11111')[0]).toEqual($tr[0]);
  177. });
  178. it('renders rows with the correct data when in subdirectory', function() {
  179. // dir listing is true when in a subdir
  180. $('#dir').val('/subdir');
  181. fileList.setFiles(testFiles);
  182. var $rows = fileList.$el.find('tbody tr');
  183. var $tr = $rows.eq(0);
  184. expect($rows.length).toEqual(4);
  185. expect($tr.attr('data-id')).toEqual('1');
  186. expect($tr.attr('data-type')).toEqual('file');
  187. expect($tr.attr('data-file')).toEqual('One.txt');
  188. expect($tr.attr('data-size')).not.toBeDefined();
  189. expect($tr.attr('data-etag')).toEqual('abc');
  190. expect($tr.attr('data-permissions')).toEqual('9'); // read and delete
  191. expect($tr.attr('data-mime')).toEqual('text/plain');
  192. expect($tr.attr('data-mtime')).toEqual('11111000');
  193. expect($tr.find('a.name').attr('href')).toEqual('#');
  194. expect($tr.find('.nametext').text().trim()).toEqual('One.txt');
  195. expect(fileList.findFileEl('One.txt')[0]).toEqual($tr[0]);
  196. });
  197. it('does not render a size column', function() {
  198. expect(fileList.$el.find('tbody tr .filesize').length).toEqual(0);
  199. });
  200. });
  201. describe('File actions', function() {
  202. describe('Deleting single files', function() {
  203. // TODO: checks ajax call
  204. // TODO: checks spinner
  205. // TODO: remove item after delete
  206. // TODO: bring back item if delete failed
  207. });
  208. describe('Restoring single files', function() {
  209. // TODO: checks ajax call
  210. // TODO: checks spinner
  211. // TODO: remove item after restore
  212. // TODO: bring back item if restore failed
  213. });
  214. });
  215. describe('file previews', function() {
  216. // TODO: check that preview URL is going through files_trashbin
  217. });
  218. describe('loading file list', function() {
  219. // TODO: check that ajax URL is going through files_trashbin
  220. });
  221. describe('breadcrumbs', function() {
  222. // TODO: test label + URL
  223. });
  224. describe('elementToFile', function() {
  225. var $tr;
  226. beforeEach(function() {
  227. fileList.setFiles(testFiles);
  228. $tr = fileList.findFileEl('One.txt.d11111');
  229. });
  230. it('converts data attributes to file info structure', function() {
  231. var fileInfo = fileList.elementToFile($tr);
  232. expect(fileInfo.id).toEqual(1);
  233. expect(fileInfo.name).toEqual('One.txt.d11111');
  234. expect(fileInfo.displayName).toEqual('One.txt');
  235. expect(fileInfo.mtime).toEqual(11111000);
  236. expect(fileInfo.etag).toEqual('abc');
  237. expect(fileInfo.permissions).toEqual(OC.PERMISSION_READ | OC.PERMISSION_DELETE);
  238. expect(fileInfo.mimetype).toEqual('text/plain');
  239. expect(fileInfo.type).toEqual('file');
  240. });
  241. });
  242. describe('Global Actions', function() {
  243. beforeEach(function() {
  244. fileList.setFiles(testFiles);
  245. fileList.findFileEl('One.txt.d11111').find('input:checkbox').click();
  246. fileList.findFileEl('Three.pdf.d33333').find('input:checkbox').click();
  247. fileList.findFileEl('somedir.d99999').find('input:checkbox').click();
  248. });
  249. describe('Delete', function() {
  250. it('Shows trashbin actions', function() {
  251. // visible because a few files were selected
  252. expect($('.selectedActions').is(':visible')).toEqual(true);
  253. expect($('.selectedActions .delete-selected').is(':visible')).toEqual(true);
  254. expect($('.selectedActions .undelete').is(':visible')).toEqual(true);
  255. // check
  256. fileList.$el.find('.select-all').click();
  257. // stays visible
  258. expect($('.selectedActions').is(':visible')).toEqual(true);
  259. expect($('.selectedActions .delete-selected').is(':visible')).toEqual(true);
  260. expect($('.selectedActions .undelete').is(':visible')).toEqual(true);
  261. // uncheck
  262. fileList.$el.find('.select-all').click();
  263. // becomes hidden now
  264. expect($('.selectedActions').is(':visible')).toEqual(false);
  265. expect($('.selectedActions .delete-selected').is(':visible')).toEqual(false);
  266. expect($('.selectedActions .undelete').is(':visible')).toEqual(false);
  267. });
  268. it('Deletes selected files when "Delete" clicked', function() {
  269. var request;
  270. $('.selectedActions .delete-selected').click();
  271. expect(fakeServer.requests.length).toEqual(1);
  272. request = fakeServer.requests[0];
  273. expect(request.url).toEqual(OC.webroot + '/index.php/apps/files_trashbin/ajax/delete.php');
  274. expect(OC.parseQueryString(request.requestBody))
  275. .toEqual({'dir': '/', files: '["One.txt.d11111","Three.pdf.d33333","somedir.d99999"]'});
  276. fakeServer.requests[0].respond(
  277. 200,
  278. { 'Content-Type': 'application/json' },
  279. JSON.stringify({
  280. status: 'success',
  281. data: {
  282. success: [
  283. {filename: 'One.txt.d11111'},
  284. {filename: 'Three.pdf.d33333'},
  285. {filename: 'somedir.d99999'}
  286. ]
  287. }
  288. })
  289. );
  290. expect(fileList.findFileEl('One.txt.d11111').length).toEqual(0);
  291. expect(fileList.findFileEl('Three.pdf.d33333').length).toEqual(0);
  292. expect(fileList.findFileEl('somedir.d99999').length).toEqual(0);
  293. expect(fileList.findFileEl('Two.jpg.d22222').length).toEqual(1);
  294. });
  295. it('Deletes all files when all selected when "Delete" clicked', function() {
  296. var request;
  297. $('.select-all').click();
  298. $('.selectedActions .delete-selected').click();
  299. expect(fakeServer.requests.length).toEqual(1);
  300. request = fakeServer.requests[0];
  301. expect(request.url).toEqual(OC.webroot + '/index.php/apps/files_trashbin/ajax/delete.php');
  302. expect(OC.parseQueryString(request.requestBody))
  303. .toEqual({'dir': '/', allfiles: 'true'});
  304. fakeServer.requests[0].respond(
  305. 200,
  306. { 'Content-Type': 'application/json' },
  307. JSON.stringify({status: 'success'})
  308. );
  309. expect(fileList.isEmpty).toEqual(true);
  310. });
  311. });
  312. describe('Restore', function() {
  313. it('Restores selected files when "Restore" clicked', function() {
  314. var request;
  315. $('.selectedActions .undelete').click();
  316. expect(fakeServer.requests.length).toEqual(1);
  317. request = fakeServer.requests[0];
  318. expect(request.url).toEqual(OC.webroot + '/index.php/apps/files_trashbin/ajax/undelete.php');
  319. expect(OC.parseQueryString(request.requestBody))
  320. .toEqual({'dir': '/', files: '["One.txt.d11111","Three.pdf.d33333","somedir.d99999"]'});
  321. fakeServer.requests[0].respond(
  322. 200,
  323. { 'Content-Type': 'application/json' },
  324. JSON.stringify({
  325. status: 'success',
  326. data: {
  327. success: [
  328. {filename: 'One.txt.d11111'},
  329. {filename: 'Three.pdf.d33333'},
  330. {filename: 'somedir.d99999'}
  331. ]
  332. }
  333. })
  334. );
  335. expect(fileList.findFileEl('One.txt.d11111').length).toEqual(0);
  336. expect(fileList.findFileEl('Three.pdf.d33333').length).toEqual(0);
  337. expect(fileList.findFileEl('somedir.d99999').length).toEqual(0);
  338. expect(fileList.findFileEl('Two.jpg.d22222').length).toEqual(1);
  339. });
  340. it('Restores all files when all selected when "Restore" clicked', function() {
  341. var request;
  342. $('.select-all').click();
  343. $('.selectedActions .undelete').click();
  344. expect(fakeServer.requests.length).toEqual(1);
  345. request = fakeServer.requests[0];
  346. expect(request.url).toEqual(OC.webroot + '/index.php/apps/files_trashbin/ajax/undelete.php');
  347. expect(OC.parseQueryString(request.requestBody))
  348. .toEqual({'dir': '/', allfiles: 'true'});
  349. fakeServer.requests[0].respond(
  350. 200,
  351. { 'Content-Type': 'application/json' },
  352. JSON.stringify({status: 'success'})
  353. );
  354. expect(fileList.isEmpty).toEqual(true);
  355. });
  356. });
  357. });
  358. });