LeaveDocTests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const chai = require('chai')
  17. const { expect } = chai
  18. chai.should()
  19. const sinon = require('sinon')
  20. const RealTimeClient = require('./helpers/RealTimeClient')
  21. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  22. const FixturesManager = require('./helpers/FixturesManager')
  23. const logger = require('logger-sharelatex')
  24. const async = require('async')
  25. describe('leaveDoc', function () {
  26. before(function () {
  27. this.lines = ['test', 'doc', 'lines']
  28. this.version = 42
  29. this.ops = ['mock', 'doc', 'ops']
  30. sinon.spy(logger, 'error')
  31. sinon.spy(logger, 'warn')
  32. sinon.spy(logger, 'log')
  33. return (this.other_doc_id = FixturesManager.getRandomId())
  34. })
  35. after(function () {
  36. logger.error.restore() // remove the spy
  37. logger.warn.restore()
  38. return logger.log.restore()
  39. })
  40. return describe('when joined to a doc', function () {
  41. beforeEach(function (done) {
  42. return async.series(
  43. [
  44. (cb) => {
  45. return FixturesManager.setUpProject(
  46. {
  47. privilegeLevel: 'readAndWrite'
  48. },
  49. (e, { project_id, user_id }) => {
  50. this.project_id = project_id
  51. this.user_id = user_id
  52. return cb(e)
  53. }
  54. )
  55. },
  56. (cb) => {
  57. return FixturesManager.setUpDoc(
  58. this.project_id,
  59. { lines: this.lines, version: this.version, ops: this.ops },
  60. (e, { doc_id }) => {
  61. this.doc_id = doc_id
  62. return cb(e)
  63. }
  64. )
  65. },
  66. (cb) => {
  67. this.client = RealTimeClient.connect()
  68. return this.client.on('connectionAccepted', cb)
  69. },
  70. (cb) => {
  71. return this.client.emit(
  72. 'joinProject',
  73. { project_id: this.project_id },
  74. cb
  75. )
  76. },
  77. (cb) => {
  78. return this.client.emit(
  79. 'joinDoc',
  80. this.doc_id,
  81. (error, ...rest) => {
  82. ;[...this.returnedArgs] = Array.from(rest)
  83. return cb(error)
  84. }
  85. )
  86. }
  87. ],
  88. done
  89. )
  90. })
  91. describe('then leaving the doc', function () {
  92. beforeEach(function (done) {
  93. return this.client.emit('leaveDoc', this.doc_id, (error) => {
  94. if (error != null) {
  95. throw error
  96. }
  97. return done()
  98. })
  99. })
  100. return it('should have left the doc room', function (done) {
  101. return RealTimeClient.getConnectedClient(
  102. this.client.socket.sessionid,
  103. (error, client) => {
  104. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
  105. false
  106. )
  107. return done()
  108. }
  109. )
  110. })
  111. })
  112. describe('when sending a leaveDoc request before the previous joinDoc request has completed', function () {
  113. beforeEach(function (done) {
  114. this.client.emit('leaveDoc', this.doc_id, () => {})
  115. this.client.emit('joinDoc', this.doc_id, () => {})
  116. return this.client.emit('leaveDoc', this.doc_id, (error) => {
  117. if (error != null) {
  118. throw error
  119. }
  120. return done()
  121. })
  122. })
  123. it('should not trigger an error', function () {
  124. return sinon.assert.neverCalledWith(
  125. logger.error,
  126. sinon.match.any,
  127. "not subscribed - shouldn't happen"
  128. )
  129. })
  130. return it('should have left the doc room', function (done) {
  131. return RealTimeClient.getConnectedClient(
  132. this.client.socket.sessionid,
  133. (error, client) => {
  134. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
  135. false
  136. )
  137. return done()
  138. }
  139. )
  140. })
  141. })
  142. return describe('when sending a leaveDoc for a room the client has not joined ', function () {
  143. beforeEach(function (done) {
  144. return this.client.emit('leaveDoc', this.other_doc_id, (error) => {
  145. if (error != null) {
  146. throw error
  147. }
  148. return done()
  149. })
  150. })
  151. return it('should trigger a low level message only', function () {
  152. return sinon.assert.calledWith(
  153. logger.log,
  154. sinon.match.any,
  155. 'ignoring request from client to leave room it is not in'
  156. )
  157. })
  158. })
  159. })
  160. })