MetaControllerTests.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 { expect } = require('chai')
  13. const sinon = require('sinon')
  14. const modulePath = '../../../../app/src/Features/Metadata/MetaController'
  15. const SandboxedModule = require('sandboxed-module')
  16. describe('MetaController', function() {
  17. beforeEach(function() {
  18. this.projectId = 'somekindofid'
  19. this.EditorRealTimeController = {
  20. emitToRoom: sinon.stub()
  21. }
  22. this.MetaHandler = {
  23. getAllMetaForProject: sinon.stub(),
  24. getMetaForDoc: sinon.stub()
  25. }
  26. return (this.MetadataController = SandboxedModule.require(modulePath, {
  27. requires: {
  28. '../Editor/EditorRealTimeController': this.EditorRealTimeController,
  29. './MetaHandler': this.MetaHandler
  30. }
  31. }))
  32. })
  33. describe('getMetadata', function() {
  34. beforeEach(function() {
  35. this.fakeLabels = { somedoc: ['a_label'] }
  36. this.MetaHandler.getAllMetaForProject = sinon
  37. .stub()
  38. .callsArgWith(1, null, this.fakeLabels)
  39. this.req = { params: { project_id: this.projectId } }
  40. this.res = { json: sinon.stub() }
  41. return (this.next = sinon.stub())
  42. })
  43. it('should call MetaHandler.getAllMetaForProject', function() {
  44. this.MetadataController.getMetadata(this.req, this.res, this.next)
  45. this.MetaHandler.getAllMetaForProject.callCount.should.equal(1)
  46. return this.MetaHandler.getAllMetaForProject
  47. .calledWith(this.projectId)
  48. .should.equal(true)
  49. })
  50. it('should call not call next with an error', function() {
  51. this.MetadataController.getMetadata(this.req, this.res, this.next)
  52. return this.next.callCount.should.equal(0)
  53. })
  54. it('should send a json response', function() {
  55. this.MetadataController.getMetadata(this.req, this.res, this.next)
  56. this.res.json.callCount.should.equal(1)
  57. return expect(this.res.json.lastCall.args[0]).to.have.all.keys([
  58. 'projectId',
  59. 'projectMeta'
  60. ])
  61. })
  62. describe('when MetaHandler.getAllMetaForProject produces an error', function() {
  63. beforeEach(function() {
  64. this.MetaHandler.getAllMetaForProject = sinon
  65. .stub()
  66. .callsArgWith(1, new Error('woops'))
  67. this.req = { params: { project_id: this.projectId } }
  68. this.res = { json: sinon.stub() }
  69. return (this.next = sinon.stub())
  70. })
  71. it('should call MetaHandler.getAllMetaForProject', function() {
  72. this.MetadataController.getMetadata(this.req, this.res, this.next)
  73. this.MetaHandler.getAllMetaForProject.callCount.should.equal(1)
  74. return this.MetaHandler.getAllMetaForProject
  75. .calledWith(this.projectId)
  76. .should.equal(true)
  77. })
  78. it('should call next with an error', function() {
  79. this.MetadataController.getMetadata(this.req, this.res, this.next)
  80. this.next.callCount.should.equal(1)
  81. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  82. })
  83. it('should not send a json response', function() {
  84. this.MetadataController.getMetadata(this.req, this.res, this.next)
  85. return this.res.json.callCount.should.equal(0)
  86. })
  87. })
  88. })
  89. describe('broadcastMetadataForDoc', function() {
  90. beforeEach(function() {
  91. this.MetaHandler.getMetaForDoc = sinon
  92. .stub()
  93. .callsArgWith(2, null, this.fakeLabels)
  94. this.EditorRealTimeController.emitToRoom = sinon.stub()
  95. this.docId = 'somedoc'
  96. this.req = { params: { project_id: this.projectId, doc_id: this.docId } }
  97. this.res = { sendStatus: sinon.stub() }
  98. return (this.next = sinon.stub())
  99. })
  100. it('should call MetaHandler.getMetaForDoc', function() {
  101. this.MetadataController.broadcastMetadataForDoc(
  102. this.req,
  103. this.res,
  104. this.next
  105. )
  106. this.MetaHandler.getMetaForDoc.callCount.should.equal(1)
  107. return this.MetaHandler.getMetaForDoc
  108. .calledWith(this.projectId)
  109. .should.equal(true)
  110. })
  111. it('should call not call next with an error', function() {
  112. this.MetadataController.broadcastMetadataForDoc(
  113. this.req,
  114. this.res,
  115. this.next
  116. )
  117. return this.next.callCount.should.equal(0)
  118. })
  119. it('should send a success response', function() {
  120. this.MetadataController.broadcastMetadataForDoc(
  121. this.req,
  122. this.res,
  123. this.next
  124. )
  125. this.res.sendStatus.callCount.should.equal(1)
  126. return this.res.sendStatus.calledWith(200).should.equal(true)
  127. })
  128. it('should emit a message to room', function() {
  129. this.MetadataController.broadcastMetadataForDoc(
  130. this.req,
  131. this.res,
  132. this.next
  133. )
  134. this.EditorRealTimeController.emitToRoom.callCount.should.equal(1)
  135. const { lastCall } = this.EditorRealTimeController.emitToRoom
  136. expect(lastCall.args[0]).to.equal(this.projectId)
  137. expect(lastCall.args[1]).to.equal('broadcastDocMeta')
  138. return expect(lastCall.args[2]).to.have.all.keys(['docId', 'meta'])
  139. })
  140. describe('when MetaHandler.getMetaForDoc produces an error', function() {
  141. beforeEach(function() {
  142. this.MetaHandler.getMetaForDoc = sinon
  143. .stub()
  144. .callsArgWith(2, new Error('woops'))
  145. this.EditorRealTimeController.emitToRoom = sinon.stub()
  146. this.docId = 'somedoc'
  147. this.req = {
  148. params: { project_id: this.projectId, doc_id: this.docId }
  149. }
  150. this.res = { json: sinon.stub() }
  151. return (this.next = sinon.stub())
  152. })
  153. it('should call MetaHandler.getMetaForDoc', function() {
  154. this.MetadataController.broadcastMetadataForDoc(
  155. this.req,
  156. this.res,
  157. this.next
  158. )
  159. this.MetaHandler.getMetaForDoc.callCount.should.equal(1)
  160. return this.MetaHandler.getMetaForDoc
  161. .calledWith(this.projectId)
  162. .should.equal(true)
  163. })
  164. it('should call next with an error', function() {
  165. this.MetadataController.broadcastMetadataForDoc(
  166. this.req,
  167. this.res,
  168. this.next
  169. )
  170. this.next.callCount.should.equal(1)
  171. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  172. })
  173. it('should not send a json response', function() {
  174. this.MetadataController.broadcastMetadataForDoc(
  175. this.req,
  176. this.res,
  177. this.next
  178. )
  179. return this.res.json.callCount.should.equal(0)
  180. })
  181. })
  182. })
  183. })