UpdateTrimmerTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon');
  13. const chai = require('chai');
  14. const should = chai.should();
  15. const { expect } = chai;
  16. const modulePath = "../../../../app/js/UpdateTrimmer.js";
  17. const SandboxedModule = require('sandboxed-module');
  18. const tk = require("timekeeper");
  19. describe("UpdateTrimmer", function() {
  20. beforeEach(function() {
  21. this.now = new Date();
  22. tk.freeze(this.now);
  23. this.UpdateTrimmer = SandboxedModule.require(modulePath, { requires: {
  24. "logger-sharelatex": (this.logger = { log: sinon.stub(), error: sinon.stub() }),
  25. "./WebApiManager": (this.WebApiManager = {}),
  26. "./MongoManager": (this.MongoManager = {})
  27. }
  28. });
  29. this.callback = sinon.stub();
  30. return this.project_id = "mock-project-id";
  31. });
  32. afterEach(function() { return tk.reset(); });
  33. return describe("shouldTrimUpdates", function() {
  34. beforeEach(function() {
  35. this.metadata = {};
  36. this.details =
  37. {features: {}};
  38. this.MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, this.metadata);
  39. this.MongoManager.setProjectMetaData = sinon.stub().callsArgWith(2);
  40. this.MongoManager.upgradeHistory = sinon.stub().callsArgWith(1);
  41. return this.WebApiManager.getProjectDetails = sinon.stub().callsArgWith(1, null, this.details);
  42. });
  43. describe("with preserveHistory set in the project meta data", function() {
  44. beforeEach(function() {
  45. this.metadata.preserveHistory = true;
  46. return this.UpdateTrimmer.shouldTrimUpdates(this.project_id, this.callback);
  47. });
  48. it("should look up the meta data", function() {
  49. return this.MongoManager.getProjectMetaData
  50. .calledWith(this.project_id)
  51. .should.equal(true);
  52. });
  53. it("should not look up the project details", function() {
  54. return this.WebApiManager.getProjectDetails
  55. .called
  56. .should.equal(false);
  57. });
  58. return it("should return false", function() {
  59. return this.callback.calledWith(null, false).should.equal(true);
  60. });
  61. });
  62. describe("without preserveHistory set in the project meta data", function() {
  63. beforeEach(function() {
  64. return this.metadata.preserveHistory = false;
  65. });
  66. describe("when the project has the versioning feature", function() {
  67. beforeEach(function() {
  68. this.details.features.versioning = true;
  69. return this.UpdateTrimmer.shouldTrimUpdates(this.project_id, this.callback);
  70. });
  71. it("should look up the meta data", function() {
  72. return this.MongoManager.getProjectMetaData
  73. .calledWith(this.project_id)
  74. .should.equal(true);
  75. });
  76. it("should look up the project details", function() {
  77. return this.WebApiManager.getProjectDetails
  78. .calledWith(this.project_id)
  79. .should.equal(true);
  80. });
  81. it("should insert preserveHistory into the metadata", function() {
  82. return this.MongoManager.setProjectMetaData
  83. .calledWith(this.project_id, {preserveHistory: true})
  84. .should.equal(true);
  85. });
  86. it("should upgrade any existing history", function() {
  87. return this.MongoManager.upgradeHistory
  88. .calledWith(this.project_id)
  89. .should.equal(true);
  90. });
  91. return it("should return false", function() {
  92. return this.callback.calledWith(null, false).should.equal(true);
  93. });
  94. });
  95. return describe("when the project does not have the versioning feature", function() {
  96. beforeEach(function() {
  97. this.details.features.versioning = false;
  98. return this.UpdateTrimmer.shouldTrimUpdates(this.project_id, this.callback);
  99. });
  100. return it("should return true", function() {
  101. return this.callback.calledWith(null, true).should.equal(true);
  102. });
  103. });
  104. });
  105. return describe("without any meta data", function() {
  106. beforeEach(function() {
  107. return this.MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, null);
  108. });
  109. describe("when the project has the versioning feature", function() {
  110. beforeEach(function() {
  111. this.details.features.versioning = true;
  112. return this.UpdateTrimmer.shouldTrimUpdates(this.project_id, this.callback);
  113. });
  114. it("should insert preserveHistory into the metadata", function() {
  115. return this.MongoManager.setProjectMetaData
  116. .calledWith(this.project_id, {preserveHistory: true})
  117. .should.equal(true);
  118. });
  119. it("should upgrade any existing history", function() {
  120. return this.MongoManager.upgradeHistory
  121. .calledWith(this.project_id)
  122. .should.equal(true);
  123. });
  124. return it("should return false", function() {
  125. return this.callback.calledWith(null, false).should.equal(true);
  126. });
  127. });
  128. return describe("when the project does not have the versioning feature", function() {
  129. beforeEach(function() {
  130. this.details.features.versioning = false;
  131. return this.UpdateTrimmer.shouldTrimUpdates(this.project_id, this.callback);
  132. });
  133. return it("should return true", function() {
  134. return this.callback.calledWith(null, true).should.equal(true);
  135. });
  136. });
  137. });
  138. });
  139. });