setDocTests.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. @project_id = "project-id-123"
  19. @doc_id = "doc-id-123"
  20. @version = 42
  21. @ops = ["mock-ops"]
  22. @callback = sinon.stub()
  23. @source = "dropbox"
  24. @user_id = "mock-user-id"
  25. describe "with plain tex lines", ->
  26. beforeEach ->
  27. @beforeLines = ["before", "lines"]
  28. @afterLines = ["after", "lines"]
  29. describe "successfully", ->
  30. beforeEach ->
  31. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version)
  32. @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
  33. @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null)
  34. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
  35. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, @callback
  36. it "should get the current doc lines", ->
  37. @DocumentManager.getDoc
  38. .calledWith(@project_id, @doc_id)
  39. .should.equal true
  40. it "should return a diff of the old and new lines", ->
  41. @DiffCodec.diffAsShareJsOp
  42. .calledWith(@beforeLines, @afterLines)
  43. .should.equal true
  44. it "should apply the diff as a ShareJS op", ->
  45. @UpdateManager.applyUpdates
  46. .calledWith(
  47. @project_id,
  48. @doc_id,
  49. [
  50. doc: @doc_id,
  51. v: @version,
  52. op: @ops,
  53. meta: {
  54. type: "external"
  55. source: @source
  56. user_id: @user_id
  57. }
  58. ]
  59. )
  60. .should.equal true
  61. it "should flush the doc to Mongo", ->
  62. @DocumentManager.flushDocIfLoaded
  63. .calledWith(@project_id, @doc_id)
  64. .should.equal true
  65. it "should call the callback", ->
  66. @callback.calledWith(null).should.equal true
  67. it "should time the execution", ->
  68. @Metrics.Timer::done.called.should.equal true
  69. describe "without new lines", ->
  70. beforeEach ->
  71. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version)
  72. @DocumentManager.setDoc @project_id, @doc_id, null, @callback
  73. it "should return teh callback with an error", ->
  74. @callback.calledWith(new Error("No lines were passed to setDoc"))
  75. it "should not try to get the doc lines", ->
  76. @DocumentManager.getDoc.called.should.equal false