EditorRealTimeControllerTests.coffee 2.7 KB

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