ProjectUpdateHandler.test.mjs 2.9 KB

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