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