ChatControllerTests.coffee 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/Chat/ChatController"
  7. expect = require("chai").expect
  8. describe "ChatController", ->
  9. beforeEach ->
  10. @settings = {}
  11. @ChatHandler =
  12. sendMessage:sinon.stub()
  13. getMessages:sinon.stub()
  14. @EditorRealTimeController =
  15. emitToRoom:sinon.stub().callsArgWith(3)
  16. @ChatController = SandboxedModule.require modulePath, requires:
  17. "settings-sharelatex":@settings
  18. "logger-sharelatex": log:->
  19. "./ChatHandler":@ChatHandler
  20. "../Editor/EditorRealTimeController":@EditorRealTimeController
  21. @query =
  22. before:"some time"
  23. @req =
  24. params:
  25. Project_id:@project_id
  26. session:
  27. user:
  28. _id:@user_id
  29. body:
  30. content:@messageContent
  31. @res =
  32. set:sinon.stub()
  33. describe "sendMessage", ->
  34. it "should tell the chat handler about the message", (done)->
  35. @ChatHandler.sendMessage.callsArgWith(3)
  36. @res.send = =>
  37. @ChatHandler.sendMessage.calledWith(@project_id, @user_id, @messageContent).should.equal true
  38. done()
  39. @ChatController.sendMessage @req, @res
  40. it "should tell the editor real time controller about the update with the data from the chat handler", (done)->
  41. @chatMessage =
  42. content:"hello world"
  43. @ChatHandler.sendMessage.callsArgWith(3, null, @chatMessage)
  44. @res.send = =>
  45. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "new-chat-message", @chatMessage).should.equal true
  46. done()
  47. @ChatController.sendMessage @req, @res
  48. describe "getMessages", ->
  49. beforeEach ->
  50. @req.query = @query
  51. it "should ask the chat handler about the request", (done)->
  52. @ChatHandler.getMessages.callsArgWith(2)
  53. @res.send = =>
  54. @ChatHandler.getMessages.calledWith(@project_id, @query).should.equal true
  55. done()
  56. @ChatController.getMessages @req, @res
  57. it "should return the messages", (done)->
  58. messages = [{content:"hello"}]
  59. @ChatHandler.getMessages.callsArgWith(2, null, messages)
  60. @res.send = (sentMessages)=>
  61. @res.set.calledWith('Content-Type', 'application/json').should.equal true
  62. sentMessages.should.deep.equal messages
  63. done()
  64. @ChatController.getMessages @req, @res