getPendingUpdatesForDocTests.coffee 1.7 KB

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