ProjectHistoryHandlerTests.js 5.3 KB

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