ProjectUpdateHandlerTests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const sinon = require('sinon')
  2. const modulePath =
  3. '../../../../app/src/Features/Project/ProjectUpdateHandler.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. describe('ProjectUpdateHandler', function () {
  6. beforeEach(function () {
  7. this.fakeTime = new Date()
  8. this.clock = sinon.useFakeTimers(this.fakeTime.getTime())
  9. })
  10. afterEach(function () {
  11. this.clock.restore()
  12. })
  13. beforeEach(function () {
  14. this.ProjectModel = class Project {}
  15. this.ProjectModel.updateOne = sinon.stub().returns({
  16. exec: sinon.stub(),
  17. })
  18. this.handler = SandboxedModule.require(modulePath, {
  19. requires: {
  20. '../../models/Project': { Project: this.ProjectModel },
  21. },
  22. })
  23. })
  24. describe('marking a project as recently updated', function () {
  25. beforeEach(function () {
  26. this.project_id = 'project_id'
  27. this.lastUpdatedAt = 987654321
  28. this.lastUpdatedBy = 'fake-last-updater-id'
  29. })
  30. it('should send an update to mongo', async function () {
  31. await this.handler.promises.markAsUpdated(
  32. this.project_id,
  33. this.lastUpdatedAt,
  34. this.lastUpdatedBy
  35. )
  36. sinon.assert.calledWith(
  37. this.ProjectModel.updateOne,
  38. {
  39. _id: this.project_id,
  40. lastUpdated: { $lt: this.lastUpdatedAt },
  41. },
  42. {
  43. lastUpdated: this.lastUpdatedAt,
  44. lastUpdatedBy: this.lastUpdatedBy,
  45. }
  46. )
  47. })
  48. it('should set smart fallbacks', async function () {
  49. await this.handler.promises.markAsUpdated(this.project_id, null, null)
  50. sinon.assert.calledWithMatch(
  51. this.ProjectModel.updateOne,
  52. {
  53. _id: this.project_id,
  54. lastUpdated: { $lt: this.fakeTime },
  55. },
  56. {
  57. lastUpdated: this.fakeTime,
  58. lastUpdatedBy: null,
  59. }
  60. )
  61. })
  62. })
  63. describe('markAsOpened', function () {
  64. it('should send an update to mongo', async function () {
  65. const projectId = 'project_id'
  66. await this.handler.promises.markAsOpened(projectId)
  67. const args = this.ProjectModel.updateOne.args[0]
  68. args[0]._id.should.equal(projectId)
  69. const date = args[1].lastOpened + ''
  70. const now = Date.now() + ''
  71. date.substring(0, 5).should.equal(now.substring(0, 5))
  72. })
  73. })
  74. describe('markAsInactive', function () {
  75. it('should send an update to mongo', async function () {
  76. const projectId = 'project_id'
  77. await this.handler.promises.markAsInactive(projectId)
  78. const args = this.ProjectModel.updateOne.args[0]
  79. args[0]._id.should.equal(projectId)
  80. args[1].active.should.equal(false)
  81. })
  82. })
  83. describe('markAsActive', function () {
  84. it('should send an update to mongo', async function () {
  85. const projectId = 'project_id'
  86. await this.handler.promises.markAsActive(projectId)
  87. const args = this.ProjectModel.updateOne.args[0]
  88. args[0]._id.should.equal(projectId)
  89. args[1].active.should.equal(true)
  90. })
  91. })
  92. })