mainfileinfodetailviewSpec.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('OCA.Files.MainFileInfoDetailView tests', function() {
  7. var view, tooltipStub, fileActions, fileList, testFileInfo;
  8. beforeEach(function() {
  9. tooltipStub = sinon.stub($.fn, 'tooltip');
  10. fileActions = new OCA.Files.FileActions();
  11. fileList = new OCA.Files.FileList($('<table></table>'), {
  12. fileActions: fileActions
  13. });
  14. view = new OCA.Files.MainFileInfoDetailView({
  15. fileList: fileList,
  16. fileActions: fileActions
  17. });
  18. testFileInfo = new OCA.Files.FileInfoModel({
  19. id: 5,
  20. name: 'One.txt',
  21. mimetype: 'text/plain',
  22. permissions: 31,
  23. path: '/subdir',
  24. size: 123456789,
  25. etag: 'abcdefg',
  26. mtime: Date.UTC(2015, 6, 17, 1, 2, 0, 0)
  27. });
  28. });
  29. afterEach(function() {
  30. view.remove();
  31. view = undefined;
  32. tooltipStub.restore();
  33. });
  34. describe('rendering', function() {
  35. it('displays basic info', function() {
  36. var clock = sinon.useFakeTimers(Date.UTC(2015, 6, 17, 1, 2, 0, 3));
  37. var dateExpected = OC.Util.formatDate(Date(Date.UTC(2015, 6, 17, 1, 2, 0, 0)));
  38. view.setFileInfo(testFileInfo);
  39. expect(view.$el.find('.fileName h3').text()).toEqual('One.txt');
  40. expect(view.$el.find('.fileName h3').attr('title')).toEqual('One.txt');
  41. expect(view.$el.find('.size').text()).toEqual('117.7 MB');
  42. expect(view.$el.find('.size').attr('title')).toEqual('123456789 bytes');
  43. expect(view.$el.find('.date').text()).toEqual('seconds ago');
  44. expect(view.$el.find('.date').attr('title')).toEqual(dateExpected);
  45. clock.restore();
  46. });
  47. it('displays permalink', function() {
  48. view.setFileInfo(testFileInfo);
  49. expect(view.$el.find('.permalink').attr('href'))
  50. .toEqual(OC.getProtocol() + '://' + OC.getHost() + OC.generateUrl('/f/5'));
  51. });
  52. it('displays favorite icon', function() {
  53. fileActions.registerAction({
  54. name: 'Favorite',
  55. mime: 'all',
  56. permissions: OC.PERMISSION_NONE
  57. });
  58. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  59. view.setFileInfo(testFileInfo);
  60. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(true);
  61. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(false);
  62. testFileInfo.set('tags', []);
  63. view.setFileInfo(testFileInfo);
  64. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(false);
  65. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(true);
  66. });
  67. it('does not display favorite icon if favorite action is not available', function() {
  68. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  69. view.setFileInfo(testFileInfo);
  70. expect(view.$el.find('.action-favorite').length).toEqual(0);
  71. testFileInfo.set('tags', []);
  72. view.setFileInfo(testFileInfo);
  73. expect(view.$el.find('.action-favorite').length).toEqual(0);
  74. });
  75. it('displays mime icon', function() {
  76. // File
  77. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  78. testFileInfo.set('mimetype', 'text/calendar');
  79. view.setFileInfo(testFileInfo);
  80. expect(lazyLoadPreviewStub.calledOnce).toEqual(true);
  81. var previewArgs = lazyLoadPreviewStub.getCall(0).args;
  82. expect(previewArgs[0].mime).toEqual('text/calendar');
  83. expect(previewArgs[0].path).toEqual('/subdir/One.txt');
  84. expect(previewArgs[0].etag).toEqual('abcdefg');
  85. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  86. // returns mime icon first without img parameter
  87. previewArgs[0].callback(
  88. OC.imagePath('core', 'filetypes/text-calendar.svg')
  89. );
  90. // still loading
  91. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  92. // preview loading failed, no prview
  93. previewArgs[0].error();
  94. // loading stopped, the mimetype icon gets displayed
  95. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  96. expect(view.$el.find('.thumbnail').css('background-image'))
  97. .toContain('filetypes/text-calendar.svg');
  98. // Folder
  99. testFileInfo.set('mimetype', 'httpd/unix-directory');
  100. view.setFileInfo(testFileInfo);
  101. expect(view.$el.find('.thumbnail').css('background-image'))
  102. .toContain('filetypes/folder.svg');
  103. lazyLoadPreviewStub.restore();
  104. });
  105. it('uses icon from model if present in model', function() {
  106. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  107. testFileInfo.set('mimetype', 'httpd/unix-directory');
  108. testFileInfo.set('icon', OC.MimeType.getIconUrl('dir-external'));
  109. view.setFileInfo(testFileInfo);
  110. expect(lazyLoadPreviewStub.notCalled).toEqual(true);
  111. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  112. expect(view.$el.find('.thumbnail').css('background-image'))
  113. .toContain('filetypes/folder-external.svg');
  114. lazyLoadPreviewStub.restore();
  115. });
  116. it('displays thumbnail', function() {
  117. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  118. testFileInfo.set('mimetype', 'text/plain');
  119. view.setFileInfo(testFileInfo);
  120. expect(lazyLoadPreviewStub.calledOnce).toEqual(true);
  121. var previewArgs = lazyLoadPreviewStub.getCall(0).args;
  122. expect(previewArgs[0].mime).toEqual('text/plain');
  123. expect(previewArgs[0].path).toEqual('/subdir/One.txt');
  124. expect(previewArgs[0].etag).toEqual('abcdefg');
  125. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  126. // returns mime icon first without img parameter
  127. previewArgs[0].callback(
  128. OC.imagePath('core', 'filetypes/text-plain.svg')
  129. );
  130. // still loading
  131. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  132. // return an actual (simulated) image
  133. previewArgs[0].callback(
  134. 'testimage', {
  135. width: 100,
  136. height: 200
  137. }
  138. );
  139. // loading stopped, image got displayed
  140. expect(view.$el.find('.thumbnail').css('background-image'))
  141. .toContain('testimage');
  142. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  143. lazyLoadPreviewStub.restore();
  144. });
  145. it('does not show size if no size available', function() {
  146. testFileInfo.unset('size');
  147. view.setFileInfo(testFileInfo);
  148. expect(view.$el.find('.size').length).toEqual(0);
  149. });
  150. it('renders displayName instead of name if available', function() {
  151. testFileInfo.set('displayName', 'hello.txt');
  152. view.setFileInfo(testFileInfo);
  153. expect(view.$el.find('.fileName h3').text()).toEqual('hello.txt');
  154. expect(view.$el.find('.fileName h3').attr('title')).toEqual('hello.txt');
  155. });
  156. it('rerenders when changes are made on the model', function() {
  157. // Show the "Favorite" icon
  158. fileActions.registerAction({
  159. name: 'Favorite',
  160. mime: 'all',
  161. permissions: OC.PERMISSION_NONE
  162. });
  163. view.setFileInfo(testFileInfo);
  164. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  165. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(true);
  166. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(false);
  167. testFileInfo.set('tags', []);
  168. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(false);
  169. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(true);
  170. });
  171. it('unbinds change listener from model', function() {
  172. // Show the "Favorite" icon
  173. fileActions.registerAction({
  174. name: 'Favorite',
  175. mime: 'all',
  176. permissions: OC.PERMISSION_NONE
  177. });
  178. view.setFileInfo(testFileInfo);
  179. view.setFileInfo(new OCA.Files.FileInfoModel({
  180. id: 999,
  181. name: 'test.txt',
  182. path: '/'
  183. }));
  184. // set value on old model
  185. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  186. // no change
  187. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(false);
  188. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(true);
  189. });
  190. });
  191. describe('events', function() {
  192. it('triggers default action when clicking on the thumbnail', function() {
  193. var actionHandler = sinon.stub();
  194. fileActions.registerAction({
  195. name: 'Something',
  196. mime: 'all',
  197. permissions: OC.PERMISSION_READ,
  198. actionHandler: actionHandler
  199. });
  200. fileActions.setDefault('text/plain', 'Something');
  201. view.setFileInfo(testFileInfo);
  202. view.$el.find('.thumbnail').click();
  203. expect(actionHandler.calledOnce).toEqual(true);
  204. expect(actionHandler.getCall(0).args[0]).toEqual('One.txt');
  205. expect(actionHandler.getCall(0).args[1].fileList).toEqual(fileList);
  206. expect(actionHandler.getCall(0).args[1].fileActions).toEqual(fileActions);
  207. expect(actionHandler.getCall(0).args[1].fileInfoModel).toEqual(testFileInfo);
  208. });
  209. it('triggers "Favorite" action when clicking on the star', function() {
  210. var actionHandler = sinon.stub();
  211. fileActions.registerAction({
  212. name: 'Favorite',
  213. mime: 'all',
  214. permissions: OC.PERMISSION_READ,
  215. actionHandler: actionHandler
  216. });
  217. view.setFileInfo(testFileInfo);
  218. view.$el.find('.action-favorite').click();
  219. expect(actionHandler.calledOnce).toEqual(true);
  220. expect(actionHandler.getCall(0).args[0]).toEqual('One.txt');
  221. expect(actionHandler.getCall(0).args[1].fileList).toEqual(fileList);
  222. expect(actionHandler.getCall(0).args[1].fileActions).toEqual(fileActions);
  223. expect(actionHandler.getCall(0).args[1].fileInfoModel).toEqual(testFileInfo);
  224. });
  225. });
  226. });