getPendingUpdatesForDocTests.coffee 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/RedisManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "RedisManager.getPendingUpdatesForDoc", ->
  7. beforeEach ->
  8. @RedisManager = SandboxedModule.require modulePath, requires:
  9. "./ZipManager": {}
  10. "redis-sharelatex": createClient: () =>
  11. @rclient =
  12. auth: () ->
  13. multi: () => @rclient
  14. "logger-sharelatex": @logger = {log: sinon.stub()}
  15. @project_id = "project-id-123"
  16. @doc_id = "doc-id-123"
  17. @callback = sinon.stub()
  18. @rclient.lrange = sinon.stub()
  19. @rclient.del = sinon.stub()
  20. describe "successfully", ->
  21. beforeEach ->
  22. @updates = [
  23. { op: [{ i: "foo", p: 4 }] }
  24. { op: [{ i: "foo", p: 4 }] }
  25. ]
  26. @jsonUpdates = @updates.map (update) -> JSON.stringify update
  27. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  28. @RedisManager.getPendingUpdatesForDoc @doc_id, @callback
  29. it "should get the pending updates", ->
  30. @rclient.lrange
  31. .calledWith("PendingUpdates:#{@doc_id}", 0, -1)
  32. .should.equal true
  33. it "should delete the pending updates", ->
  34. @rclient.del
  35. .calledWith("PendingUpdates:#{@doc_id}")
  36. .should.equal true
  37. it "should call the callback with the updates", ->
  38. @callback.calledWith(null, @updates).should.equal true
  39. describe "when the JSON doesn't parse", ->
  40. beforeEach ->
  41. @jsonUpdates = [
  42. JSON.stringify { op: [{ i: "foo", p: 4 }] }
  43. "broken json"
  44. ]
  45. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  46. @RedisManager.getPendingUpdatesForDoc @doc_id, @callback
  47. it "should return an error to the callback", ->
  48. @callback.calledWith(new Error("JSON parse error")).should.equal true