MetaControllerTests.coffee 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. chai = require('chai')
  2. chai.should()
  3. expect = chai.expect
  4. sinon = require("sinon")
  5. modulePath = "../../../../app/js/Features/Metadata/MetaController"
  6. SandboxedModule = require('sandboxed-module')
  7. describe 'MetaController', ->
  8. beforeEach ->
  9. @projectId = 'somekindofid'
  10. @EditorRealTimeController = {
  11. emitToRoom: sinon.stub()
  12. }
  13. @MetaHandler = {
  14. getAllMetaForProject: sinon.stub()
  15. getMetaForDoc: sinon.stub()
  16. }
  17. @MetadataController = SandboxedModule.require modulePath, requires:
  18. 'logger-sharelatex': {log: sinon.stub(), err: sinon.stub()}
  19. '../Editor/EditorRealTimeController': @EditorRealTimeController
  20. './MetaHandler': @MetaHandler
  21. describe 'getMetadata', ->
  22. beforeEach ->
  23. @fakeLabels = {'somedoc': ['a_label']}
  24. @MetaHandler.getAllMetaForProject = sinon.stub().callsArgWith(1, null, @fakeLabels)
  25. @req = {params: {project_id: @projectId}}
  26. @res = {json: sinon.stub()}
  27. @next = sinon.stub()
  28. it 'should call MetaHandler.getAllMetaForProject', () ->
  29. @MetadataController.getMetadata(@req, @res, @next)
  30. @MetaHandler.getAllMetaForProject.callCount.should.equal 1
  31. @MetaHandler.getAllMetaForProject.calledWith(@projectId).should.equal true
  32. it 'should call not call next with an error', () ->
  33. @MetadataController.getMetadata(@req, @res, @next)
  34. @next.callCount.should.equal 0
  35. it 'should send a json response', () ->
  36. @MetadataController.getMetadata(@req, @res, @next)
  37. @res.json.callCount.should.equal 1
  38. expect(@res.json.lastCall.args[0]).to.have.all.keys ['projectId', 'projectMeta']
  39. describe 'when MetaHandler.getAllMetaForProject produces an error', ->
  40. beforeEach ->
  41. @MetaHandler.getAllMetaForProject = sinon.stub().callsArgWith(1, new Error('woops'))
  42. @req = {params: {project_id: @projectId}}
  43. @res = {json: sinon.stub()}
  44. @next = sinon.stub()
  45. it 'should call MetaHandler.getAllMetaForProject', () ->
  46. @MetadataController.getMetadata(@req, @res, @next)
  47. @MetaHandler.getAllMetaForProject.callCount.should.equal 1
  48. @MetaHandler.getAllMetaForProject.calledWith(@projectId).should.equal true
  49. it 'should call next with an error', ->
  50. @MetadataController.getMetadata(@req, @res, @next)
  51. @next.callCount.should.equal 1
  52. expect(@next.lastCall.args[0]).to.be.instanceof Error
  53. it 'should not send a json response', ->
  54. @MetadataController.getMetadata(@req, @res, @next)
  55. @res.json.callCount.should.equal 0
  56. describe 'broadcastMetadataForDoc', ->
  57. beforeEach ->
  58. @MetaHandler.getMetaForDoc = sinon.stub().callsArgWith(2, null, @fakeLabels)
  59. @EditorRealTimeController.emitToRoom = sinon.stub()
  60. @docId = 'somedoc'
  61. @req = {params: {project_id: @projectId, doc_id: @docId}}
  62. @res = {sendStatus: sinon.stub()}
  63. @next = sinon.stub()
  64. it 'should call MetaHandler.getMetaForDoc', () ->
  65. @MetadataController.broadcastMetadataForDoc(@req, @res, @next)
  66. @MetaHandler.getMetaForDoc.callCount.should.equal 1
  67. @MetaHandler.getMetaForDoc.calledWith(@projectId).should.equal true
  68. it 'should call not call next with an error', () ->
  69. @MetadataController.broadcastMetadataForDoc(@req, @res, @next)
  70. @next.callCount.should.equal 0
  71. it 'should send a success response', () ->
  72. @MetadataController.broadcastMetadataForDoc(@req, @res, @next)
  73. @res.sendStatus.callCount.should.equal 1
  74. @res.sendStatus.calledWith(200).should.equal true
  75. it 'should emit a message to room', () ->
  76. @MetadataController.broadcastMetadataForDoc(@req, @res, @next)
  77. @EditorRealTimeController.emitToRoom.callCount.should.equal 1
  78. lastCall = @EditorRealTimeController.emitToRoom.lastCall
  79. expect(lastCall.args[0]).to.equal @projectId
  80. expect(lastCall.args[1]).to.equal 'broadcastDocMeta'
  81. expect(lastCall.args[2]).to.have.all.keys ['docId', 'meta']
  82. describe 'when MetaHandler.getMetaForDoc produces an error', ->
  83. beforeEach ->
  84. @MetaHandler.getMetaForDoc = sinon.stub().callsArgWith(2, new Error('woops'))
  85. @EditorRealTimeController.emitToRoom = sinon.stub()
  86. @docId = 'somedoc'
  87. @req = {params: {project_id: @projectId, doc_id: @docId}}
  88. @res = {json: sinon.stub()}
  89. @next = sinon.stub()
  90. it 'should call MetaHandler.getMetaForDoc', () ->
  91. @MetadataController.broadcastMetadataForDoc(@req, @res, @next)
  92. @MetaHandler.getMetaForDoc.callCount.should.equal 1
  93. @MetaHandler.getMetaForDoc.calledWith(@projectId).should.equal true
  94. it 'should call next with an error', ->
  95. @MetadataController.broadcastMetadataForDoc(@req, @res, @next)
  96. @next.callCount.should.equal 1
  97. expect(@next.lastCall.args[0]).to.be.instanceof Error
  98. it 'should not send a json response', ->
  99. @MetadataController.broadcastMetadataForDoc(@req, @res, @next)
  100. @res.json.callCount.should.equal 0