ReferencesController.test.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import esmock from 'esmock'
  2. import sinon from 'sinon'
  3. import MockRequest from '../helpers/MockRequest.js'
  4. import MockResponse from '../helpers/MockResponse.js'
  5. const modulePath =
  6. '../../../../app/src/Features/References/ReferencesController'
  7. describe('ReferencesController', function () {
  8. beforeEach(async function () {
  9. this.projectId = '2222'
  10. this.controller = await esmock.strict(modulePath, {
  11. '@overleaf/settings': (this.settings = {
  12. apis: { web: { url: 'http://some.url' } },
  13. }),
  14. '../../../../app/src/Features/References/ReferencesHandler':
  15. (this.ReferencesHandler = {
  16. index: sinon.stub(),
  17. indexAll: sinon.stub(),
  18. }),
  19. '../../../../app/src/Features/Editor/EditorRealTimeController':
  20. (this.EditorRealTimeController = {
  21. emitToRoom: sinon.stub(),
  22. }),
  23. })
  24. this.req = new MockRequest()
  25. this.req.params.Project_id = this.projectId
  26. this.req.body = {
  27. docIds: (this.docIds = ['aaa', 'bbb']),
  28. shouldBroadcast: false,
  29. }
  30. this.res = new MockResponse()
  31. this.res.json = sinon.stub()
  32. this.res.sendStatus = sinon.stub()
  33. this.next = sinon.stub()
  34. this.fakeResponseData = {
  35. projectId: this.projectId,
  36. keys: ['one', 'two', 'three'],
  37. }
  38. })
  39. describe('indexAll', function () {
  40. beforeEach(function () {
  41. this.req.body = { shouldBroadcast: false }
  42. this.ReferencesHandler.indexAll.callsArgWith(
  43. 1,
  44. null,
  45. this.fakeResponseData
  46. )
  47. this.call = callback => {
  48. this.controller.indexAll(this.req, this.res, this.next)
  49. return callback()
  50. }
  51. })
  52. it('should not produce an error', function (done) {
  53. this.call(() => {
  54. this.res.sendStatus.callCount.should.equal(0)
  55. this.res.sendStatus.calledWith(500).should.equal(false)
  56. this.res.sendStatus.calledWith(400).should.equal(false)
  57. done()
  58. })
  59. })
  60. it('should return data', function (done) {
  61. this.call(() => {
  62. this.res.json.callCount.should.equal(1)
  63. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  64. done()
  65. })
  66. })
  67. it('should call ReferencesHandler.indexAll', function (done) {
  68. this.call(() => {
  69. this.ReferencesHandler.indexAll.callCount.should.equal(1)
  70. this.ReferencesHandler.indexAll
  71. .calledWith(this.projectId)
  72. .should.equal(true)
  73. done()
  74. })
  75. })
  76. describe('when shouldBroadcast is true', function () {
  77. beforeEach(function () {
  78. this.ReferencesHandler.index.callsArgWith(
  79. 2,
  80. null,
  81. this.fakeResponseData
  82. )
  83. this.req.body.shouldBroadcast = true
  84. })
  85. it('should call EditorRealTimeController.emitToRoom', function (done) {
  86. this.call(() => {
  87. this.EditorRealTimeController.emitToRoom.callCount.should.equal(1)
  88. done()
  89. })
  90. })
  91. it('should not produce an error', function (done) {
  92. this.call(() => {
  93. this.res.sendStatus.callCount.should.equal(0)
  94. this.res.sendStatus.calledWith(500).should.equal(false)
  95. this.res.sendStatus.calledWith(400).should.equal(false)
  96. done()
  97. })
  98. })
  99. it('should still return data', function (done) {
  100. this.call(() => {
  101. this.res.json.callCount.should.equal(1)
  102. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  103. done()
  104. })
  105. })
  106. })
  107. describe('when shouldBroadcast is false', function () {
  108. beforeEach(function () {
  109. this.ReferencesHandler.index.callsArgWith(
  110. 2,
  111. null,
  112. this.fakeResponseData
  113. )
  114. this.req.body.shouldBroadcast = false
  115. })
  116. it('should not call EditorRealTimeController.emitToRoom', function (done) {
  117. this.call(() => {
  118. this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
  119. done()
  120. })
  121. })
  122. it('should not produce an error', function (done) {
  123. this.call(() => {
  124. this.res.sendStatus.callCount.should.equal(0)
  125. this.res.sendStatus.calledWith(500).should.equal(false)
  126. this.res.sendStatus.calledWith(400).should.equal(false)
  127. done()
  128. })
  129. })
  130. it('should still return data', function (done) {
  131. this.call(() => {
  132. this.res.json.callCount.should.equal(1)
  133. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  134. done()
  135. })
  136. })
  137. })
  138. })
  139. describe('there is no data', function () {
  140. beforeEach(function () {
  141. this.ReferencesHandler.indexAll.callsArgWith(1)
  142. this.call = callback => {
  143. this.controller.indexAll(this.req, this.res, this.next)
  144. callback()
  145. }
  146. })
  147. it('should not call EditorRealTimeController.emitToRoom', function (done) {
  148. this.call(() => {
  149. this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
  150. done()
  151. })
  152. })
  153. it('should not produce an error', function (done) {
  154. this.call(() => {
  155. this.res.sendStatus.callCount.should.equal(0)
  156. this.res.sendStatus.calledWith(500).should.equal(false)
  157. this.res.sendStatus.calledWith(400).should.equal(false)
  158. done()
  159. })
  160. })
  161. it('should send a response with an empty keys list', function (done) {
  162. this.call(() => {
  163. this.res.json.called.should.equal(true)
  164. this.res.json
  165. .calledWith({ projectId: this.projectId, keys: [] })
  166. .should.equal(true)
  167. done()
  168. })
  169. })
  170. })
  171. })