ProjectUpdateHandlerTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* eslint-disable
  2. n/handle-callback-err,
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const modulePath =
  16. '../../../../app/src/Features/Project/ProjectUpdateHandler.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('ProjectUpdateHandler', function () {
  19. beforeEach(function () {
  20. this.fakeTime = new Date()
  21. this.clock = sinon.useFakeTimers(this.fakeTime.getTime())
  22. })
  23. afterEach(function () {
  24. this.clock.restore()
  25. })
  26. beforeEach(function () {
  27. let Project
  28. this.ProjectModel = Project = class Project {}
  29. this.ProjectModel.updateOne = sinon.stub().callsArg(3)
  30. return (this.handler = SandboxedModule.require(modulePath, {
  31. requires: {
  32. '../../models/Project': { Project: this.ProjectModel },
  33. },
  34. }))
  35. })
  36. describe('marking a project as recently updated', function () {
  37. beforeEach(function () {
  38. this.project_id = 'project_id'
  39. this.lastUpdatedAt = 987654321
  40. return (this.lastUpdatedBy = 'fake-last-updater-id')
  41. })
  42. it('should send an update to mongo', function (done) {
  43. return this.handler.markAsUpdated(
  44. this.project_id,
  45. this.lastUpdatedAt,
  46. this.lastUpdatedBy,
  47. err => {
  48. sinon.assert.calledWith(
  49. this.ProjectModel.updateOne,
  50. {
  51. _id: this.project_id,
  52. lastUpdated: { $lt: this.lastUpdatedAt },
  53. },
  54. {
  55. lastUpdated: this.lastUpdatedAt,
  56. lastUpdatedBy: this.lastUpdatedBy,
  57. }
  58. )
  59. return done()
  60. }
  61. )
  62. })
  63. it('should set smart fallbacks', function (done) {
  64. return this.handler.markAsUpdated(this.project_id, null, null, err => {
  65. sinon.assert.calledWithMatch(
  66. this.ProjectModel.updateOne,
  67. {
  68. _id: this.project_id,
  69. lastUpdated: { $lt: this.fakeTime },
  70. },
  71. {
  72. lastUpdated: this.fakeTime,
  73. lastUpdatedBy: null,
  74. }
  75. )
  76. return done()
  77. })
  78. })
  79. })
  80. describe('markAsOpened', function () {
  81. it('should send an update to mongo', function (done) {
  82. const projectId = 'project_id'
  83. return this.handler.markAsOpened(projectId, err => {
  84. const args = this.ProjectModel.updateOne.args[0]
  85. args[0]._id.should.equal(projectId)
  86. const date = args[1].lastOpened + ''
  87. const now = Date.now() + ''
  88. date.substring(0, 5).should.equal(now.substring(0, 5))
  89. return done()
  90. })
  91. })
  92. })
  93. describe('markAsInactive', function () {
  94. it('should send an update to mongo', function (done) {
  95. const projectId = 'project_id'
  96. return this.handler.markAsInactive(projectId, err => {
  97. const args = this.ProjectModel.updateOne.args[0]
  98. args[0]._id.should.equal(projectId)
  99. args[1].active.should.equal(false)
  100. return done()
  101. })
  102. })
  103. })
  104. describe('markAsActive', function () {
  105. it('should send an update to mongo', function (done) {
  106. const projectId = 'project_id'
  107. return this.handler.markAsActive(projectId, err => {
  108. const args = this.ProjectModel.updateOne.args[0]
  109. args[0]._id.should.equal(projectId)
  110. args[1].active.should.equal(true)
  111. return done()
  112. })
  113. })
  114. })
  115. })