ProjectHistoryHandlerTests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS206: Consider reworking classes to avoid initClass
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const { assert, expect } = require('chai')
  15. const sinon = require('sinon')
  16. const modulePath = '../../../../app/src/Features/Project/ProjectHistoryHandler'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('ProjectHistoryHandler', function () {
  19. const projectId = '4eecb1c1bffa66588e0000a1'
  20. const userId = 1234
  21. beforeEach(function () {
  22. let Project
  23. this.ProjectModel = Project = (function () {
  24. Project = class Project {
  25. static initClass() {
  26. this.prototype.rootFolder = [this.rootFolder]
  27. }
  28. constructor(options) {
  29. this._id = projectId
  30. this.name = 'project_name_here'
  31. this.rev = 0
  32. }
  33. }
  34. Project.initClass()
  35. return Project
  36. })()
  37. this.project = new this.ProjectModel()
  38. this.historyId = this.project._id.toString()
  39. this.callback = sinon.stub()
  40. return (this.ProjectHistoryHandler = SandboxedModule.require(modulePath, {
  41. requires: {
  42. '@overleaf/settings': (this.Settings = {}),
  43. '../../models/Project': {
  44. Project: this.ProjectModel,
  45. },
  46. './ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
  47. '../History/HistoryManager': (this.HistoryManager = {}),
  48. './ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {}),
  49. },
  50. }))
  51. })
  52. describe('starting history for an existing project', function () {
  53. beforeEach(function () {
  54. this.HistoryManager.initializeProject = sinon
  55. .stub()
  56. .yields(null, this.historyId)
  57. this.HistoryManager.flushProject = sinon.stub().callsArg(1)
  58. return (this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
  59. .stub()
  60. .callsArg(1))
  61. })
  62. describe('when the history does not already exist', function () {
  63. beforeEach(function () {
  64. this.ProjectDetailsHandler.getDetails = sinon
  65. .stub()
  66. .withArgs(projectId)
  67. .callsArgWith(1, null, this.project)
  68. this.ProjectModel.updateOne = sinon
  69. .stub()
  70. .callsArgWith(2, null, { matchedCount: 1 })
  71. return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
  72. projectId,
  73. this.callback
  74. )
  75. })
  76. it('should get any existing history id for the project', function () {
  77. return this.ProjectDetailsHandler.getDetails
  78. .calledWith(projectId)
  79. .should.equal(true)
  80. })
  81. it('should initialize a new history in the v1 history service', function () {
  82. return this.HistoryManager.initializeProject.called.should.equal(true)
  83. })
  84. it('should set the new history id on the project', function () {
  85. return this.ProjectModel.updateOne
  86. .calledWith(
  87. { _id: projectId, 'overleaf.history.id': { $exists: false } },
  88. { 'overleaf.history.id': this.historyId }
  89. )
  90. .should.equal(true)
  91. })
  92. it('should resync the project history', function () {
  93. return this.ProjectEntityUpdateHandler.resyncProjectHistory
  94. .calledWith(projectId)
  95. .should.equal(true)
  96. })
  97. it('should flush the project history', function () {
  98. return this.HistoryManager.flushProject
  99. .calledWith(projectId)
  100. .should.equal(true)
  101. })
  102. it('should call the callback without an error', function () {
  103. return this.callback.called.should.equal(true)
  104. })
  105. })
  106. describe('when the history already exists', function () {
  107. beforeEach(function () {
  108. this.project.overleaf = { history: { id: 1234 } }
  109. this.ProjectDetailsHandler.getDetails = sinon
  110. .stub()
  111. .withArgs(projectId)
  112. .callsArgWith(1, null, this.project)
  113. this.ProjectModel.updateOne = sinon.stub()
  114. return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
  115. projectId,
  116. this.callback
  117. )
  118. })
  119. it('should get any existing history id for the project', function () {
  120. return this.ProjectDetailsHandler.getDetails
  121. .calledWith(projectId)
  122. .should.equal(true)
  123. })
  124. it('should not initialize a new history in the v1 history service', function () {
  125. return this.HistoryManager.initializeProject.called.should.equal(false)
  126. })
  127. it('should not set the new history id on the project', function () {
  128. return this.ProjectModel.updateOne.called.should.equal(false)
  129. })
  130. it('should not resync the project history', function () {
  131. return this.ProjectEntityUpdateHandler.resyncProjectHistory.called.should.equal(
  132. false
  133. )
  134. })
  135. it('should not flush the project history', function () {
  136. return this.HistoryManager.flushProject.called.should.equal(false)
  137. })
  138. it('should call the callback', function () {
  139. return this.callback.calledWith().should.equal(true)
  140. })
  141. })
  142. })
  143. })