DeletingADocumentTests.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* eslint-disable
  2. handle-callback-err,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const MockTrackChangesApi = require('./helpers/MockTrackChangesApi')
  15. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  16. const MockWebApi = require('./helpers/MockWebApi')
  17. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  18. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  19. describe('Deleting a document', function () {
  20. before(function (done) {
  21. this.lines = ['one', 'two', 'three']
  22. this.version = 42
  23. this.update = {
  24. doc: this.doc_id,
  25. op: [
  26. {
  27. i: 'one and a half\n',
  28. p: 4,
  29. },
  30. ],
  31. v: this.version,
  32. }
  33. this.result = ['one', 'one and a half', 'two', 'three']
  34. sinon.spy(MockTrackChangesApi, 'flushDoc')
  35. sinon.spy(MockProjectHistoryApi, 'flushProject')
  36. return DocUpdaterApp.ensureRunning(done)
  37. })
  38. after(function () {
  39. MockTrackChangesApi.flushDoc.restore()
  40. return MockProjectHistoryApi.flushProject.restore()
  41. })
  42. describe('when the updated doc exists in the doc updater', function () {
  43. before(function (done) {
  44. ;[this.project_id, this.doc_id] = Array.from([
  45. DocUpdaterClient.randomId(),
  46. DocUpdaterClient.randomId(),
  47. ])
  48. sinon.spy(MockWebApi, 'setDocument')
  49. sinon.spy(MockWebApi, 'getDocument')
  50. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  51. lines: this.lines,
  52. version: this.version,
  53. })
  54. return DocUpdaterClient.preloadDoc(
  55. this.project_id,
  56. this.doc_id,
  57. error => {
  58. if (error != null) {
  59. throw error
  60. }
  61. return DocUpdaterClient.sendUpdate(
  62. this.project_id,
  63. this.doc_id,
  64. this.update,
  65. error => {
  66. if (error != null) {
  67. throw error
  68. }
  69. return setTimeout(() => {
  70. return DocUpdaterClient.deleteDoc(
  71. this.project_id,
  72. this.doc_id,
  73. (error, res, body) => {
  74. this.statusCode = res.statusCode
  75. return setTimeout(done, 200)
  76. }
  77. )
  78. }, 200)
  79. }
  80. )
  81. }
  82. )
  83. })
  84. after(function () {
  85. MockWebApi.setDocument.restore()
  86. return MockWebApi.getDocument.restore()
  87. })
  88. it('should return a 204 status code', function () {
  89. return this.statusCode.should.equal(204)
  90. })
  91. it('should send the updated document and version to the web api', function () {
  92. return MockWebApi.setDocument
  93. .calledWith(this.project_id, this.doc_id, this.result, this.version + 1)
  94. .should.equal(true)
  95. })
  96. it('should need to reload the doc if read again', function (done) {
  97. MockWebApi.getDocument.resetHistory()
  98. MockWebApi.getDocument.called.should.equals(false)
  99. return DocUpdaterClient.getDoc(
  100. this.project_id,
  101. this.doc_id,
  102. (error, res, doc) => {
  103. MockWebApi.getDocument
  104. .calledWith(this.project_id, this.doc_id)
  105. .should.equal(true)
  106. return done()
  107. }
  108. )
  109. })
  110. it('should flush track changes', function () {
  111. return MockTrackChangesApi.flushDoc
  112. .calledWith(this.doc_id)
  113. .should.equal(true)
  114. })
  115. return it('should flush project history', function () {
  116. return MockProjectHistoryApi.flushProject
  117. .calledWith(this.project_id)
  118. .should.equal(true)
  119. })
  120. })
  121. return describe('when the doc is not in the doc updater', function () {
  122. before(function (done) {
  123. ;[this.project_id, this.doc_id] = Array.from([
  124. DocUpdaterClient.randomId(),
  125. DocUpdaterClient.randomId(),
  126. ])
  127. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  128. lines: this.lines,
  129. })
  130. sinon.spy(MockWebApi, 'setDocument')
  131. sinon.spy(MockWebApi, 'getDocument')
  132. return DocUpdaterClient.deleteDoc(
  133. this.project_id,
  134. this.doc_id,
  135. (error, res, body) => {
  136. this.statusCode = res.statusCode
  137. return setTimeout(done, 200)
  138. }
  139. )
  140. })
  141. after(function () {
  142. MockWebApi.setDocument.restore()
  143. return MockWebApi.getDocument.restore()
  144. })
  145. it('should return a 204 status code', function () {
  146. return this.statusCode.should.equal(204)
  147. })
  148. it('should not need to send the updated document to the web api', function () {
  149. return MockWebApi.setDocument.called.should.equal(false)
  150. })
  151. it('should need to reload the doc if read again', function (done) {
  152. MockWebApi.getDocument.called.should.equals(false)
  153. return DocUpdaterClient.getDoc(
  154. this.project_id,
  155. this.doc_id,
  156. (error, res, doc) => {
  157. MockWebApi.getDocument
  158. .calledWith(this.project_id, this.doc_id)
  159. .should.equal(true)
  160. return done()
  161. }
  162. )
  163. })
  164. it('should flush track changes', function () {
  165. return MockTrackChangesApi.flushDoc
  166. .calledWith(this.doc_id)
  167. .should.equal(true)
  168. })
  169. return it('should flush project history', function () {
  170. return MockProjectHistoryApi.flushProject
  171. .calledWith(this.project_id)
  172. .should.equal(true)
  173. })
  174. })
  175. })