UpdateTrimmerTests.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/UpdateTrimmer.js"
  6. SandboxedModule = require('sandboxed-module')
  7. tk = require "timekeeper"
  8. describe "UpdateTrimmer", ->
  9. beforeEach ->
  10. @now = new Date()
  11. tk.freeze(@now)
  12. @UpdateTrimmer = SandboxedModule.require modulePath, requires:
  13. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  14. "./WebApiManager": @WebApiManager = {}
  15. "./MongoManager": @MongoManager = {}
  16. @callback = sinon.stub()
  17. @project_id = "mock-project-id"
  18. afterEach ->
  19. tk.reset()
  20. describe "shouldTrimUpdates", ->
  21. beforeEach ->
  22. @metadata = {}
  23. @details =
  24. features: {}
  25. @MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, @metadata)
  26. @MongoManager.setProjectMetaData = sinon.stub().callsArgWith(2)
  27. @WebApiManager.getProjectDetails = sinon.stub().callsArgWith(1, null, @details)
  28. describe "with preserveHistory set in the project meta data", ->
  29. beforeEach ->
  30. @metadata.preserveHistory = true
  31. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  32. it "should look up the meta data", ->
  33. @MongoManager.getProjectMetaData
  34. .calledWith(@project_id)
  35. .should.equal true
  36. it "should not look up the project details", ->
  37. @WebApiManager.getProjectDetails
  38. .called
  39. .should.equal false
  40. it "should return false", ->
  41. @callback.calledWith(null, false).should.equal true
  42. describe "without preserveHistory set in the project meta data", ->
  43. beforeEach ->
  44. @metadata.preserveHistory = false
  45. describe "when the project has the versioning feature", ->
  46. beforeEach ->
  47. @details.features.versioning = true
  48. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  49. it "should look up the meta data", ->
  50. @MongoManager.getProjectMetaData
  51. .calledWith(@project_id)
  52. .should.equal true
  53. it "should look up the project details", ->
  54. @WebApiManager.getProjectDetails
  55. .calledWith(@project_id)
  56. .should.equal true
  57. it "should insert preserveHistory into the metadata", ->
  58. @MongoManager.setProjectMetaData
  59. .calledWith(@project_id, {preserveHistory: true})
  60. .should.equal true
  61. it "should return false", ->
  62. @callback.calledWith(null, false).should.equal true
  63. describe "when the project does not have the versioning feature", ->
  64. beforeEach ->
  65. @details.features.versioning = false
  66. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  67. it "should return true", ->
  68. @callback.calledWith(null, true).should.equal true
  69. describe "without any meta data", ->
  70. beforeEach ->
  71. @MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, null)
  72. describe "when the project has the versioning feature", ->
  73. beforeEach ->
  74. @details.features.versioning = true
  75. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  76. it "should insert preserveHistory into the metadata", ->
  77. @MongoManager.setProjectMetaData
  78. .calledWith(@project_id, {preserveHistory: true})
  79. .should.equal true
  80. it "should return false", ->
  81. @callback.calledWith(null, false).should.equal true
  82. describe "when the project does not have the versioning feature", ->
  83. beforeEach ->
  84. @details.features.versioning = false
  85. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  86. it "should return true", ->
  87. @callback.calledWith(null, true).should.equal true