MetaController.test.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import esmock from 'esmock'
  4. import MockResponse from '../helpers/MockResponse.js'
  5. const modulePath = '../../../../app/src/Features/Metadata/MetaController.mjs'
  6. describe('MetaController', function () {
  7. beforeEach(async function () {
  8. this.EditorRealTimeController = {
  9. emitToRoom: sinon.stub(),
  10. }
  11. this.MetaHandler = {
  12. promises: {
  13. getAllMetaForProject: sinon.stub(),
  14. getMetaForDoc: sinon.stub(),
  15. },
  16. }
  17. this.MetadataController = await esmock.strict(modulePath, {
  18. '../../../../app/src/Features/Editor/EditorRealTimeController':
  19. this.EditorRealTimeController,
  20. '../../../../app/src/Features/Metadata/MetaHandler': this.MetaHandler,
  21. })
  22. })
  23. describe('getMetadata', function () {
  24. it('should respond with json', async function () {
  25. const projectMeta = {
  26. 'doc-id': {
  27. labels: ['foo'],
  28. packages: { a: { commands: [] } },
  29. packageNames: ['a'],
  30. },
  31. }
  32. this.MetaHandler.promises.getAllMetaForProject = sinon
  33. .stub()
  34. .resolves(projectMeta)
  35. const req = { params: { project_id: 'project-id' } }
  36. const res = new MockResponse()
  37. const next = sinon.stub()
  38. await this.MetadataController.getMetadata(req, res, next)
  39. this.MetaHandler.promises.getAllMetaForProject.should.have.been.calledWith(
  40. 'project-id'
  41. )
  42. res.json.should.have.been.calledOnceWith({
  43. projectId: 'project-id',
  44. projectMeta,
  45. })
  46. next.should.not.have.been.called
  47. })
  48. it('should handle an error', async function () {
  49. this.MetaHandler.promises.getAllMetaForProject = sinon
  50. .stub()
  51. .throws(new Error('woops'))
  52. const req = { params: { project_id: 'project-id' } }
  53. const res = new MockResponse()
  54. const next = sinon.stub()
  55. await this.MetadataController.getMetadata(req, res, next)
  56. this.MetaHandler.promises.getAllMetaForProject.should.have.been.calledWith(
  57. 'project-id'
  58. )
  59. res.json.should.not.have.been.called
  60. next.should.have.been.calledWithMatch(error => error instanceof Error)
  61. })
  62. })
  63. describe('broadcastMetadataForDoc', function () {
  64. it('should broadcast on broadcast:true ', async function () {
  65. this.MetaHandler.promises.getMetaForDoc = sinon.stub().resolves({
  66. labels: ['foo'],
  67. packages: { a: { commands: [] } },
  68. packageNames: ['a'],
  69. })
  70. this.EditorRealTimeController.emitToRoom = sinon.stub()
  71. const req = {
  72. params: { project_id: 'project-id', doc_id: 'doc-id' },
  73. body: { broadcast: true },
  74. }
  75. const res = new MockResponse()
  76. const next = sinon.stub()
  77. await this.MetadataController.broadcastMetadataForDoc(req, res, next)
  78. this.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  79. 'project-id'
  80. )
  81. res.json.should.not.have.been.called
  82. res.sendStatus.should.have.been.calledOnceWith(200)
  83. next.should.not.have.been.called
  84. this.EditorRealTimeController.emitToRoom.should.have.been.calledOnce
  85. const { lastCall } = this.EditorRealTimeController.emitToRoom
  86. expect(lastCall.args[0]).to.equal('project-id')
  87. expect(lastCall.args[1]).to.equal('broadcastDocMeta')
  88. expect(lastCall.args[2]).to.have.all.keys(['docId', 'meta'])
  89. })
  90. it('should return json on broadcast:false ', async function () {
  91. const docMeta = {
  92. labels: ['foo'],
  93. packages: { a: [] },
  94. packageNames: ['a'],
  95. }
  96. this.MetaHandler.promises.getMetaForDoc = sinon.stub().resolves(docMeta)
  97. this.EditorRealTimeController.emitToRoom = sinon.stub()
  98. const req = {
  99. params: { project_id: 'project-id', doc_id: 'doc-id' },
  100. body: { broadcast: false },
  101. }
  102. const res = new MockResponse()
  103. const next = sinon.stub()
  104. await this.MetadataController.broadcastMetadataForDoc(req, res, next)
  105. this.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  106. 'project-id'
  107. )
  108. this.EditorRealTimeController.emitToRoom.should.not.have.been.called
  109. res.json.should.have.been.calledOnceWith({
  110. docId: 'doc-id',
  111. meta: docMeta,
  112. })
  113. next.should.not.have.been.called
  114. })
  115. it('should handle an error', async function () {
  116. this.MetaHandler.promises.getMetaForDoc = sinon
  117. .stub()
  118. .throws(new Error('woops'))
  119. this.EditorRealTimeController.emitToRoom = sinon.stub()
  120. const req = {
  121. params: { project_id: 'project-id', doc_id: 'doc-id' },
  122. body: { broadcast: true },
  123. }
  124. const res = new MockResponse()
  125. const next = sinon.stub()
  126. await this.MetadataController.broadcastMetadataForDoc(req, res, next)
  127. this.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  128. 'project-id'
  129. )
  130. res.json.should.not.have.been.called
  131. next.should.have.been.calledWithMatch(error => error instanceof Error)
  132. })
  133. })
  134. })