ProjectUpdateHandlerTests.js 3.5 KB

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