ReferencesControllerTests.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. const { assert } = require('chai')
  16. const modulePath =
  17. '../../../../app/src/Features/References/ReferencesController'
  18. const MockRequest = require('../helpers/MockRequest')
  19. const MockResponse = require('../helpers/MockResponse')
  20. describe('ReferencesController', function () {
  21. beforeEach(function () {
  22. this.projectId = '2222'
  23. this.controller = SandboxedModule.require(modulePath, {
  24. requires: {
  25. '@overleaf/settings': (this.settings = {
  26. apis: { web: { url: 'http://some.url' } },
  27. }),
  28. './ReferencesHandler': (this.ReferencesHandler = {
  29. index: sinon.stub(),
  30. indexAll: sinon.stub(),
  31. }),
  32. '../Editor/EditorRealTimeController': (this.EditorRealTimeController = {
  33. emitToRoom: sinon.stub(),
  34. }),
  35. },
  36. })
  37. this.req = new MockRequest()
  38. this.req.params.Project_id = this.projectId
  39. this.req.body = {
  40. docIds: (this.docIds = ['aaa', 'bbb']),
  41. shouldBroadcast: false,
  42. }
  43. this.res = new MockResponse()
  44. this.res.json = sinon.stub()
  45. this.res.sendStatus = sinon.stub()
  46. this.next = sinon.stub()
  47. return (this.fakeResponseData = {
  48. projectId: this.projectId,
  49. keys: ['one', 'two', 'three'],
  50. })
  51. })
  52. describe('indexAll', function () {
  53. beforeEach(function () {
  54. this.req.body = { shouldBroadcast: false }
  55. this.ReferencesHandler.indexAll.callsArgWith(
  56. 1,
  57. null,
  58. this.fakeResponseData
  59. )
  60. return (this.call = callback => {
  61. this.controller.indexAll(this.req, this.res, this.next)
  62. return callback()
  63. })
  64. })
  65. it('should not produce an error', function (done) {
  66. return this.call(() => {
  67. this.res.sendStatus.callCount.should.equal(0)
  68. this.res.sendStatus.calledWith(500).should.equal(false)
  69. this.res.sendStatus.calledWith(400).should.equal(false)
  70. return done()
  71. })
  72. })
  73. it('should return data', function (done) {
  74. return this.call(() => {
  75. this.res.json.callCount.should.equal(1)
  76. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  77. return done()
  78. })
  79. })
  80. it('should call ReferencesHandler.indexAll', function (done) {
  81. return this.call(() => {
  82. this.ReferencesHandler.indexAll.callCount.should.equal(1)
  83. this.ReferencesHandler.indexAll
  84. .calledWith(this.projectId)
  85. .should.equal(true)
  86. return done()
  87. })
  88. })
  89. describe('when shouldBroadcast is true', function () {
  90. beforeEach(function () {
  91. this.ReferencesHandler.index.callsArgWith(
  92. 2,
  93. null,
  94. this.fakeResponseData
  95. )
  96. return (this.req.body.shouldBroadcast = true)
  97. })
  98. it('should call EditorRealTimeController.emitToRoom', function (done) {
  99. return this.call(() => {
  100. this.EditorRealTimeController.emitToRoom.callCount.should.equal(1)
  101. return done()
  102. })
  103. })
  104. it('should not produce an error', function (done) {
  105. return this.call(() => {
  106. this.res.sendStatus.callCount.should.equal(0)
  107. this.res.sendStatus.calledWith(500).should.equal(false)
  108. this.res.sendStatus.calledWith(400).should.equal(false)
  109. return done()
  110. })
  111. })
  112. it('should still return data', function (done) {
  113. return this.call(() => {
  114. this.res.json.callCount.should.equal(1)
  115. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  116. return done()
  117. })
  118. })
  119. })
  120. describe('when shouldBroadcast is false', function () {
  121. beforeEach(function () {
  122. this.ReferencesHandler.index.callsArgWith(
  123. 2,
  124. null,
  125. this.fakeResponseData
  126. )
  127. return (this.req.body.shouldBroadcast = false)
  128. })
  129. it('should not call EditorRealTimeController.emitToRoom', function (done) {
  130. return this.call(() => {
  131. this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
  132. return done()
  133. })
  134. })
  135. it('should not produce an error', function (done) {
  136. return this.call(() => {
  137. this.res.sendStatus.callCount.should.equal(0)
  138. this.res.sendStatus.calledWith(500).should.equal(false)
  139. this.res.sendStatus.calledWith(400).should.equal(false)
  140. return done()
  141. })
  142. })
  143. it('should still return data', function (done) {
  144. return this.call(() => {
  145. this.res.json.callCount.should.equal(1)
  146. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  147. return done()
  148. })
  149. })
  150. })
  151. })
  152. describe('there is no data', function () {
  153. beforeEach(function () {
  154. this.ReferencesHandler.indexAll.callsArgWith(1)
  155. return (this.call = callback => {
  156. this.controller.indexAll(this.req, this.res, this.next)
  157. return callback()
  158. })
  159. })
  160. it('should not call EditorRealTimeController.emitToRoom', function (done) {
  161. return this.call(() => {
  162. this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
  163. return done()
  164. })
  165. })
  166. it('should not produce an error', function (done) {
  167. return this.call(() => {
  168. this.res.sendStatus.callCount.should.equal(0)
  169. this.res.sendStatus.calledWith(500).should.equal(false)
  170. this.res.sendStatus.calledWith(400).should.equal(false)
  171. return done()
  172. })
  173. })
  174. it('should send a response with an empty keys list', function (done) {
  175. return this.call(() => {
  176. this.res.json.called.should.equal(true)
  177. this.res.json
  178. .calledWith({ projectId: this.projectId, keys: [] })
  179. .should.equal(true)
  180. return done()
  181. })
  182. })
  183. })
  184. })