MetaControllerTests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const chai = require('chai')
  13. chai.should()
  14. const { expect } = chai
  15. const sinon = require('sinon')
  16. const modulePath = '../../../../app/src/Features/Metadata/MetaController'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('MetaController', function() {
  19. beforeEach(function() {
  20. this.projectId = 'somekindofid'
  21. this.EditorRealTimeController = {
  22. emitToRoom: sinon.stub()
  23. }
  24. this.MetaHandler = {
  25. getAllMetaForProject: sinon.stub(),
  26. getMetaForDoc: sinon.stub()
  27. }
  28. return (this.MetadataController = SandboxedModule.require(modulePath, {
  29. globals: {
  30. console: console
  31. },
  32. requires: {
  33. 'logger-sharelatex': {
  34. log: sinon.stub(),
  35. warn: sinon.stub(),
  36. err: sinon.stub()
  37. },
  38. '../Editor/EditorRealTimeController': this.EditorRealTimeController,
  39. './MetaHandler': this.MetaHandler
  40. }
  41. }))
  42. })
  43. describe('getMetadata', function() {
  44. beforeEach(function() {
  45. this.fakeLabels = { somedoc: ['a_label'] }
  46. this.MetaHandler.getAllMetaForProject = sinon
  47. .stub()
  48. .callsArgWith(1, null, this.fakeLabels)
  49. this.req = { params: { project_id: this.projectId } }
  50. this.res = { json: sinon.stub() }
  51. return (this.next = sinon.stub())
  52. })
  53. it('should call MetaHandler.getAllMetaForProject', function() {
  54. this.MetadataController.getMetadata(this.req, this.res, this.next)
  55. this.MetaHandler.getAllMetaForProject.callCount.should.equal(1)
  56. return this.MetaHandler.getAllMetaForProject
  57. .calledWith(this.projectId)
  58. .should.equal(true)
  59. })
  60. it('should call not call next with an error', function() {
  61. this.MetadataController.getMetadata(this.req, this.res, this.next)
  62. return this.next.callCount.should.equal(0)
  63. })
  64. it('should send a json response', function() {
  65. this.MetadataController.getMetadata(this.req, this.res, this.next)
  66. this.res.json.callCount.should.equal(1)
  67. return expect(this.res.json.lastCall.args[0]).to.have.all.keys([
  68. 'projectId',
  69. 'projectMeta'
  70. ])
  71. })
  72. describe('when MetaHandler.getAllMetaForProject produces an error', function() {
  73. beforeEach(function() {
  74. this.MetaHandler.getAllMetaForProject = sinon
  75. .stub()
  76. .callsArgWith(1, new Error('woops'))
  77. this.req = { params: { project_id: this.projectId } }
  78. this.res = { json: sinon.stub() }
  79. return (this.next = sinon.stub())
  80. })
  81. it('should call MetaHandler.getAllMetaForProject', function() {
  82. this.MetadataController.getMetadata(this.req, this.res, this.next)
  83. this.MetaHandler.getAllMetaForProject.callCount.should.equal(1)
  84. return this.MetaHandler.getAllMetaForProject
  85. .calledWith(this.projectId)
  86. .should.equal(true)
  87. })
  88. it('should call next with an error', function() {
  89. this.MetadataController.getMetadata(this.req, this.res, this.next)
  90. this.next.callCount.should.equal(1)
  91. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  92. })
  93. it('should not send a json response', function() {
  94. this.MetadataController.getMetadata(this.req, this.res, this.next)
  95. return this.res.json.callCount.should.equal(0)
  96. })
  97. })
  98. })
  99. describe('broadcastMetadataForDoc', function() {
  100. beforeEach(function() {
  101. this.MetaHandler.getMetaForDoc = sinon
  102. .stub()
  103. .callsArgWith(2, null, this.fakeLabels)
  104. this.EditorRealTimeController.emitToRoom = sinon.stub()
  105. this.docId = 'somedoc'
  106. this.req = { params: { project_id: this.projectId, doc_id: this.docId } }
  107. this.res = { sendStatus: sinon.stub() }
  108. return (this.next = sinon.stub())
  109. })
  110. it('should call MetaHandler.getMetaForDoc', function() {
  111. this.MetadataController.broadcastMetadataForDoc(
  112. this.req,
  113. this.res,
  114. this.next
  115. )
  116. this.MetaHandler.getMetaForDoc.callCount.should.equal(1)
  117. return this.MetaHandler.getMetaForDoc
  118. .calledWith(this.projectId)
  119. .should.equal(true)
  120. })
  121. it('should call not call next with an error', function() {
  122. this.MetadataController.broadcastMetadataForDoc(
  123. this.req,
  124. this.res,
  125. this.next
  126. )
  127. return this.next.callCount.should.equal(0)
  128. })
  129. it('should send a success response', function() {
  130. this.MetadataController.broadcastMetadataForDoc(
  131. this.req,
  132. this.res,
  133. this.next
  134. )
  135. this.res.sendStatus.callCount.should.equal(1)
  136. return this.res.sendStatus.calledWith(200).should.equal(true)
  137. })
  138. it('should emit a message to room', function() {
  139. this.MetadataController.broadcastMetadataForDoc(
  140. this.req,
  141. this.res,
  142. this.next
  143. )
  144. this.EditorRealTimeController.emitToRoom.callCount.should.equal(1)
  145. const { lastCall } = this.EditorRealTimeController.emitToRoom
  146. expect(lastCall.args[0]).to.equal(this.projectId)
  147. expect(lastCall.args[1]).to.equal('broadcastDocMeta')
  148. return expect(lastCall.args[2]).to.have.all.keys(['docId', 'meta'])
  149. })
  150. describe('when MetaHandler.getMetaForDoc produces an error', function() {
  151. beforeEach(function() {
  152. this.MetaHandler.getMetaForDoc = sinon
  153. .stub()
  154. .callsArgWith(2, new Error('woops'))
  155. this.EditorRealTimeController.emitToRoom = sinon.stub()
  156. this.docId = 'somedoc'
  157. this.req = {
  158. params: { project_id: this.projectId, doc_id: this.docId }
  159. }
  160. this.res = { json: sinon.stub() }
  161. return (this.next = sinon.stub())
  162. })
  163. it('should call MetaHandler.getMetaForDoc', function() {
  164. this.MetadataController.broadcastMetadataForDoc(
  165. this.req,
  166. this.res,
  167. this.next
  168. )
  169. this.MetaHandler.getMetaForDoc.callCount.should.equal(1)
  170. return this.MetaHandler.getMetaForDoc
  171. .calledWith(this.projectId)
  172. .should.equal(true)
  173. })
  174. it('should call next with an error', function() {
  175. this.MetadataController.broadcastMetadataForDoc(
  176. this.req,
  177. this.res,
  178. this.next
  179. )
  180. this.next.callCount.should.equal(1)
  181. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  182. })
  183. it('should not send a json response', function() {
  184. this.MetadataController.broadcastMetadataForDoc(
  185. this.req,
  186. this.res,
  187. this.next
  188. )
  189. return this.res.json.callCount.should.equal(0)
  190. })
  191. })
  192. })
  193. })