versionstabviewSpec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. $item = $versions.eq(1);
  74. expect($item.find('.downloadVersion').attr('href')).toEqual(version2.getDownloadUrl());
  75. expect($item.find('.versiondate').text()).toEqual('2 days ago');
  76. expect($item.find('.size').text()).toEqual('< 1 KB');
  77. expect($item.find('.revertVersion').length).toEqual(1);
  78. });
  79. it('does not render revert button when no update permissions', function() {
  80. fileInfoModel.set('permissions', OC.PERMISSION_READ);
  81. tabView.setFileInfo(fileInfoModel);
  82. tabView.collection.set(testVersions);
  83. var version1 = testVersions[0];
  84. var version2 = testVersions[1];
  85. var $versions = tabView.$el.find('.versions>li');
  86. expect($versions.length).toEqual(2);
  87. var $item = $versions.eq(0);
  88. expect($item.find('.downloadVersion').attr('href')).toEqual(version1.getDownloadUrl());
  89. expect($item.find('.versiondate').text()).toEqual('seconds ago');
  90. expect($item.find('.revertVersion').length).toEqual(0);
  91. $item = $versions.eq(1);
  92. expect($item.find('.downloadVersion').attr('href')).toEqual(version2.getDownloadUrl());
  93. expect($item.find('.versiondate').text()).toEqual('2 days ago');
  94. expect($item.find('.revertVersion').length).toEqual(0);
  95. });
  96. });
  97. describe('Reverting', function() {
  98. var revertStub;
  99. beforeEach(function() {
  100. revertStub = sinon.stub(VersionModel.prototype, 'revert');
  101. tabView.setFileInfo(fileInfoModel);
  102. tabView.collection.set(testVersions);
  103. });
  104. afterEach(function() {
  105. revertStub.restore();
  106. });
  107. it('tells the model to revert when clicking "Revert"', function() {
  108. tabView.$el.find('.revertVersion').eq(1).click();
  109. expect(revertStub.calledOnce).toEqual(true);
  110. });
  111. it('triggers busy state during revert', function() {
  112. var busyStub = sinon.stub();
  113. fileInfoModel.on('busy', busyStub);
  114. tabView.$el.find('.revertVersion').eq(1).click();
  115. expect(busyStub.calledOnce).toEqual(true);
  116. expect(busyStub.calledWith(fileInfoModel, true)).toEqual(true);
  117. busyStub.reset();
  118. revertStub.getCall(0).args[0].success();
  119. expect(busyStub.calledOnce).toEqual(true);
  120. expect(busyStub.calledWith(fileInfoModel, false)).toEqual(true);
  121. });
  122. it('updates the file info model with the information from the reverted revision', function() {
  123. var changeStub = sinon.stub();
  124. fileInfoModel.on('change', changeStub);
  125. tabView.$el.find('.revertVersion').eq(1).click();
  126. expect(changeStub.notCalled).toEqual(true);
  127. revertStub.getCall(0).args[0].success();
  128. expect(changeStub.calledOnce).toEqual(true);
  129. var changes = changeStub.getCall(0).args[0].changed;
  130. expect(changes.size).toEqual(150);
  131. expect(changes.mtime).toEqual(testVersions[1].get('timestamp') * 1000);
  132. expect(changes.etag).toBeDefined();
  133. });
  134. it('shows notification on revert error', function() {
  135. var notificationStub = sinon.stub(OC.Notification, 'show');
  136. tabView.$el.find('.revertVersion').eq(1).click();
  137. revertStub.getCall(0).args[0].error();
  138. expect(notificationStub.calledOnce).toEqual(true);
  139. notificationStub.restore();
  140. });
  141. });
  142. });