ProjectHistoryHandlerTests.js 5.2 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. promises: {},
  48. }),
  49. '../History/HistoryManager': (this.HistoryManager = {
  50. promises: {},
  51. }),
  52. './ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {
  53. promises: {},
  54. }),
  55. },
  56. }))
  57. })
  58. describe('starting history for an existing project', function () {
  59. beforeEach(async function () {
  60. this.HistoryManager.promises.initializeProject = sinon
  61. .stub()
  62. .resolves(this.historyId)
  63. this.HistoryManager.promises.flushProject = sinon.stub()
  64. return (this.ProjectEntityUpdateHandler.promises.resyncProjectHistory =
  65. sinon.stub())
  66. })
  67. describe('when the history does not already exist', function () {
  68. beforeEach(async function () {
  69. this.ProjectDetailsHandler.promises.getDetails = sinon
  70. .stub()
  71. .withArgs(projectId)
  72. .resolves(this.project)
  73. this.ProjectModel.updateOne = sinon.stub().resolves({ matchedCount: 1 })
  74. return this.ProjectHistoryHandler.promises.ensureHistoryExistsForProject(
  75. projectId
  76. )
  77. })
  78. it('should get any existing history id for the project', async function () {
  79. return this.ProjectDetailsHandler.promises.getDetails
  80. .calledWith(projectId)
  81. .should.equal(true)
  82. })
  83. it('should initialize a new history in the v1 history service', async function () {
  84. return this.HistoryManager.promises.initializeProject.called.should.equal(
  85. true
  86. )
  87. })
  88. it('should set the new history id on the project', async function () {
  89. return this.ProjectModel.updateOne
  90. .calledWith(
  91. { _id: projectId, 'overleaf.history.id': { $exists: false } },
  92. { 'overleaf.history.id': this.historyId }
  93. )
  94. .should.equal(true)
  95. })
  96. it('should resync the project history', async function () {
  97. return this.ProjectEntityUpdateHandler.promises.resyncProjectHistory
  98. .calledWith(projectId)
  99. .should.equal(true)
  100. })
  101. it('should flush the project history', async function () {
  102. return this.HistoryManager.promises.flushProject
  103. .calledWith(projectId)
  104. .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.promises.getDetails = sinon
  111. .stub()
  112. .withArgs(projectId)
  113. .resolves(this.project)
  114. this.ProjectModel.updateOne = sinon.stub().resolves({ matchedCount: 1 })
  115. return this.ProjectHistoryHandler.promises.ensureHistoryExistsForProject(
  116. projectId
  117. )
  118. })
  119. it('should get any existing history id for the project', async function () {
  120. return this.ProjectDetailsHandler.promises.getDetails
  121. .calledWith(projectId)
  122. .should.equal(true)
  123. })
  124. it('should not initialize a new history in the v1 history service', async function () {
  125. return this.HistoryManager.promises.initializeProject.called.should.equal(
  126. false
  127. )
  128. })
  129. it('should not set the new history id on the project', async function () {
  130. return this.ProjectModel.updateOne.called.should.equal(false)
  131. })
  132. it('should not resync the project history', async function () {
  133. return this.ProjectEntityUpdateHandler.promises.resyncProjectHistory.called.should.equal(
  134. false
  135. )
  136. })
  137. it('should not flush the project history', async function () {
  138. return this.HistoryManager.promises.flushProject.called.should.equal(
  139. false
  140. )
  141. })
  142. })
  143. })
  144. })