versionstabviewSpec.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * Copyright (c) 2015
  3. *
  4. * @author Michael Jobst <mjobst+github@tecratech.de>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author noveens <noveen.sachdeva@research.iiit.ac.in>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Vincent Petry <vincent@nextcloud.com>
  9. *
  10. * @license AGPL-3.0-or-later
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. describe('OCA.Versions.VersionsTabView', function() {
  27. var VersionCollection = OCA.Versions.VersionCollection;
  28. var VersionModel = OCA.Versions.VersionModel;
  29. var VersionsTabView = OCA.Versions.VersionsTabView;
  30. var fetchStub, fileInfoModel, tabView, testVersions, clock;
  31. beforeEach(function() {
  32. clock = sinon.useFakeTimers(Date.UTC(2015, 6, 17, 1, 2, 0, 3));
  33. var time1 = Date.UTC(2015, 6, 17, 1, 2, 0, 3) / 1000;
  34. var time2 = Date.UTC(2015, 6, 15, 1, 2, 0, 3) / 1000;
  35. var version1 = new VersionModel({
  36. id: time1,
  37. timestamp: time1,
  38. name: 'some file.txt',
  39. size: 140,
  40. fullPath: '/subdir/some file.txt',
  41. mimetype: 'text/plain'
  42. });
  43. var version2 = new VersionModel({
  44. id: time2,
  45. timestamp: time2,
  46. name: 'some file.txt',
  47. size: 150,
  48. fullPath: '/subdir/some file.txt',
  49. mimetype: 'text/plain'
  50. });
  51. testVersions = [version1, version2];
  52. fetchStub = sinon.stub(VersionCollection.prototype, 'fetch');
  53. fileInfoModel = new OCA.Files.FileInfoModel({
  54. id: 123,
  55. name: 'test.txt',
  56. permissions: OC.PERMISSION_READ | OC.PERMISSION_UPDATE
  57. });
  58. tabView = new VersionsTabView();
  59. tabView.render();
  60. });
  61. afterEach(function() {
  62. fetchStub.restore();
  63. tabView.remove();
  64. clock.restore();
  65. });
  66. describe('rendering', function() {
  67. it('reloads matching versions when setting file info model', function() {
  68. tabView.setFileInfo(fileInfoModel);
  69. expect(fetchStub.calledOnce).toEqual(true);
  70. });
  71. it('renders loading icon while fetching versions', function() {
  72. tabView.setFileInfo(fileInfoModel);
  73. tabView.collection.trigger('request');
  74. expect(tabView.$el.find('.loading').length).toEqual(1);
  75. expect(tabView.$el.find('.versions li').length).toEqual(0);
  76. });
  77. it('renders versions', function() {
  78. tabView.setFileInfo(fileInfoModel);
  79. tabView.collection.set(testVersions);
  80. var version1 = testVersions[0];
  81. var version2 = testVersions[1];
  82. var $versions = tabView.$el.find('.versions>li');
  83. expect($versions.length).toEqual(2);
  84. var $item = $versions.eq(0);
  85. expect($item.find('.downloadVersion').attr('href')).toEqual(version1.getDownloadUrl());
  86. expect($item.find('.versiondate').text()).toEqual('seconds ago');
  87. expect($item.find('.size').text()).toEqual('< 1 KB');
  88. expect($item.find('.revertVersion').length).toEqual(1);
  89. $item = $versions.eq(1);
  90. expect($item.find('.downloadVersion').attr('href')).toEqual(version2.getDownloadUrl());
  91. expect($item.find('.versiondate').text()).toEqual('2 days ago');
  92. expect($item.find('.size').text()).toEqual('< 1 KB');
  93. expect($item.find('.revertVersion').length).toEqual(1);
  94. });
  95. it('does not render revert button when no update permissions', function() {
  96. fileInfoModel.set('permissions', OC.PERMISSION_READ);
  97. tabView.setFileInfo(fileInfoModel);
  98. tabView.collection.set(testVersions);
  99. var version1 = testVersions[0];
  100. var version2 = testVersions[1];
  101. var $versions = tabView.$el.find('.versions>li');
  102. expect($versions.length).toEqual(2);
  103. var $item = $versions.eq(0);
  104. expect($item.find('.downloadVersion').attr('href')).toEqual(version1.getDownloadUrl());
  105. expect($item.find('.versiondate').text()).toEqual('seconds ago');
  106. expect($item.find('.revertVersion').length).toEqual(0);
  107. $item = $versions.eq(1);
  108. expect($item.find('.downloadVersion').attr('href')).toEqual(version2.getDownloadUrl());
  109. expect($item.find('.versiondate').text()).toEqual('2 days ago');
  110. expect($item.find('.revertVersion').length).toEqual(0);
  111. });
  112. });
  113. describe('Reverting', function() {
  114. var revertStub;
  115. beforeEach(function() {
  116. revertStub = sinon.stub(VersionModel.prototype, 'revert');
  117. tabView.setFileInfo(fileInfoModel);
  118. tabView.collection.set(testVersions);
  119. });
  120. afterEach(function() {
  121. revertStub.restore();
  122. });
  123. it('tells the model to revert when clicking "Revert"', function() {
  124. tabView.$el.find('.revertVersion').eq(1).click();
  125. expect(revertStub.calledOnce).toEqual(true);
  126. });
  127. it('triggers busy state during revert', function() {
  128. var busyStub = sinon.stub();
  129. fileInfoModel.on('busy', busyStub);
  130. tabView.$el.find('.revertVersion').eq(1).click();
  131. expect(busyStub.calledOnce).toEqual(true);
  132. expect(busyStub.calledWith(fileInfoModel, true)).toEqual(true);
  133. busyStub.reset();
  134. revertStub.getCall(0).args[0].success();
  135. expect(busyStub.calledOnce).toEqual(true);
  136. expect(busyStub.calledWith(fileInfoModel, false)).toEqual(true);
  137. });
  138. it('updates the file info model with the information from the reverted revision', function() {
  139. var changeStub = sinon.stub();
  140. fileInfoModel.on('change', changeStub);
  141. tabView.$el.find('.revertVersion').eq(1).click();
  142. expect(changeStub.notCalled).toEqual(true);
  143. revertStub.getCall(0).args[0].success();
  144. expect(changeStub.calledOnce).toEqual(true);
  145. var changes = changeStub.getCall(0).args[0].changed;
  146. expect(changes.size).toEqual(150);
  147. expect(changes.mtime).toEqual(testVersions[1].get('timestamp') * 1000);
  148. expect(changes.etag).toBeDefined();
  149. });
  150. it('shows notification on revert error', function() {
  151. var notificationStub = sinon.stub(OC.Notification, 'show');
  152. tabView.$el.find('.revertVersion').eq(1).click();
  153. revertStub.getCall(0).args[0].error();
  154. expect(notificationStub.calledOnce).toEqual(true);
  155. notificationStub.restore();
  156. });
  157. });
  158. });