DeletingADocumentTests.js 5.0 KB

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