mainfileinfodetailviewSpec.js 9.7 KB

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