ProjectUpdateHandlerTests.coffee 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. sinon = require('sinon')
  2. chai = require('chai').should()
  3. modulePath = "../../../../app/js/Features/Project/ProjectUpdateHandler.js"
  4. SandboxedModule = require('sandboxed-module')
  5. describe 'ProjectUpdateHandler', ->
  6. before ->
  7. @fakeTime = new Date()
  8. @clock = sinon.useFakeTimers(@fakeTime.getTime())
  9. beforeEach ->
  10. @ProjectModel = class Project
  11. @ProjectModel.update = sinon.stub().callsArg(3)
  12. @handler = SandboxedModule.require modulePath, requires:
  13. '../../models/Project':{Project:@ProjectModel}
  14. 'logger-sharelatex' : { log: sinon.stub() }
  15. after ->
  16. @clock.restore()
  17. describe 'marking a project as recently updated', ->
  18. beforeEach ->
  19. @project_id = "project_id"
  20. @lastUpdatedAt = 987654321
  21. @lastUpdatedBy = 'fake-last-updater-id'
  22. it 'should send an update to mongo', (done)->
  23. @handler.markAsUpdated @project_id, @lastUpdatedAt, @lastUpdatedBy, (err) =>
  24. sinon.assert.calledWith(
  25. @ProjectModel.update,
  26. {
  27. _id: @project_id,
  28. lastUpdated: { $lt: @lastUpdatedAt }
  29. },
  30. {
  31. lastUpdated: @lastUpdatedAt,
  32. lastUpdatedBy: @lastUpdatedBy
  33. }
  34. )
  35. done()
  36. it 'should set smart fallbacks', (done)->
  37. @handler.markAsUpdated @project_id, null, null, (err) =>
  38. sinon.assert.calledWithMatch(
  39. @ProjectModel.update,
  40. {
  41. _id: @project_id,
  42. lastUpdated: { $lt: @fakeTime }
  43. },
  44. {
  45. lastUpdated: @fakeTime
  46. lastUpdatedBy: null
  47. }
  48. )
  49. done()
  50. describe "markAsOpened", ->
  51. it 'should send an update to mongo', (done)->
  52. project_id = "project_id"
  53. @handler.markAsOpened project_id, (err)=>
  54. args = @ProjectModel.update.args[0]
  55. args[0]._id.should.equal project_id
  56. date = args[1].lastOpened+""
  57. now = Date.now()+""
  58. date.substring(0,5).should.equal now.substring(0,5)
  59. done()
  60. describe "markAsInactive", ->
  61. it 'should send an update to mongo', (done)->
  62. project_id = "project_id"
  63. @handler.markAsInactive project_id, (err)=>
  64. args = @ProjectModel.update.args[0]
  65. args[0]._id.should.equal project_id
  66. args[1].active.should.equal false
  67. done()
  68. describe "markAsActive", ->
  69. it 'should send an update to mongo', (done)->
  70. project_id = "project_id"
  71. @handler.markAsActive project_id, (err)=>
  72. args = @ProjectModel.update.args[0]
  73. args[0]._id.should.equal project_id
  74. args[1].active.should.equal true
  75. done()