LeaveDocTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 { expect } = require('chai')
  17. const sinon = require('sinon')
  18. const RealTimeClient = require('./helpers/RealTimeClient')
  19. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  20. const FixturesManager = require('./helpers/FixturesManager')
  21. const logger = require('logger-sharelatex')
  22. const async = require('async')
  23. describe('leaveDoc', function () {
  24. before(function () {
  25. this.lines = ['test', 'doc', 'lines']
  26. this.version = 42
  27. this.ops = ['mock', 'doc', 'ops']
  28. sinon.spy(logger, 'error')
  29. sinon.spy(logger, 'warn')
  30. sinon.spy(logger, 'log')
  31. return (this.other_doc_id = FixturesManager.getRandomId())
  32. })
  33. after(function () {
  34. logger.error.restore() // remove the spy
  35. logger.warn.restore()
  36. return logger.log.restore()
  37. })
  38. return describe('when joined to a doc', function () {
  39. beforeEach(function (done) {
  40. return async.series(
  41. [
  42. (cb) => {
  43. return FixturesManager.setUpProject(
  44. {
  45. privilegeLevel: 'readAndWrite'
  46. },
  47. (e, { project_id, user_id }) => {
  48. this.project_id = project_id
  49. this.user_id = user_id
  50. return cb(e)
  51. }
  52. )
  53. },
  54. (cb) => {
  55. return FixturesManager.setUpDoc(
  56. this.project_id,
  57. { lines: this.lines, version: this.version, ops: this.ops },
  58. (e, { doc_id }) => {
  59. this.doc_id = doc_id
  60. return cb(e)
  61. }
  62. )
  63. },
  64. (cb) => {
  65. this.client = RealTimeClient.connect()
  66. return this.client.on('connectionAccepted', cb)
  67. },
  68. (cb) => {
  69. return this.client.emit(
  70. 'joinProject',
  71. { project_id: this.project_id },
  72. cb
  73. )
  74. },
  75. (cb) => {
  76. return this.client.emit(
  77. 'joinDoc',
  78. this.doc_id,
  79. (error, ...rest) => {
  80. ;[...this.returnedArgs] = Array.from(rest)
  81. return cb(error)
  82. }
  83. )
  84. }
  85. ],
  86. done
  87. )
  88. })
  89. describe('then leaving the doc', function () {
  90. beforeEach(function (done) {
  91. return this.client.emit('leaveDoc', this.doc_id, (error) => {
  92. if (error != null) {
  93. throw error
  94. }
  95. return done()
  96. })
  97. })
  98. return it('should have left the doc room', function (done) {
  99. return RealTimeClient.getConnectedClient(
  100. this.client.socket.sessionid,
  101. (error, client) => {
  102. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
  103. false
  104. )
  105. return done()
  106. }
  107. )
  108. })
  109. })
  110. describe('when sending a leaveDoc request before the previous joinDoc request has completed', function () {
  111. beforeEach(function (done) {
  112. this.client.emit('leaveDoc', this.doc_id, () => {})
  113. this.client.emit('joinDoc', this.doc_id, () => {})
  114. return this.client.emit('leaveDoc', this.doc_id, (error) => {
  115. if (error != null) {
  116. throw error
  117. }
  118. return done()
  119. })
  120. })
  121. it('should not trigger an error', function () {
  122. return sinon.assert.neverCalledWith(
  123. logger.error,
  124. sinon.match.any,
  125. "not subscribed - shouldn't happen"
  126. )
  127. })
  128. return it('should have left the doc room', function (done) {
  129. return RealTimeClient.getConnectedClient(
  130. this.client.socket.sessionid,
  131. (error, client) => {
  132. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
  133. false
  134. )
  135. return done()
  136. }
  137. )
  138. })
  139. })
  140. return describe('when sending a leaveDoc for a room the client has not joined ', function () {
  141. beforeEach(function (done) {
  142. return this.client.emit('leaveDoc', this.other_doc_id, (error) => {
  143. if (error != null) {
  144. throw error
  145. }
  146. return done()
  147. })
  148. })
  149. return it('should trigger a low level message only', function () {
  150. return sinon.assert.calledWith(
  151. logger.log,
  152. sinon.match.any,
  153. 'ignoring request from client to leave room it is not in'
  154. )
  155. })
  156. })
  157. })
  158. })