GetSnapshotTests.coffee 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/ShareJsDB.js"
  6. SandboxedModule = require('sandboxed-module')
  7. Errors = require "../../../../app/js/Errors"
  8. describe "ShareJsDB.getSnapshot", ->
  9. beforeEach ->
  10. @doc_id = "document-id"
  11. @project_id = "project-id"
  12. @doc_key = "#{@project_id}:#{@doc_id}"
  13. @callback = sinon.stub()
  14. @ShareJsDB = SandboxedModule.require modulePath, requires:
  15. "./DocumentManager": @DocumentManager = {}
  16. "./RedisManager": {}
  17. "./DocOpsManager": {}
  18. "logger-sharelatex": {}
  19. @version = 42
  20. describe "with a text document", ->
  21. beforeEach ->
  22. @lines = ["one", "two", "three"]
  23. describe "successfully", ->
  24. beforeEach ->
  25. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
  26. @ShareJsDB.getSnapshot @doc_key, @callback
  27. it "should get the doc", ->
  28. @DocumentManager.getDoc
  29. .calledWith(@project_id, @doc_id)
  30. .should.equal true
  31. it "should return the doc lines", ->
  32. @callback.args[0][1].snapshot.should.equal @lines.join("\n")
  33. it "should return the doc version", ->
  34. @callback.args[0][1].v.should.equal @version
  35. it "should return the type as text", ->
  36. @callback.args[0][1].type.should.equal "text"
  37. describe "when the doclines do not exist", ->
  38. beforeEach ->
  39. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, null, null)
  40. @ShareJsDB.getSnapshot @doc_key, @callback
  41. it "should return the callback with a NotFoundError", ->
  42. @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
  43. describe "when getDoc returns an error", ->
  44. beforeEach ->
  45. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, @error = new Error("oops"), null, null)
  46. @ShareJsDB.getSnapshot @doc_key, @callback
  47. it "should return the callback with an error", ->
  48. @callback.calledWith(@error).should.equal true
  49. describe "with a JSON document", ->
  50. beforeEach ->
  51. @lines = [{text: "one"}, {text:"two"}, {text:"three"}]
  52. describe "successfully", ->
  53. beforeEach ->
  54. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
  55. @ShareJsDB.getSnapshot @doc_key, @callback
  56. it "should get the doc", ->
  57. @DocumentManager.getDoc
  58. .calledWith(@project_id, @doc_id)
  59. .should.equal true
  60. it "should return the doc lines", ->
  61. expect(@callback.args[0][1].snapshot).to.deep.equal lines: @lines
  62. it "should return the doc version", ->
  63. @callback.args[0][1].v.should.equal @version
  64. it "should return the type as text", ->
  65. @callback.args[0][1].type.should.equal "json"