setDocTests.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/DocumentManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "DocumentManager.setDoc", ->
  7. beforeEach ->
  8. @DocumentManager = SandboxedModule.require modulePath, requires:
  9. "./RedisManager": @RedisManager = {}
  10. "./PersistenceManager": @PersistenceManager = {}
  11. "./DiffCodec": @DiffCodec = {}
  12. "./DocOpsManager":{}
  13. "./UpdateManager": @UpdateManager = {}
  14. "logger-sharelatex": @logger = {log: sinon.stub()}
  15. "./Metrics": @Metrics =
  16. Timer: class Timer
  17. done: sinon.stub()
  18. "./TrackChangesManager": {}
  19. @project_id = "project-id-123"
  20. @doc_id = "doc-id-123"
  21. @version = 42
  22. @ops = ["mock-ops"]
  23. @callback = sinon.stub()
  24. @source = "dropbox"
  25. @user_id = "mock-user-id"
  26. describe "with plain tex lines", ->
  27. beforeEach ->
  28. @beforeLines = ["before", "lines"]
  29. @afterLines = ["after", "lines"]
  30. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, true)
  31. @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
  32. @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null)
  33. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
  34. @DocumentManager.flushAndDeleteDoc = sinon.stub().callsArg(2)
  35. describe "when already loaded", ->
  36. beforeEach ->
  37. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, @callback
  38. it "should get the current doc lines", ->
  39. @DocumentManager.getDoc
  40. .calledWith(@project_id, @doc_id)
  41. .should.equal true
  42. it "should return a diff of the old and new lines", ->
  43. @DiffCodec.diffAsShareJsOp
  44. .calledWith(@beforeLines, @afterLines)
  45. .should.equal true
  46. it "should apply the diff as a ShareJS op", ->
  47. @UpdateManager.applyUpdates
  48. .calledWith(
  49. @project_id,
  50. @doc_id,
  51. [
  52. doc: @doc_id,
  53. v: @version,
  54. op: @ops,
  55. meta: {
  56. type: "external"
  57. source: @source
  58. user_id: @user_id
  59. }
  60. ]
  61. )
  62. .should.equal true
  63. it "should flush the doc to Mongo", ->
  64. @DocumentManager.flushDocIfLoaded
  65. .calledWith(@project_id, @doc_id)
  66. .should.equal true
  67. it "should call the callback", ->
  68. @callback.calledWith(null).should.equal true
  69. it "should time the execution", ->
  70. @Metrics.Timer::done.called.should.equal true
  71. describe "when not already loaded", ->
  72. beforeEach ->
  73. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, false)
  74. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, @callback
  75. it "should flush and delete the doc from the doc updater", ->
  76. @DocumentManager.flushAndDeleteDoc
  77. .calledWith(@project_id, @doc_id)
  78. .should.equal true
  79. describe "without new lines", ->
  80. beforeEach ->
  81. @DocumentManager.setDoc @project_id, @doc_id, null, @source, @user_id, @callback
  82. it "should return the callback with an error", ->
  83. @callback.calledWith(new Error("No lines were passed to setDoc"))
  84. it "should not try to get the doc lines", ->
  85. @DocumentManager.getDoc.called.should.equal false