DeletingADocumentTests.js 5.6 KB

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