EditorRealTimeControllerTests.coffee 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor/EditorRealTimeController'
  5. describe "EditorRealTimeController", ->
  6. beforeEach ->
  7. @EditorRealTimeController = SandboxedModule.require modulePath, requires:
  8. "redis-sharelatex":
  9. createClient: () ->
  10. auth:->
  11. createMonitoredSubscriptionClient: () ->
  12. auth:->
  13. "../../infrastructure/Server" : io: @io = {}
  14. @EditorRealTimeController.rclientPub = publish: sinon.stub()
  15. @EditorRealTimeController.rclientSub =
  16. subscribe: sinon.stub()
  17. on: sinon.stub()
  18. @room_id = "room-id"
  19. @message = "message-to-editor"
  20. @payload = ["argument one", 42]
  21. describe "emitToRoom", ->
  22. beforeEach ->
  23. @EditorRealTimeController.emitToRoom(@room_id, @message, @payload...)
  24. it "should publish the message to redis", ->
  25. @EditorRealTimeController.rclientPub.publish
  26. .calledWith("editor-events", JSON.stringify(
  27. room_id: @room_id,
  28. message: @message
  29. payload: @payload
  30. ))
  31. .should.equal true
  32. describe "emitToAll", ->
  33. beforeEach ->
  34. @EditorRealTimeController.emitToRoom = sinon.stub()
  35. @EditorRealTimeController.emitToAll @message, @payload...
  36. it "should emit to the room 'all'", ->
  37. @EditorRealTimeController.emitToRoom
  38. .calledWith("all", @message, @payload...)
  39. .should.equal true
  40. describe "listenForEditorEvents", ->
  41. beforeEach ->
  42. @EditorRealTimeController._processEditorEvent = sinon.stub()
  43. @EditorRealTimeController.listenForEditorEvents()
  44. it "should subscribe to the editor-events channel", ->
  45. @EditorRealTimeController.rclientSub.subscribe
  46. .calledWith("editor-events")
  47. .should.equal true
  48. it "should process the events with _processEditorEvent", ->
  49. @EditorRealTimeController.rclientSub.on
  50. .calledWith("message", sinon.match.func)
  51. .should.equal true
  52. describe "_processEditorEvent", ->
  53. describe "with a designated room", ->
  54. beforeEach ->
  55. @io.sockets =
  56. in: sinon.stub().returns(emit: @emit = sinon.stub())
  57. data = JSON.stringify
  58. room_id: @room_id
  59. message: @message
  60. payload: @payload
  61. @EditorRealTimeController._processEditorEvent("editor-events", data)
  62. it "should send the message to all clients in the room", ->
  63. @io.sockets.in
  64. .calledWith(@room_id)
  65. .should.equal true
  66. @emit.calledWith(@message, @payload...).should.equal true
  67. describe "when emitting to all", ->
  68. beforeEach ->
  69. @io.sockets =
  70. emit: @emit = sinon.stub()
  71. data = JSON.stringify
  72. room_id: "all"
  73. message: @message
  74. payload: @payload
  75. @EditorRealTimeController._processEditorEvent("editor-events", data)
  76. it "should send the message to all clients", ->
  77. @emit.calledWith(@message, @payload...).should.equal true