ChatControllerTests.coffee 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 = 'mock-user-id'
  11. @settings = {}
  12. @ChatApiHandler = {}
  13. @EditorRealTimeController =
  14. emitToRoom:sinon.stub().callsArgWith(3)
  15. @AuthenticationController =
  16. getLoggedInUserId: sinon.stub().returns(@user_id)
  17. @ChatController = SandboxedModule.require modulePath, requires:
  18. "settings-sharelatex": @settings
  19. "logger-sharelatex": log: ->
  20. "./ChatApiHandler": @ChatApiHandler
  21. "../Editor/EditorRealTimeController": @EditorRealTimeController
  22. '../Authentication/AuthenticationController': @AuthenticationController
  23. @req =
  24. params:
  25. project_id: @project_id
  26. @res =
  27. json: sinon.stub()
  28. send: sinon.stub()
  29. describe "sendMessage", ->
  30. beforeEach ->
  31. @req.body =
  32. content: @content = "message-content"
  33. @ChatApiHandler.sendGlobalMessage = sinon.stub().yields(null, @message = {"mock": "message"})
  34. @ChatController.sendMessage @req, @res
  35. it "should tell the chat handler about the message", ->
  36. @ChatApiHandler.sendGlobalMessage
  37. .calledWith(@project_id, @user_id, @content)
  38. .should.equal true
  39. it "should tell the editor real time controller about the update with the data from the chat handler", ->
  40. @EditorRealTimeController.emitToRoom
  41. .calledWith(@project_id, "new-chat-message", @message)
  42. .should.equal true
  43. it "should return a 204 status code", ->
  44. @res.send.calledWith(204).should.equal true
  45. describe "getMessages", ->
  46. beforeEach ->
  47. @req.query =
  48. limit: @limit = "30"
  49. before: @before = "12345"
  50. @ChatApiHandler.getGlobalMessages = sinon.stub().yields(null, @messages = ["mock", "messages"])
  51. @ChatController.getMessages @req, @res
  52. it "should ask the chat handler about the request", ->
  53. @ChatApiHandler.getGlobalMessages
  54. .calledWith(@project_id, @limit, @before)
  55. .should.equal true
  56. it "should return the messages", ->
  57. @res.json.calledWith(@messages).should.equal true