fileactionsmenuSpec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * @copyright 2015 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  6. * @author John Molakvoæ <skjnldsv@protonmail.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Roland Tapken <roland@bitarbeiter.net>
  9. * @author Vincent Petry <vincent@nextcloud.com>
  10. *
  11. * @license AGPL-3.0-or-later
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. describe('OCA.Files.FileActionsMenu tests', function() {
  28. var fileList, fileActions, menu, actionStub, menuContext, $tr;
  29. beforeEach(function() {
  30. // init horrible parameters
  31. var $body = $('#testArea');
  32. $body.append('<input type="hidden" id="permissions" value="31"></input>');
  33. // dummy files table
  34. actionStub = sinon.stub();
  35. fileActions = new OCA.Files.FileActions();
  36. fileList = new OCA.Files.FileList($body, {
  37. fileActions: fileActions
  38. });
  39. fileList.changeDirectory('/subdir', false, true);
  40. fileActions.registerAction({
  41. name: 'Testdropdown',
  42. displayName: 'Testdropdowndisplay',
  43. mime: 'all',
  44. permissions: OC.PERMISSION_READ,
  45. icon: function () {
  46. return OC.imagePath('core', 'actions/download');
  47. },
  48. actionHandler: actionStub
  49. });
  50. fileActions.registerAction({
  51. name: 'Testdropdownnoicon',
  52. displayName: 'Testdropdowndisplaynoicon',
  53. mime: 'all',
  54. permissions: OC.PERMISSION_READ,
  55. actionHandler: actionStub
  56. });
  57. fileActions.registerAction({
  58. name: 'Testinline',
  59. displayName: 'Testinlinedisplay',
  60. type: OCA.Files.FileActions.TYPE_INLINE,
  61. mime: 'all',
  62. permissions: OC.PERMISSION_READ
  63. });
  64. fileActions.registerAction({
  65. name: 'Testdefault',
  66. displayName: 'Testdefaultdisplay',
  67. mime: 'all',
  68. permissions: OC.PERMISSION_READ
  69. });
  70. fileActions.setDefault('all', 'Testdefault');
  71. var fileData = {
  72. id: 18,
  73. type: 'file',
  74. name: 'testName.txt',
  75. mimetype: 'text/plain',
  76. size: '1234',
  77. etag: 'a01234c',
  78. mtime: '123456'
  79. };
  80. $tr = fileList.add(fileData);
  81. menuContext = {
  82. $file: $tr,
  83. fileList: fileList,
  84. fileActions: fileActions,
  85. dir: fileList.getCurrentDirectory()
  86. };
  87. menu = new OCA.Files.FileActionsMenu();
  88. menu.show(menuContext);
  89. });
  90. afterEach(function() {
  91. fileActions = null;
  92. fileList.destroy();
  93. fileList = undefined;
  94. menu.remove();
  95. $('#permissions, .files-filestable').remove();
  96. });
  97. describe('rendering', function() {
  98. it('renders dropdown actions in menu', function() {
  99. var $action = menu.$el.find('a[data-action=Testdropdown]');
  100. expect($action.length).toEqual(1);
  101. expect($action.find('img').attr('src'))
  102. .toEqual(OC.imagePath('core', 'actions/download'));
  103. expect($action.find('.no-icon').length).toEqual(0);
  104. $action = menu.$el.find('a[data-action=Testdropdownnoicon]');
  105. expect($action.length).toEqual(1);
  106. expect($action.find('img').length).toEqual(0);
  107. expect($action.find('.no-icon').length).toEqual(1);
  108. });
  109. it('does not render default actions', function() {
  110. expect(menu.$el.find('a[data-action=Testdefault]').length).toEqual(0);
  111. });
  112. it('render inline actions', function() {
  113. expect(menu.$el.find('a[data-action=Testinline]').length).toEqual(1);
  114. });
  115. it('render inline actions but it is hidden', function() {
  116. expect(menu.$el.find('a[data-action=Testinline]').parent().hasClass('hidden')).toEqual(true);
  117. });
  118. it('only renders actions relevant to the mime type', function() {
  119. fileActions.registerAction({
  120. name: 'Match',
  121. displayName: 'MatchDisplay',
  122. mime: 'text/plain',
  123. permissions: OC.PERMISSION_READ
  124. });
  125. fileActions.registerAction({
  126. name: 'Nomatch',
  127. displayName: 'NoMatchDisplay',
  128. mime: 'application/octet-stream',
  129. permissions: OC.PERMISSION_READ
  130. });
  131. menu.render();
  132. expect(menu.$el.find('a[data-action=Match]').length).toEqual(1);
  133. expect(menu.$el.find('a[data-action=NoMatch]').length).toEqual(0);
  134. });
  135. it('only renders actions relevant to the permissions', function() {
  136. fileActions.registerAction({
  137. name: 'Match',
  138. displayName: 'MatchDisplay',
  139. mime: 'text/plain',
  140. permissions: OC.PERMISSION_UPDATE
  141. });
  142. fileActions.registerAction({
  143. name: 'Nomatch',
  144. displayName: 'NoMatchDisplay',
  145. mime: 'text/plain',
  146. permissions: OC.PERMISSION_DELETE
  147. });
  148. menu.render();
  149. expect(menu.$el.find('a[data-action=Match]').length).toEqual(1);
  150. expect(menu.$el.find('a[data-action=NoMatch]').length).toEqual(0);
  151. });
  152. it('sorts by order attribute, then name', function() {
  153. fileActions.registerAction({
  154. name: 'Baction',
  155. displayName: 'Baction',
  156. order: 2,
  157. mime: 'text/plain',
  158. permissions: OC.PERMISSION_ALL
  159. });
  160. fileActions.registerAction({
  161. name: 'Zaction',
  162. displayName: 'Zaction',
  163. order: 1,
  164. mime: 'text/plain',
  165. permissions: OC.PERMISSION_ALL
  166. });
  167. fileActions.registerAction({
  168. name: 'Yaction',
  169. displayName: 'Yaction',
  170. mime: 'text/plain',
  171. permissions: OC.PERMISSION_ALL
  172. });
  173. fileActions.registerAction({
  174. name: 'Waction',
  175. displayName: 'Waction',
  176. mime: 'text/plain',
  177. permissions: OC.PERMISSION_ALL
  178. });
  179. menu.render();
  180. var zactionIndex = menu.$el.find('a[data-action=Zaction]').closest('li').index();
  181. var bactionIndex = menu.$el.find('a[data-action=Baction]').closest('li').index();
  182. expect(zactionIndex).toBeLessThan(bactionIndex);
  183. var wactionIndex = menu.$el.find('a[data-action=Waction]').closest('li').index();
  184. var yactionIndex = menu.$el.find('a[data-action=Yaction]').closest('li').index();
  185. expect(wactionIndex).toBeLessThan(yactionIndex);
  186. });
  187. it('calls displayName function', function() {
  188. var displayNameStub = sinon.stub().returns('Test');
  189. fileActions.registerAction({
  190. name: 'Something',
  191. displayName: displayNameStub,
  192. mime: 'text/plain',
  193. permissions: OC.PERMISSION_ALL
  194. });
  195. menu.render();
  196. expect(displayNameStub.calledOnce).toEqual(true);
  197. expect(displayNameStub.calledWith(menuContext)).toEqual(true);
  198. expect(menu.$el.find('a[data-action=Something] span:not(.icon)').text()).toEqual('Test');
  199. });
  200. it('uses plain iconClass', function() {
  201. fileActions.registerAction({
  202. name: 'Something',
  203. mime: 'text/plain',
  204. permissions: OC.PERMISSION_ALL,
  205. iconClass: 'test'
  206. });
  207. menu.render();
  208. expect(menu.$el.find('a[data-action=Something]').children('span.icon').hasClass('test')).toEqual(true);
  209. });
  210. it('calls iconClass function', function() {
  211. var iconClassStub = sinon.stub().returns('test');
  212. fileActions.registerAction({
  213. name: 'Something',
  214. mime: 'text/plain',
  215. permissions: OC.PERMISSION_ALL,
  216. iconClass: iconClassStub
  217. });
  218. menu.render();
  219. expect(iconClassStub.calledOnce).toEqual(true);
  220. expect(iconClassStub.calledWith(menuContext.$file.attr('data-file'), menuContext)).toEqual(true);
  221. expect(menu.$el.find('a[data-action=Something]').children('span.icon').hasClass('test')).toEqual(true);
  222. });
  223. });
  224. describe('action handler', function() {
  225. it('calls action handler when clicking menu item', function() {
  226. var $action = menu.$el.find('a[data-action=Testdropdown]');
  227. $action.click();
  228. expect(actionStub.calledOnce).toEqual(true);
  229. expect(actionStub.getCall(0).args[0]).toEqual('testName.txt');
  230. expect(actionStub.getCall(0).args[1].$file[0]).toEqual($tr[0]);
  231. expect(actionStub.getCall(0).args[1].fileList).toEqual(fileList);
  232. expect(actionStub.getCall(0).args[1].fileActions).toEqual(fileActions);
  233. expect(actionStub.getCall(0).args[1].dir).toEqual('/subdir');
  234. });
  235. });
  236. describe('default actions from registerDefaultActions', function() {
  237. beforeEach(function() {
  238. fileActions.clear();
  239. fileActions.registerDefaultActions();
  240. });
  241. it('redirects to download URL when clicking download', function() {
  242. var redirectStub = sinon.stub(OC, 'redirect');
  243. var fileData = {
  244. id: 18,
  245. type: 'file',
  246. name: 'testName.txt',
  247. mimetype: 'text/plain',
  248. size: '1234',
  249. etag: 'a01234c',
  250. mtime: '123456'
  251. };
  252. var $tr = fileList.add(fileData);
  253. fileActions.display($tr.find('td.filename'), true, fileList);
  254. var menuContext = {
  255. $file: $tr,
  256. fileList: fileList,
  257. fileActions: fileActions,
  258. fileInfoModel: new OCA.Files.FileInfoModel(fileData),
  259. dir: fileList.getCurrentDirectory()
  260. };
  261. menu = new OCA.Files.FileActionsMenu();
  262. menu.show(menuContext);
  263. menu.$el.find('.action-download').click();
  264. expect(redirectStub.calledOnce).toEqual(true);
  265. expect(redirectStub.getCall(0).args[0]).toContain(
  266. OC.getRootPath() +
  267. '/remote.php/webdav/subdir/testName.txt'
  268. );
  269. redirectStub.restore();
  270. });
  271. it('takes the file\'s path into account when clicking download', function() {
  272. var redirectStub = sinon.stub(OC, 'redirect');
  273. var fileData = {
  274. id: 18,
  275. type: 'file',
  276. name: 'testName.txt',
  277. path: '/anotherpath/there',
  278. mimetype: 'text/plain',
  279. size: '1234',
  280. etag: 'a01234c',
  281. mtime: '123456'
  282. };
  283. var $tr = fileList.add(fileData);
  284. fileActions.display($tr.find('td.filename'), true, fileList);
  285. var menuContext = {
  286. $file: $tr,
  287. fileList: fileList,
  288. fileActions: fileActions,
  289. fileInfoModel: new OCA.Files.FileInfoModel(fileData),
  290. dir: '/anotherpath/there'
  291. };
  292. menu = new OCA.Files.FileActionsMenu();
  293. menu.show(menuContext);
  294. menu.$el.find('.action-download').click();
  295. expect(redirectStub.calledOnce).toEqual(true);
  296. expect(redirectStub.getCall(0).args[0]).toContain(
  297. OC.getRootPath() + '/remote.php/webdav/anotherpath/there/testName.txt'
  298. );
  299. redirectStub.restore();
  300. });
  301. it('deletes file when clicking delete', function() {
  302. var deleteStub = sinon.stub(fileList, 'do_delete');
  303. var fileData = {
  304. id: 18,
  305. type: 'file',
  306. name: 'testName.txt',
  307. path: '/somepath/dir',
  308. mimetype: 'text/plain',
  309. size: '1234',
  310. etag: 'a01234c',
  311. mtime: '123456'
  312. };
  313. var $tr = fileList.add(fileData);
  314. fileActions.display($tr.find('td.filename'), true, fileList);
  315. var menuContext = {
  316. $file: $tr,
  317. fileList: fileList,
  318. fileActions: fileActions,
  319. fileInfoModel: new OCA.Files.FileInfoModel(fileData),
  320. dir: '/somepath/dir'
  321. };
  322. menu = new OCA.Files.FileActionsMenu();
  323. menu.show(menuContext);
  324. menu.$el.find('.action-delete').click();
  325. expect(deleteStub.calledOnce).toEqual(true);
  326. expect(deleteStub.getCall(0).args[0]).toEqual('testName.txt');
  327. expect(deleteStub.getCall(0).args[1]).toEqual('/somepath/dir');
  328. deleteStub.restore();
  329. });
  330. });
  331. });