MetaControllerTests.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.res = { sendStatus: sinon.stub(), json: sinon.stub() }
  97. return (this.next = sinon.stub())
  98. })
  99. describe('with broadcast:true', function () {
  100. beforeEach(function () {
  101. this.req = {
  102. params: { project_id: this.projectId, doc_id: this.docId },
  103. body: { broadcast: true },
  104. }
  105. })
  106. it('should call MetaHandler.getMetaForDoc', function () {
  107. this.MetadataController.broadcastMetadataForDoc(
  108. this.req,
  109. this.res,
  110. this.next
  111. )
  112. this.MetaHandler.getMetaForDoc.callCount.should.equal(1)
  113. return this.MetaHandler.getMetaForDoc
  114. .calledWith(this.projectId)
  115. .should.equal(true)
  116. })
  117. it('should call not call next with an error', function () {
  118. this.MetadataController.broadcastMetadataForDoc(
  119. this.req,
  120. this.res,
  121. this.next
  122. )
  123. return this.next.callCount.should.equal(0)
  124. })
  125. it('should send a success response', function () {
  126. this.MetadataController.broadcastMetadataForDoc(
  127. this.req,
  128. this.res,
  129. this.next
  130. )
  131. this.res.sendStatus.callCount.should.equal(1)
  132. return this.res.sendStatus.calledWith(200).should.equal(true)
  133. })
  134. it('should emit a message to room', function () {
  135. this.MetadataController.broadcastMetadataForDoc(
  136. this.req,
  137. this.res,
  138. this.next
  139. )
  140. this.EditorRealTimeController.emitToRoom.callCount.should.equal(1)
  141. const { lastCall } = this.EditorRealTimeController.emitToRoom
  142. expect(lastCall.args[0]).to.equal(this.projectId)
  143. expect(lastCall.args[1]).to.equal('broadcastDocMeta')
  144. return expect(lastCall.args[2]).to.have.all.keys(['docId', 'meta'])
  145. })
  146. })
  147. describe('with broadcast:false', function () {
  148. beforeEach(function () {
  149. this.req = {
  150. params: { project_id: this.projectId, doc_id: this.docId },
  151. body: { broadcast: false },
  152. }
  153. })
  154. it('should call MetaHandler.getMetaForDoc', function () {
  155. this.MetadataController.broadcastMetadataForDoc(
  156. this.req,
  157. this.res,
  158. this.next
  159. )
  160. this.MetaHandler.getMetaForDoc.callCount.should.equal(1)
  161. return this.MetaHandler.getMetaForDoc
  162. .calledWith(this.projectId)
  163. .should.equal(true)
  164. })
  165. it('should call not call next with an error', function () {
  166. this.MetadataController.broadcastMetadataForDoc(
  167. this.req,
  168. this.res,
  169. this.next
  170. )
  171. return this.next.callCount.should.equal(0)
  172. })
  173. it('should send the metadata in the response', function () {
  174. this.MetadataController.broadcastMetadataForDoc(
  175. this.req,
  176. this.res,
  177. this.next
  178. )
  179. this.res.json.callCount.should.equal(1)
  180. return this.res.json
  181. .calledWith({ docId: this.docId, meta: this.fakeLabels })
  182. .should.equal(true)
  183. })
  184. it('should not emit a message to room', function () {
  185. this.MetadataController.broadcastMetadataForDoc(
  186. this.req,
  187. this.res,
  188. this.next
  189. )
  190. return this.EditorRealTimeController.emitToRoom.callCount.should.equal(
  191. 0
  192. )
  193. })
  194. })
  195. describe('when MetaHandler.getMetaForDoc produces an error', function () {
  196. beforeEach(function () {
  197. this.MetaHandler.getMetaForDoc = sinon
  198. .stub()
  199. .callsArgWith(2, new Error('woops'))
  200. this.EditorRealTimeController.emitToRoom = sinon.stub()
  201. this.docId = 'somedoc'
  202. this.req = {
  203. params: { project_id: this.projectId, doc_id: this.docId },
  204. body: { broadcast: true },
  205. }
  206. this.res = { json: sinon.stub() }
  207. return (this.next = sinon.stub())
  208. })
  209. it('should call MetaHandler.getMetaForDoc', function () {
  210. this.MetadataController.broadcastMetadataForDoc(
  211. this.req,
  212. this.res,
  213. this.next
  214. )
  215. this.MetaHandler.getMetaForDoc.callCount.should.equal(1)
  216. return this.MetaHandler.getMetaForDoc
  217. .calledWith(this.projectId)
  218. .should.equal(true)
  219. })
  220. it('should call next with an error', function () {
  221. this.MetadataController.broadcastMetadataForDoc(
  222. this.req,
  223. this.res,
  224. this.next
  225. )
  226. this.next.callCount.should.equal(1)
  227. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  228. })
  229. it('should not send a json response', function () {
  230. this.MetadataController.broadcastMetadataForDoc(
  231. this.req,
  232. this.res,
  233. this.next
  234. )
  235. return this.res.json.callCount.should.equal(0)
  236. })
  237. })
  238. })
  239. })