2017-06-08 04:22:26 -04:00
|
|
|
chai = require('chai')
|
|
|
|
chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
sinon = require("sinon")
|
|
|
|
modulePath = "../../../../app/js/Features/Labels/LabelsController"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
|
|
|
|
describe 'LabelsController', ->
|
|
|
|
beforeEach ->
|
|
|
|
@projectId = 'somekindofid'
|
|
|
|
@EditorRealTimeController = {
|
|
|
|
emitToRoom: sinon.stub()
|
|
|
|
}
|
|
|
|
@LabelsHandler = {
|
|
|
|
getAllLabelsForProject: sinon.stub()
|
|
|
|
getLabelsForDoc: sinon.stub()
|
|
|
|
}
|
|
|
|
@LabelsController = SandboxedModule.require modulePath, requires:
|
|
|
|
'logger-sharelatex': {log: sinon.stub(), err: sinon.stub()}
|
|
|
|
'../Editor/EditorRealTimeController': @EditorRealTimeController
|
|
|
|
'./LabelsHandler': @LabelsHandler
|
|
|
|
|
|
|
|
describe 'getAllLabels', ->
|
|
|
|
beforeEach ->
|
|
|
|
@fakeLabels = {'somedoc': ['a_label']}
|
|
|
|
@LabelsHandler.getAllLabelsForProject = sinon.stub().callsArgWith(1, null, @fakeLabels)
|
2017-06-12 06:37:05 -04:00
|
|
|
@req = {params: {project_id: @projectId}}
|
2017-06-08 04:22:26 -04:00
|
|
|
@res = {json: sinon.stub()}
|
|
|
|
@next = sinon.stub()
|
|
|
|
|
|
|
|
it 'should call LabelsHandler.getAllLabelsForProject', () ->
|
|
|
|
@LabelsController.getAllLabels(@req, @res, @next)
|
|
|
|
@LabelsHandler.getAllLabelsForProject.callCount.should.equal 1
|
|
|
|
@LabelsHandler.getAllLabelsForProject.calledWith(@projectId).should.equal true
|
|
|
|
|
|
|
|
it 'should call not call next with an error', () ->
|
|
|
|
@LabelsController.getAllLabels(@req, @res, @next)
|
|
|
|
@next.callCount.should.equal 0
|
|
|
|
|
|
|
|
it 'should send a json response', () ->
|
|
|
|
@LabelsController.getAllLabels(@req, @res, @next)
|
|
|
|
@res.json.callCount.should.equal 1
|
|
|
|
expect(@res.json.lastCall.args[0]).to.have.all.keys ['projectId', 'projectLabels']
|
|
|
|
|
|
|
|
describe 'when LabelsHandler.getAllLabelsForProject produces an error', ->
|
|
|
|
beforeEach ->
|
|
|
|
@LabelsHandler.getAllLabelsForProject = sinon.stub().callsArgWith(1, new Error('woops'))
|
2017-06-12 06:37:05 -04:00
|
|
|
@req = {params: {project_id: @projectId}}
|
2017-06-08 04:22:26 -04:00
|
|
|
@res = {json: sinon.stub()}
|
|
|
|
@next = sinon.stub()
|
|
|
|
|
|
|
|
it 'should call LabelsHandler.getAllLabelsForProject', () ->
|
|
|
|
@LabelsController.getAllLabels(@req, @res, @next)
|
|
|
|
@LabelsHandler.getAllLabelsForProject.callCount.should.equal 1
|
|
|
|
@LabelsHandler.getAllLabelsForProject.calledWith(@projectId).should.equal true
|
|
|
|
|
|
|
|
it 'should call next with an error', ->
|
|
|
|
@LabelsController.getAllLabels(@req, @res, @next)
|
|
|
|
@next.callCount.should.equal 1
|
|
|
|
expect(@next.lastCall.args[0]).to.be.instanceof Error
|
|
|
|
|
|
|
|
it 'should not send a json response', ->
|
|
|
|
@LabelsController.getAllLabels(@req, @res, @next)
|
|
|
|
@res.json.callCount.should.equal 0
|
|
|
|
|
2017-06-13 06:38:15 -04:00
|
|
|
describe 'broadcastLabelsForDoc', ->
|
2017-06-08 04:22:26 -04:00
|
|
|
beforeEach ->
|
|
|
|
@LabelsHandler.getLabelsForDoc = sinon.stub().callsArgWith(2, null, @fakeLabels)
|
|
|
|
@EditorRealTimeController.emitToRoom = sinon.stub()
|
|
|
|
@docId = 'somedoc'
|
2017-06-12 06:37:05 -04:00
|
|
|
@req = {params: {project_id: @projectId, doc_id: @docId}}
|
2017-06-13 06:38:15 -04:00
|
|
|
@res = {sendStatus: sinon.stub()}
|
2017-06-08 04:22:26 -04:00
|
|
|
@next = sinon.stub()
|
|
|
|
|
|
|
|
it 'should call LabelsHandler.getLabelsForDoc', () ->
|
2017-06-13 06:38:15 -04:00
|
|
|
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
|
2017-06-08 04:22:26 -04:00
|
|
|
@LabelsHandler.getLabelsForDoc.callCount.should.equal 1
|
|
|
|
@LabelsHandler.getLabelsForDoc.calledWith(@projectId).should.equal true
|
|
|
|
|
|
|
|
it 'should call not call next with an error', () ->
|
2017-06-13 06:38:15 -04:00
|
|
|
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
|
2017-06-08 04:22:26 -04:00
|
|
|
@next.callCount.should.equal 0
|
|
|
|
|
2017-06-13 06:38:15 -04:00
|
|
|
it 'should send a success response', () ->
|
|
|
|
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
|
|
|
|
@res.sendStatus.callCount.should.equal 1
|
|
|
|
@res.sendStatus.calledWith(200).should.equal true
|
2017-06-08 04:22:26 -04:00
|
|
|
|
|
|
|
it 'should emit a message to room', () ->
|
2017-06-13 06:38:15 -04:00
|
|
|
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
|
2017-06-08 04:22:26 -04:00
|
|
|
@EditorRealTimeController.emitToRoom.callCount.should.equal 1
|
|
|
|
lastCall = @EditorRealTimeController.emitToRoom.lastCall
|
|
|
|
expect(lastCall.args[0]).to.equal @projectId
|
2017-06-12 08:06:56 -04:00
|
|
|
expect(lastCall.args[1]).to.equal 'broadcastDocLabels'
|
2017-06-08 04:22:26 -04:00
|
|
|
expect(lastCall.args[2]).to.have.all.keys ['docId', 'labels']
|
|
|
|
|
|
|
|
describe 'when LabelsHandler.getLabelsForDoc produces an error', ->
|
|
|
|
beforeEach ->
|
|
|
|
@LabelsHandler.getLabelsForDoc = sinon.stub().callsArgWith(2, new Error('woops'))
|
|
|
|
@EditorRealTimeController.emitToRoom = sinon.stub()
|
|
|
|
@docId = 'somedoc'
|
2017-06-12 06:37:05 -04:00
|
|
|
@req = {params: {project_id: @projectId, doc_id: @docId}}
|
2017-06-08 04:22:26 -04:00
|
|
|
@res = {json: sinon.stub()}
|
|
|
|
@next = sinon.stub()
|
|
|
|
|
|
|
|
it 'should call LabelsHandler.getLabelsForDoc', () ->
|
2017-06-13 06:38:15 -04:00
|
|
|
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
|
2017-06-08 04:22:26 -04:00
|
|
|
@LabelsHandler.getLabelsForDoc.callCount.should.equal 1
|
|
|
|
@LabelsHandler.getLabelsForDoc.calledWith(@projectId).should.equal true
|
|
|
|
|
|
|
|
it 'should call next with an error', ->
|
2017-06-13 06:38:15 -04:00
|
|
|
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
|
2017-06-08 04:22:26 -04:00
|
|
|
@next.callCount.should.equal 1
|
|
|
|
expect(@next.lastCall.args[0]).to.be.instanceof Error
|
|
|
|
|
|
|
|
it 'should not send a json response', ->
|
2017-06-13 06:38:15 -04:00
|
|
|
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
|
2017-06-08 04:22:26 -04:00
|
|
|
@res.json.callCount.should.equal 0
|