DeletingADocumentTests.js 5.7 KB

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