| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- SandboxedModule = require('sandboxed-module')
- should = require('chai').should()
- sinon = require 'sinon'
- assert = require("chai").assert
- modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchController"
- MockRequest = require "../helpers/MockRequest"
- MockResponse = require "../helpers/MockResponse"
- describe "ReferencesSearchController", ->
- beforeEach ->
- @projectId = '2222'
- @controller = SandboxedModule.require modulePath, requires:
- 'logger-sharelatex': {
- log: ->
- err: ->
- },
- 'settings-sharelatex': @settings = {
- apis: {web: {url: 'http://some.url'}}
- },
- './ReferencesSearchHandler': @ReferencesSearchHandler = {
- index: sinon.stub()
- indexAll: sinon.stub()
- },
- '../Editor/EditorRealTimeController': @EditorRealTimeController = {
- emitToRoom: sinon.stub()
- }
- @req = new MockRequest()
- @req.params.Project_id = @projectId
- @req.body =
- docIds: @docIds = ['aaa', 'bbb']
- shouldBroadcast: false
- @res = new MockResponse()
- @res.json = sinon.stub()
- @res.send = sinon.stub()
- @fakeResponseData =
- projectId: @projectId,
- keys: ['one', 'two', 'three']
- describe 'indexAll', ->
- beforeEach ->
- @req.body = {shouldBroadcast: false}
- @ReferencesSearchHandler.indexAll.callsArgWith(1, null, @fakeResponseData)
- @call = (callback) =>
- @controller.indexAll @req, @res
- callback()
- it 'should not produce an error', (done) ->
- @call () =>
- @res.send.callCount.should.equal 0
- @res.send.calledWith(500).should.equal false
- @res.send.calledWith(400).should.equal false
- done()
- it 'should return data', (done) ->
- @call () =>
- @res.json.callCount.should.equal 1
- @res.json.calledWith(@fakeResponseData).should.equal true
- done()
- it 'should call ReferencesSearchHandler.indexAll', (done) ->
- @call () =>
- @ReferencesSearchHandler.indexAll.callCount.should.equal 1
- @ReferencesSearchHandler.indexAll.calledWith(@projectId).should.equal true
- done()
- describe 'when shouldBroadcast is true', ->
- beforeEach ->
- @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
- @req.body.shouldBroadcast = true
- it 'should call EditorRealTimeController.emitToRoom', (done) ->
- @call () =>
- @EditorRealTimeController.emitToRoom.callCount.should.equal 1
- done()
- it 'should not produce an error', (done) ->
- @call () =>
- @res.send.callCount.should.equal 0
- @res.send.calledWith(500).should.equal false
- @res.send.calledWith(400).should.equal false
- done()
- it 'should still return data', (done) ->
- @call () =>
- @res.json.callCount.should.equal 1
- @res.json.calledWith(@fakeResponseData).should.equal true
- done()
- describe 'when shouldBroadcast is false', ->
- beforeEach ->
- @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
- @req.body.shouldBroadcast = false
- it 'should not call EditorRealTimeController.emitToRoom', (done) ->
- @call () =>
- @EditorRealTimeController.emitToRoom.callCount.should.equal 0
- done()
- it 'should not produce an error', (done) ->
- @call () =>
- @res.send.callCount.should.equal 0
- @res.send.calledWith(500).should.equal false
- @res.send.calledWith(400).should.equal false
- done()
- it 'should still return data', (done) ->
- @call () =>
- @res.json.callCount.should.equal 1
- @res.json.calledWith(@fakeResponseData).should.equal true
- done()
- describe 'index', ->
- describe 'with docIds as an array and shouldBroadcast as false', ->
- beforeEach ->
- @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
- @call = (callback) =>
- @controller.index @req, @res
- callback()
- it 'should call ReferencesSearchHandler.index', (done) ->
- @call () =>
- @ReferencesSearchHandler.index.callCount.should.equal 1
- @ReferencesSearchHandler.index.calledWith(@projectId, @docIds).should.equal true
- done()
- it 'should return data', (done) ->
- @call () =>
- @res.json.callCount.should.equal 1
- @res.json.calledWith(@fakeResponseData).should.equal true
- done()
- it 'should not produce an error', (done) ->
- @call () =>
- @res.send.callCount.should.equal 0
- @res.send.calledWith(500).should.equal false
- @res.send.calledWith(400).should.equal false
- done()
- it 'should not call EditorRealTimController.emitToRoom', (done) ->
- @call () =>
- @EditorRealTimeController.emitToRoom.callCount.should.equal 0
- done()
- describe 'when ReferencesSearchHandler.index produces an error', ->
- beforeEach ->
- @ReferencesSearchHandler.index.callsArgWith(2, new Error('woops'), null)
- it 'should produce an error response', (done) ->
- @call () =>
- @res.send.callCount.should.equal 1
- @res.send.calledWith(500).should.equal true
- done()
- describe 'when shouldBroadcast is true', ->
- beforeEach ->
- @ReferencesSearchHandler.index.callsArgWith(2, null, @fakeResponseData)
- @req.body.shouldBroadcast = true
- it 'should call EditorRealTimeController.emitToRoom', (done) ->
- @call () =>
- @EditorRealTimeController.emitToRoom.callCount.should.equal 1
- done()
- it 'should not produce an error', (done) ->
- @call () =>
- @res.send.callCount.should.equal 0
- @res.send.calledWith(500).should.equal false
- @res.send.calledWith(400).should.equal false
- done()
- it 'should still return data', (done) ->
- @call () =>
- @res.json.callCount.should.equal 1
- @res.json.calledWith(@fakeResponseData).should.equal true
- done()
- describe 'with missing docIds', ->
- beforeEach ->
- delete @req.body.docIds
- it 'should produce an error response', (done) ->
- @call () =>
- @res.send.callCount.should.equal 1
- @res.send.calledWith(400).should.equal true
- done()
- it 'should not call ReferencesSearchHandler.index', (done) ->
- @call () =>
- @ReferencesSearchHandler.index.callCount.should.equal 0
- done()
- describe 'with invalid docIds', ->
- beforeEach ->
- @req.body.docIds = 42
- it 'should produce an error response', (done) ->
- @call () =>
- @res.send.callCount.should.equal 1
- @res.send.calledWith(400).should.equal true
- done()
- it 'should not call ReferencesSearchHandler.index', (done) ->
- @call () =>
- @ReferencesSearchHandler.index.callCount.should.equal 0
- done()
|