UpdateTrimmerTests.js 5.7 KB

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