ReferencesSearchControllerTests.coffee 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. sinon = require 'sinon'
  4. assert = require("chai").assert
  5. modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchController"
  6. MockRequest = require "../helpers/MockRequest"
  7. MockResponse = require "../helpers/MockResponse"
  8. describe "ReferencesSearchController", ->
  9. beforeEach ->
  10. @projectId = '2222'
  11. @controller = SandboxedModule.require modulePath, requires:
  12. 'logger-sharelatex': {
  13. log: ->
  14. err: ->
  15. },
  16. 'settings-sharelatex': @settings = {
  17. apis: {web: {url: 'http://some.url'}}
  18. },
  19. './ReferencesSearchHandler': @ReferencesSearchHandler = {
  20. index: sinon.stub()
  21. },
  22. '../Editor/EditorRealTimeController': @EditorRealTimeController = {
  23. emitToRoom: sinon.stub()
  24. }
  25. @req = new MockRequest()
  26. @req.params.Project_id = @projectId
  27. @req.body =
  28. docIds: @docIds = ['aaa', 'bbb']
  29. shouldBroadcast: false
  30. @res = new MockResponse()
  31. @res.json = sinon.stub()
  32. @res.send = sinon.stub()
  33. @fakeResponseData =
  34. projectId: @projectId,
  35. keys: ['one', 'two', 'three']
  36. describe 'index', ->
  37. describe 'with docIds as an array and shouldBroadcast as false', ->
  38. beforeEach ->
  39. @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
  40. @call = (callback) =>
  41. @controller.index @req, @res
  42. callback()
  43. it 'should call ReferencesSearchHandler.index', (done) ->
  44. @call () =>
  45. @ReferencesSearchHandler.index.callCount.should.equal 1
  46. @ReferencesSearchHandler.index.calledWith(@projectId, @docIds).should.equal true
  47. done()
  48. it 'should return data', (done) ->
  49. @call () =>
  50. @res.json.callCount.should.equal 1
  51. @res.json.calledWith(@fakeResponseData).should.equal true
  52. done()
  53. it 'should not produce an error', (done) ->
  54. @call () =>
  55. @res.send.callCount.should.equal 0
  56. @res.send.calledWith(500).should.equal false
  57. @res.send.calledWith(400).should.equal false
  58. done()
  59. it 'should not call EditorRealTimController.emitToRoom', (done) ->
  60. @call () =>
  61. @EditorRealTimeController.emitToRoom.callCount.should.equal 0
  62. done()
  63. describe 'with docIds set to ALL', ->
  64. beforeEach ->
  65. @req.body.docIds = 'ALL'
  66. it 'should still pass the "ALL" value to handler', (done) ->
  67. @call () =>
  68. @ReferencesSearchHandler.index.callCount.should.equal 1
  69. @ReferencesSearchHandler.index.calledWith(@projectId, 'ALL').should.equal true
  70. done()
  71. it 'should not produce an error', (done) ->
  72. @call () =>
  73. @res.send.callCount.should.equal 0
  74. @res.send.calledWith(500).should.equal false
  75. @res.send.calledWith(400).should.equal false
  76. done()
  77. describe 'when ReferencesSearchHandler.index produces an error', ->
  78. beforeEach ->
  79. @ReferencesSearchHandler.index.callsArgWith(2, new Error('woops'), null)
  80. it 'should produce an error response', (done) ->
  81. @call () =>
  82. @res.send.callCount.should.equal 1
  83. @res.send.calledWith(500).should.equal true
  84. done()
  85. describe 'when shouldBroadcast is true', ->
  86. beforeEach ->
  87. @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
  88. @req.body.shouldBroadcast = true
  89. it 'should call EditorRealTimeController.emitToRoom', (done) ->
  90. @call () =>
  91. @EditorRealTimeController.emitToRoom.callCount.should.equal 1
  92. done()
  93. it 'should not produce an error', (done) ->
  94. @call () =>
  95. @res.send.callCount.should.equal 0
  96. @res.send.calledWith(500).should.equal false
  97. @res.send.calledWith(400).should.equal false
  98. done()
  99. it 'should still return data', (done) ->
  100. @call () =>
  101. @res.json.callCount.should.equal 1
  102. @res.json.calledWith(@fakeResponseData).should.equal true
  103. done()
  104. describe 'with missing docIds', ->
  105. beforeEach ->
  106. delete @req.body.docIds
  107. it 'should produce an error response', (done) ->
  108. @call () =>
  109. @res.send.callCount.should.equal 1
  110. @res.send.calledWith(400).should.equal true
  111. done()
  112. it 'should not call ReferencesSearchHandler.index', (done) ->
  113. @call () =>
  114. @ReferencesSearchHandler.index.callCount.should.equal 0
  115. done()
  116. describe 'with invalid docIds', ->
  117. beforeEach ->
  118. @req.body.docIds = 42
  119. it 'should produce an error response', (done) ->
  120. @call () =>
  121. @res.send.callCount.should.equal 1
  122. @res.send.calledWith(400).should.equal true
  123. done()
  124. it 'should not call ReferencesSearchHandler.index', (done) ->
  125. @call () =>
  126. @ReferencesSearchHandler.index.callCount.should.equal 0
  127. done()