mainfileinfodetailviewSpec.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 favorite icon', function() {
  63. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  64. view.setFileInfo(testFileInfo);
  65. expect(view.$el.find('.favorite img').attr('src'))
  66. .toEqual(OC.imagePath('core', 'actions/starred'));
  67. testFileInfo.set('tags', []);
  68. view.setFileInfo(testFileInfo);
  69. expect(view.$el.find('.favorite img').attr('src'))
  70. .toEqual(OC.imagePath('core', 'actions/star'));
  71. });
  72. it('displays mime icon', function() {
  73. // File
  74. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  75. testFileInfo.set('mimetype', 'text/calendar');
  76. view.setFileInfo(testFileInfo);
  77. expect(lazyLoadPreviewStub.calledOnce).toEqual(true);
  78. var previewArgs = lazyLoadPreviewStub.getCall(0).args;
  79. expect(previewArgs[0].mime).toEqual('text/calendar');
  80. expect(previewArgs[0].path).toEqual('/subdir/One.txt');
  81. expect(previewArgs[0].etag).toEqual('abcdefg');
  82. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  83. // returns mime icon first without img parameter
  84. previewArgs[0].callback(
  85. OC.imagePath('core', 'filetypes/text-calendar.svg')
  86. );
  87. // still loading
  88. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  89. // preview loading failed, no prview
  90. previewArgs[0].error();
  91. // loading stopped, the mimetype icon gets displayed
  92. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  93. expect(view.$el.find('.thumbnail').css('background-image'))
  94. .toContain('filetypes/text-calendar.svg');
  95. // Folder
  96. testFileInfo.set('mimetype', 'httpd/unix-directory');
  97. view.setFileInfo(testFileInfo);
  98. expect(view.$el.find('.thumbnail').css('background-image'))
  99. .toContain('filetypes/folder.svg');
  100. lazyLoadPreviewStub.restore();
  101. });
  102. it('uses icon from model if present in model', function() {
  103. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  104. testFileInfo.set('mimetype', 'httpd/unix-directory');
  105. testFileInfo.set('icon', OC.MimeType.getIconUrl('dir-external'));
  106. view.setFileInfo(testFileInfo);
  107. expect(lazyLoadPreviewStub.notCalled).toEqual(true);
  108. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  109. expect(view.$el.find('.thumbnail').css('background-image'))
  110. .toContain('filetypes/folder-external.svg');
  111. lazyLoadPreviewStub.restore();
  112. });
  113. it('displays thumbnail', function() {
  114. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  115. testFileInfo.set('mimetype', 'text/plain');
  116. view.setFileInfo(testFileInfo);
  117. expect(lazyLoadPreviewStub.calledOnce).toEqual(true);
  118. var previewArgs = lazyLoadPreviewStub.getCall(0).args;
  119. expect(previewArgs[0].mime).toEqual('text/plain');
  120. expect(previewArgs[0].path).toEqual('/subdir/One.txt');
  121. expect(previewArgs[0].etag).toEqual('abcdefg');
  122. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  123. // returns mime icon first without img parameter
  124. previewArgs[0].callback(
  125. OC.imagePath('core', 'filetypes/text-plain.svg')
  126. );
  127. // still loading
  128. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  129. // return an actual (simulated) image
  130. previewArgs[0].callback(
  131. 'testimage', {
  132. width: 100,
  133. height: 200
  134. }
  135. );
  136. // loading stopped, image got displayed
  137. expect(view.$el.find('.thumbnail').css('background-image'))
  138. .toContain('testimage');
  139. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  140. lazyLoadPreviewStub.restore();
  141. });
  142. it('does not show size if no size available', function() {
  143. testFileInfo.unset('size');
  144. view.setFileInfo(testFileInfo);
  145. expect(view.$el.find('.size').length).toEqual(0);
  146. });
  147. it('renders displayName instead of name if available', function() {
  148. testFileInfo.set('displayName', 'hello.txt');
  149. view.setFileInfo(testFileInfo);
  150. expect(view.$el.find('.fileName h3').text()).toEqual('hello.txt');
  151. expect(view.$el.find('.fileName h3').attr('title')).toEqual('hello.txt');
  152. });
  153. it('rerenders when changes are made on the model', function() {
  154. view.setFileInfo(testFileInfo);
  155. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  156. expect(view.$el.find('.favorite img').attr('src'))
  157. .toEqual(OC.imagePath('core', 'actions/starred'));
  158. testFileInfo.set('tags', []);
  159. expect(view.$el.find('.favorite img').attr('src'))
  160. .toEqual(OC.imagePath('core', 'actions/star'));
  161. });
  162. it('unbinds change listener from model', function() {
  163. view.setFileInfo(testFileInfo);
  164. view.setFileInfo(new OCA.Files.FileInfoModel({
  165. id: 999,
  166. name: 'test.txt',
  167. path: '/'
  168. }));
  169. // set value on old model
  170. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  171. // no change
  172. expect(view.$el.find('.favorite img').attr('src'))
  173. .toEqual(OC.imagePath('core', 'actions/star'));
  174. });
  175. });
  176. describe('events', function() {
  177. it('triggers default action when clicking on the thumbnail', function() {
  178. var actionHandler = sinon.stub();
  179. fileActions.registerAction({
  180. name: 'Something',
  181. mime: 'all',
  182. permissions: OC.PERMISSION_READ,
  183. actionHandler: actionHandler
  184. });
  185. fileActions.setDefault('text/plain', 'Something');
  186. view.setFileInfo(testFileInfo);
  187. view.$el.find('.thumbnail').click();
  188. expect(actionHandler.calledOnce).toEqual(true);
  189. expect(actionHandler.getCall(0).args[0]).toEqual('One.txt');
  190. expect(actionHandler.getCall(0).args[1].fileList).toEqual(fileList);
  191. expect(actionHandler.getCall(0).args[1].fileActions).toEqual(fileActions);
  192. expect(actionHandler.getCall(0).args[1].fileInfoModel).toEqual(testFileInfo);
  193. });
  194. it('triggers "Favorite" action when clicking on the star', function() {
  195. var actionHandler = sinon.stub();
  196. fileActions.registerAction({
  197. name: 'Favorite',
  198. mime: 'all',
  199. permissions: OC.PERMISSION_READ,
  200. actionHandler: actionHandler
  201. });
  202. view.setFileInfo(testFileInfo);
  203. view.$el.find('.action-favorite').click();
  204. expect(actionHandler.calledOnce).toEqual(true);
  205. expect(actionHandler.getCall(0).args[0]).toEqual('One.txt');
  206. expect(actionHandler.getCall(0).args[1].fileList).toEqual(fileList);
  207. expect(actionHandler.getCall(0).args[1].fileActions).toEqual(fileActions);
  208. expect(actionHandler.getCall(0).args[1].fileInfoModel).toEqual(testFileInfo);
  209. });
  210. });
  211. });