GettingProjectDocsTests.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. chai.should()
  17. const { expect } = chai
  18. const MockWebApi = require('./helpers/MockWebApi')
  19. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  20. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  21. describe('Getting documents for project', function () {
  22. before(function (done) {
  23. this.lines = ['one', 'two', 'three']
  24. this.version = 42
  25. return DocUpdaterApp.ensureRunning(done)
  26. })
  27. describe('when project state hash does not match', function () {
  28. before(function (done) {
  29. this.projectStateHash = DocUpdaterClient.randomId()
  30. ;[this.project_id, this.doc_id] = Array.from([
  31. DocUpdaterClient.randomId(),
  32. DocUpdaterClient.randomId()
  33. ])
  34. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  35. lines: this.lines,
  36. version: this.version
  37. })
  38. return DocUpdaterClient.preloadDoc(
  39. this.project_id,
  40. this.doc_id,
  41. (error) => {
  42. if (error != null) {
  43. throw error
  44. }
  45. return DocUpdaterClient.getProjectDocs(
  46. this.project_id,
  47. this.projectStateHash,
  48. (error, res, returnedDocs) => {
  49. this.res = res
  50. this.returnedDocs = returnedDocs
  51. return done()
  52. }
  53. )
  54. }
  55. )
  56. })
  57. return it('should return a 409 Conflict response', function () {
  58. return this.res.statusCode.should.equal(409)
  59. })
  60. })
  61. describe('when project state hash matches', function () {
  62. before(function (done) {
  63. this.projectStateHash = DocUpdaterClient.randomId()
  64. ;[this.project_id, this.doc_id] = Array.from([
  65. DocUpdaterClient.randomId(),
  66. DocUpdaterClient.randomId()
  67. ])
  68. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  69. lines: this.lines,
  70. version: this.version
  71. })
  72. return DocUpdaterClient.preloadDoc(
  73. this.project_id,
  74. this.doc_id,
  75. (error) => {
  76. if (error != null) {
  77. throw error
  78. }
  79. return DocUpdaterClient.getProjectDocs(
  80. this.project_id,
  81. this.projectStateHash,
  82. (error, res0, returnedDocs0) => {
  83. // set the hash
  84. this.res0 = res0
  85. this.returnedDocs0 = returnedDocs0
  86. return DocUpdaterClient.getProjectDocs(
  87. this.project_id,
  88. this.projectStateHash,
  89. (error, res, returnedDocs) => {
  90. // the hash should now match
  91. this.res = res
  92. this.returnedDocs = returnedDocs
  93. return done()
  94. }
  95. )
  96. }
  97. )
  98. }
  99. )
  100. })
  101. it('should return a 200 response', function () {
  102. return this.res.statusCode.should.equal(200)
  103. })
  104. return it('should return the documents', function () {
  105. return this.returnedDocs.should.deep.equal([
  106. { _id: this.doc_id, lines: this.lines, v: this.version }
  107. ])
  108. })
  109. })
  110. return describe('when the doc has been removed', function () {
  111. before(function (done) {
  112. this.projectStateHash = DocUpdaterClient.randomId()
  113. ;[this.project_id, this.doc_id] = Array.from([
  114. DocUpdaterClient.randomId(),
  115. DocUpdaterClient.randomId()
  116. ])
  117. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  118. lines: this.lines,
  119. version: this.version
  120. })
  121. return DocUpdaterClient.preloadDoc(
  122. this.project_id,
  123. this.doc_id,
  124. (error) => {
  125. if (error != null) {
  126. throw error
  127. }
  128. return DocUpdaterClient.getProjectDocs(
  129. this.project_id,
  130. this.projectStateHash,
  131. (error, res0, returnedDocs0) => {
  132. // set the hash
  133. this.res0 = res0
  134. this.returnedDocs0 = returnedDocs0
  135. return DocUpdaterClient.deleteDoc(
  136. this.project_id,
  137. this.doc_id,
  138. (error, res, body) => {
  139. // delete the doc
  140. return DocUpdaterClient.getProjectDocs(
  141. this.project_id,
  142. this.projectStateHash,
  143. (error, res1, returnedDocs) => {
  144. // the hash would match, but the doc has been deleted
  145. this.res = res1
  146. this.returnedDocs = returnedDocs
  147. return done()
  148. }
  149. )
  150. }
  151. )
  152. }
  153. )
  154. }
  155. )
  156. })
  157. return it('should return a 409 Conflict response', function () {
  158. return this.res.statusCode.should.equal(409)
  159. })
  160. })
  161. })