RealTimeEventManagerTests.coffee 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = "../../../../app/js/Features/Sockets/RealTimeEventManager.js"
  5. describe "RealTimeEventManager", ->
  6. beforeEach ->
  7. @settings =
  8. redis:
  9. web:
  10. host: "host here"
  11. port: "port here"
  12. password: "password here"
  13. @RealTimeEventManager = SandboxedModule.require modulePath, requires:
  14. "redis":
  15. createClient: () ->
  16. auth:->
  17. "../../server" : io: @io = {}
  18. "settings-sharelatex":@settings
  19. @RealTimeEventManager.rclientPub = publish: sinon.stub()
  20. @RealTimeEventManager.rclientSub =
  21. subscribe: sinon.stub()
  22. on: sinon.stub()
  23. @room_id = "room-id-here"
  24. @message = "message-to-chat-here"
  25. @payload = ["argument one", 42]
  26. describe "emitToRoom", ->
  27. beforeEach ->
  28. @RealTimeEventManager.emitToRoom(@room_id, @message, @payload...)
  29. it "should publish the message to redis", ->
  30. @RealTimeEventManager.rclientPub.publish
  31. .calledWith("chat-events", JSON.stringify(
  32. room_id: @room_id,
  33. message: @message
  34. payload: @payload
  35. ))
  36. .should.equal true
  37. describe "listenForChatEvents", ->
  38. beforeEach ->
  39. @RealTimeEventManager._processEditorEvent = sinon.stub()
  40. @RealTimeEventManager.listenForChatEvents()
  41. it "should subscribe to the chat-events channel", ->
  42. @RealTimeEventManager.rclientSub.subscribe
  43. .calledWith("chat-events")
  44. .should.equal true
  45. it "should process the events with _processEditorEvent", ->
  46. @RealTimeEventManager.rclientSub.on
  47. .calledWith("message", sinon.match.func)
  48. .should.equal true
  49. describe "_processEditorEvent", ->
  50. describe "with a designated room", ->
  51. beforeEach ->
  52. @io.sockets =
  53. in: sinon.stub().returns(emit: @emit = sinon.stub())
  54. data = JSON.stringify
  55. room_id: @room_id
  56. message: @message
  57. payload: @payload
  58. @RealTimeEventManager._processEditorEvent("chat-events", data)
  59. it "should send the message to all clients in the room", ->
  60. @io.sockets.in
  61. .calledWith(@room_id)
  62. .should.equal true
  63. @emit.calledWith(@message, @payload...).should.equal true