ChatControllerTests.coffee 2.4 KB

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