ProjectUpdateHandlerTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const chai = require('chai').should()
  17. const modulePath =
  18. '../../../../app/src/Features/Project/ProjectUpdateHandler.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('ProjectUpdateHandler', function() {
  21. beforeEach(function() {
  22. this.fakeTime = new Date()
  23. this.clock = sinon.useFakeTimers(this.fakeTime.getTime())
  24. })
  25. beforeEach(function() {
  26. let Project
  27. this.ProjectModel = Project = class Project {}
  28. this.ProjectModel.update = sinon.stub().callsArg(3)
  29. return (this.handler = SandboxedModule.require(modulePath, {
  30. globals: {
  31. console: console
  32. },
  33. requires: {
  34. '../../models/Project': { Project: this.ProjectModel },
  35. 'logger-sharelatex': { log: sinon.stub() }
  36. }
  37. }))
  38. })
  39. describe('marking a project as recently updated', function() {
  40. beforeEach(function() {
  41. this.project_id = 'project_id'
  42. this.lastUpdatedAt = 987654321
  43. return (this.lastUpdatedBy = 'fake-last-updater-id')
  44. })
  45. it('should send an update to mongo', function(done) {
  46. return this.handler.markAsUpdated(
  47. this.project_id,
  48. this.lastUpdatedAt,
  49. this.lastUpdatedBy,
  50. err => {
  51. sinon.assert.calledWith(
  52. this.ProjectModel.update,
  53. {
  54. _id: this.project_id,
  55. lastUpdated: { $lt: this.lastUpdatedAt }
  56. },
  57. {
  58. lastUpdated: this.lastUpdatedAt,
  59. lastUpdatedBy: this.lastUpdatedBy
  60. }
  61. )
  62. return done()
  63. }
  64. )
  65. })
  66. it('should set smart fallbacks', function(done) {
  67. return this.handler.markAsUpdated(this.project_id, null, null, err => {
  68. sinon.assert.calledWithMatch(
  69. this.ProjectModel.update,
  70. {
  71. _id: this.project_id,
  72. lastUpdated: { $lt: this.fakeTime }
  73. },
  74. {
  75. lastUpdated: this.fakeTime,
  76. lastUpdatedBy: null
  77. }
  78. )
  79. return done()
  80. })
  81. })
  82. })
  83. describe('markAsOpened', function() {
  84. it('should send an update to mongo', function(done) {
  85. const project_id = 'project_id'
  86. return this.handler.markAsOpened(project_id, err => {
  87. const args = this.ProjectModel.update.args[0]
  88. args[0]._id.should.equal(project_id)
  89. const date = args[1].lastOpened + ''
  90. const now = Date.now() + ''
  91. date.substring(0, 5).should.equal(now.substring(0, 5))
  92. return done()
  93. })
  94. })
  95. })
  96. describe('markAsInactive', function() {
  97. it('should send an update to mongo', function(done) {
  98. const project_id = 'project_id'
  99. return this.handler.markAsInactive(project_id, err => {
  100. const args = this.ProjectModel.update.args[0]
  101. args[0]._id.should.equal(project_id)
  102. args[1].active.should.equal(false)
  103. return done()
  104. })
  105. })
  106. })
  107. describe('markAsActive', function() {
  108. it('should send an update to mongo', function(done) {
  109. const project_id = 'project_id'
  110. return this.handler.markAsActive(project_id, err => {
  111. const args = this.ProjectModel.update.args[0]
  112. args[0]._id.should.equal(project_id)
  113. args[1].active.should.equal(true)
  114. return done()
  115. })
  116. })
  117. })
  118. })