UpdateTrimmerTests.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, {
  24. requires: {
  25. 'logger-sharelatex': (this.logger = {
  26. log: sinon.stub(),
  27. error: sinon.stub()
  28. }),
  29. './WebApiManager': (this.WebApiManager = {}),
  30. './MongoManager': (this.MongoManager = {})
  31. }
  32. })
  33. this.callback = sinon.stub()
  34. return (this.project_id = 'mock-project-id')
  35. })
  36. afterEach(function () {
  37. return tk.reset()
  38. })
  39. return describe('shouldTrimUpdates', function () {
  40. beforeEach(function () {
  41. this.metadata = {}
  42. this.details = { features: {} }
  43. this.MongoManager.getProjectMetaData = sinon
  44. .stub()
  45. .callsArgWith(1, null, this.metadata)
  46. this.MongoManager.setProjectMetaData = sinon.stub().callsArgWith(2)
  47. this.MongoManager.upgradeHistory = sinon.stub().callsArgWith(1)
  48. return (this.WebApiManager.getProjectDetails = sinon
  49. .stub()
  50. .callsArgWith(1, null, this.details))
  51. })
  52. describe('with preserveHistory set in the project meta data', function () {
  53. beforeEach(function () {
  54. this.metadata.preserveHistory = true
  55. return this.UpdateTrimmer.shouldTrimUpdates(
  56. this.project_id,
  57. this.callback
  58. )
  59. })
  60. it('should look up the meta data', function () {
  61. return this.MongoManager.getProjectMetaData
  62. .calledWith(this.project_id)
  63. .should.equal(true)
  64. })
  65. it('should not look up the project details', function () {
  66. return this.WebApiManager.getProjectDetails.called.should.equal(false)
  67. })
  68. return it('should return false', function () {
  69. return this.callback.calledWith(null, false).should.equal(true)
  70. })
  71. })
  72. describe('without preserveHistory set in the project meta data', function () {
  73. beforeEach(function () {
  74. return (this.metadata.preserveHistory = false)
  75. })
  76. describe('when the project has the versioning feature', function () {
  77. beforeEach(function () {
  78. this.details.features.versioning = true
  79. return this.UpdateTrimmer.shouldTrimUpdates(
  80. this.project_id,
  81. this.callback
  82. )
  83. })
  84. it('should look up the meta data', function () {
  85. return this.MongoManager.getProjectMetaData
  86. .calledWith(this.project_id)
  87. .should.equal(true)
  88. })
  89. it('should look up the project details', function () {
  90. return this.WebApiManager.getProjectDetails
  91. .calledWith(this.project_id)
  92. .should.equal(true)
  93. })
  94. it('should insert preserveHistory into the metadata', function () {
  95. return this.MongoManager.setProjectMetaData
  96. .calledWith(this.project_id, { preserveHistory: true })
  97. .should.equal(true)
  98. })
  99. it('should upgrade any existing history', function () {
  100. return this.MongoManager.upgradeHistory
  101. .calledWith(this.project_id)
  102. .should.equal(true)
  103. })
  104. return it('should return false', function () {
  105. return this.callback.calledWith(null, false).should.equal(true)
  106. })
  107. })
  108. return describe('when the project does not have the versioning feature', function () {
  109. beforeEach(function () {
  110. this.details.features.versioning = false
  111. return this.UpdateTrimmer.shouldTrimUpdates(
  112. this.project_id,
  113. this.callback
  114. )
  115. })
  116. return it('should return true', function () {
  117. return this.callback.calledWith(null, true).should.equal(true)
  118. })
  119. })
  120. })
  121. return describe('without any meta data', function () {
  122. beforeEach(function () {
  123. return (this.MongoManager.getProjectMetaData = sinon
  124. .stub()
  125. .callsArgWith(1, null, null))
  126. })
  127. describe('when the project has the versioning feature', function () {
  128. beforeEach(function () {
  129. this.details.features.versioning = true
  130. return this.UpdateTrimmer.shouldTrimUpdates(
  131. this.project_id,
  132. this.callback
  133. )
  134. })
  135. it('should insert preserveHistory into the metadata', function () {
  136. return this.MongoManager.setProjectMetaData
  137. .calledWith(this.project_id, { preserveHistory: true })
  138. .should.equal(true)
  139. })
  140. it('should upgrade any existing history', function () {
  141. return this.MongoManager.upgradeHistory
  142. .calledWith(this.project_id)
  143. .should.equal(true)
  144. })
  145. return it('should return false', function () {
  146. return this.callback.calledWith(null, false).should.equal(true)
  147. })
  148. })
  149. return describe('when the project does not have the versioning feature', function () {
  150. beforeEach(function () {
  151. this.details.features.versioning = false
  152. return this.UpdateTrimmer.shouldTrimUpdates(
  153. this.project_id,
  154. this.callback
  155. )
  156. })
  157. return it('should return true', function () {
  158. return this.callback.calledWith(null, true).should.equal(true)
  159. })
  160. })
  161. })
  162. })
  163. })