versionstabviewSpec.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2015
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. describe('OCA.Versions.VersionsTabView', function() {
  11. var VersionCollection = OCA.Versions.VersionCollection;
  12. var VersionModel = OCA.Versions.VersionModel;
  13. var VersionsTabView = OCA.Versions.VersionsTabView;
  14. var fetchStub, fileInfoModel, tabView, testVersions, clock;
  15. beforeEach(function() {
  16. clock = sinon.useFakeTimers(Date.UTC(2015, 6, 17, 1, 2, 0, 3));
  17. var time1 = Date.UTC(2015, 6, 17, 1, 2, 0, 3) / 1000;
  18. var time2 = Date.UTC(2015, 6, 15, 1, 2, 0, 3) / 1000;
  19. var version1 = new VersionModel({
  20. id: time1,
  21. timestamp: time1,
  22. name: 'some file.txt',
  23. size: 140,
  24. fullPath: '/subdir/some file.txt',
  25. mimetype: 'text/plain'
  26. });
  27. var version2 = new VersionModel({
  28. id: time2,
  29. timestamp: time2,
  30. name: 'some file.txt',
  31. size: 150,
  32. fullPath: '/subdir/some file.txt',
  33. mimetype: 'text/plain'
  34. });
  35. testVersions = [version1, version2];
  36. fetchStub = sinon.stub(VersionCollection.prototype, 'fetch');
  37. fileInfoModel = new OCA.Files.FileInfoModel({
  38. id: 123,
  39. name: 'test.txt',
  40. permissions: OC.PERMISSION_READ | OC.PERMISSION_UPDATE
  41. });
  42. tabView = new VersionsTabView();
  43. tabView.render();
  44. });
  45. afterEach(function() {
  46. fetchStub.restore();
  47. tabView.remove();
  48. clock.restore();
  49. });
  50. describe('rendering', function() {
  51. it('reloads matching versions when setting file info model', function() {
  52. tabView.setFileInfo(fileInfoModel);
  53. expect(fetchStub.calledOnce).toEqual(true);
  54. });
  55. it('renders loading icon while fetching versions', function() {
  56. tabView.setFileInfo(fileInfoModel);
  57. tabView.collection.trigger('request');
  58. expect(tabView.$el.find('.loading').length).toEqual(1);
  59. expect(tabView.$el.find('.versions li').length).toEqual(0);
  60. });
  61. it('renders versions', function() {
  62. tabView.setFileInfo(fileInfoModel);
  63. tabView.collection.set(testVersions);
  64. var version1 = testVersions[0];
  65. var version2 = testVersions[1];
  66. var $versions = tabView.$el.find('.versions>li');
  67. expect($versions.length).toEqual(2);
  68. var $item = $versions.eq(0);
  69. expect($item.find('.downloadVersion').attr('href')).toEqual(version1.getDownloadUrl());
  70. expect($item.find('.versiondate').text()).toEqual('seconds ago');
  71. expect($item.find('.size').text()).toEqual('< 1 KB');
  72. expect($item.find('.revertVersion').length).toEqual(1);
  73. expect($item.find('.preview').attr('src')).toEqual('http://localhost/core/img/filetypes/text.svg');
  74. $item = $versions.eq(1);
  75. expect($item.find('.downloadVersion').attr('href')).toEqual(version2.getDownloadUrl());
  76. expect($item.find('.versiondate').text()).toEqual('2 days ago');
  77. expect($item.find('.size').text()).toEqual('< 1 KB');
  78. expect($item.find('.revertVersion').length).toEqual(1);
  79. expect($item.find('.preview').attr('src')).toEqual('http://localhost/core/img/filetypes/text.svg');
  80. });
  81. it('does not render revert button when no update permissions', function() {
  82. fileInfoModel.set('permissions', OC.PERMISSION_READ);
  83. tabView.setFileInfo(fileInfoModel);
  84. tabView.collection.set(testVersions);
  85. var version1 = testVersions[0];
  86. var version2 = testVersions[1];
  87. var $versions = tabView.$el.find('.versions>li');
  88. expect($versions.length).toEqual(2);
  89. var $item = $versions.eq(0);
  90. expect($item.find('.downloadVersion').attr('href')).toEqual(version1.getDownloadUrl());
  91. expect($item.find('.versiondate').text()).toEqual('seconds ago');
  92. expect($item.find('.revertVersion').length).toEqual(0);
  93. expect($item.find('.preview').attr('src')).toEqual('http://localhost/core/img/filetypes/text.svg');
  94. $item = $versions.eq(1);
  95. expect($item.find('.downloadVersion').attr('href')).toEqual(version2.getDownloadUrl());
  96. expect($item.find('.versiondate').text()).toEqual('2 days ago');
  97. expect($item.find('.revertVersion').length).toEqual(0);
  98. expect($item.find('.preview').attr('src')).toEqual('http://localhost/core/img/filetypes/text.svg');
  99. });
  100. });
  101. describe('More versions', function() {
  102. var hasMoreResultsStub;
  103. beforeEach(function() {
  104. tabView.setFileInfo(fileInfoModel);
  105. fetchStub.reset();
  106. tabView.collection.set(testVersions);
  107. hasMoreResultsStub = sinon.stub(VersionCollection.prototype, 'hasMoreResults');
  108. });
  109. afterEach(function() {
  110. hasMoreResultsStub.restore();
  111. });
  112. it('shows "More versions" button when more versions are available', function() {
  113. hasMoreResultsStub.returns(true);
  114. tabView.collection.trigger('sync');
  115. expect(tabView.$el.find('.showMoreVersions').hasClass('hidden')).toEqual(false);
  116. });
  117. it('does not show "More versions" button when more versions are available', function() {
  118. hasMoreResultsStub.returns(false);
  119. tabView.collection.trigger('sync');
  120. expect(tabView.$el.find('.showMoreVersions').hasClass('hidden')).toEqual(true);
  121. });
  122. it('fetches and appends the next page when clicking the "More" button', function() {
  123. hasMoreResultsStub.returns(true);
  124. expect(fetchStub.notCalled).toEqual(true);
  125. tabView.$el.find('.showMoreVersions').click();
  126. expect(fetchStub.calledOnce).toEqual(true);
  127. });
  128. it('appends version to the list when added to collection', function() {
  129. var time3 = Date.UTC(2015, 6, 10, 1, 0, 0, 0) / 1000;
  130. var version3 = new VersionModel({
  131. id: time3,
  132. timestamp: time3,
  133. name: 'some file.txt',
  134. size: 54,
  135. fullPath: '/subdir/some file.txt',
  136. mimetype: 'text/plain'
  137. });
  138. tabView.collection.add(version3);
  139. expect(tabView.$el.find('.versions>li').length).toEqual(3);
  140. var $item = tabView.$el.find('.versions>li').eq(2);
  141. expect($item.find('.downloadVersion').attr('href')).toEqual(version3.getDownloadUrl());
  142. expect($item.find('.versiondate').text()).toEqual('7 days ago');
  143. expect($item.find('.revertVersion').length).toEqual(1);
  144. expect($item.find('.preview').attr('src')).toEqual('http://localhost/core/img/filetypes/text.svg');
  145. });
  146. });
  147. describe('Reverting', function() {
  148. var revertStub;
  149. beforeEach(function() {
  150. revertStub = sinon.stub(VersionModel.prototype, 'revert');
  151. tabView.setFileInfo(fileInfoModel);
  152. tabView.collection.set(testVersions);
  153. });
  154. afterEach(function() {
  155. revertStub.restore();
  156. });
  157. it('tells the model to revert when clicking "Revert"', function() {
  158. tabView.$el.find('.revertVersion').eq(1).click();
  159. expect(revertStub.calledOnce).toEqual(true);
  160. });
  161. it('triggers busy state during revert', function() {
  162. var busyStub = sinon.stub();
  163. fileInfoModel.on('busy', busyStub);
  164. tabView.$el.find('.revertVersion').eq(1).click();
  165. expect(busyStub.calledOnce).toEqual(true);
  166. expect(busyStub.calledWith(fileInfoModel, true)).toEqual(true);
  167. busyStub.reset();
  168. revertStub.getCall(0).args[0].success();
  169. expect(busyStub.calledOnce).toEqual(true);
  170. expect(busyStub.calledWith(fileInfoModel, false)).toEqual(true);
  171. });
  172. it('updates the file info model with the information from the reverted revision', function() {
  173. var changeStub = sinon.stub();
  174. fileInfoModel.on('change', changeStub);
  175. tabView.$el.find('.revertVersion').eq(1).click();
  176. expect(changeStub.notCalled).toEqual(true);
  177. revertStub.getCall(0).args[0].success();
  178. expect(changeStub.calledOnce).toEqual(true);
  179. var changes = changeStub.getCall(0).args[0].changed;
  180. expect(changes.size).toEqual(150);
  181. expect(changes.mtime).toEqual(testVersions[1].get('timestamp') * 1000);
  182. expect(changes.etag).toBeDefined();
  183. });
  184. it('shows notification on revert error', function() {
  185. var notificationStub = sinon.stub(OC.Notification, 'show');
  186. tabView.$el.find('.revertVersion').eq(1).click();
  187. revertStub.getCall(0).args[0].error();
  188. expect(notificationStub.calledOnce).toEqual(true);
  189. notificationStub.restore();
  190. });
  191. });
  192. });