ChatControllerTests.coffee 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. describe "sendMessage", ->
  33. it "should tell the chat handler about the message", (done)->
  34. @ChatHandler.sendMessage.callsArgWith(3)
  35. @res.send = =>
  36. @ChatHandler.sendMessage.calledWith(@project_id, @user_id, @messageContent).should.equal true
  37. done()
  38. @ChatController.sendMessage @req, @res
  39. it "should tell the editor real time controller about the update with the data from the chat handler", (done)->
  40. @chatMessage =
  41. content:"hello world"
  42. @ChatHandler.sendMessage.callsArgWith(3, null, @chatMessage)
  43. @res.send = =>
  44. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "new-chat-message", @chatMessage).should.equal true
  45. done()
  46. @ChatController.sendMessage @req, @res
  47. describe "getMessages", ->
  48. beforeEach ->
  49. @req.query = @query
  50. it "should ask the chat handler about the request", (done)->
  51. @ChatHandler.getMessages.callsArgWith(2)
  52. @res.send = =>
  53. @ChatHandler.getMessages.calledWith(@project_id, @query).should.equal true
  54. done()
  55. @ChatController.getMessages @req, @res
  56. it "should return the messages", (done)->
  57. messages = [{content:"hello"}]
  58. @ChatHandler.getMessages.callsArgWith(2, null, messages)
  59. @res.send = (sentMessages)=>
  60. sentMessages.should.deep.equal messages
  61. done()
  62. @ChatController.getMessages @req, @res