GettingDocsTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const { ObjectId } = require('mongodb-legacy')
  14. const DocstoreApp = require('./helpers/DocstoreApp')
  15. const DocstoreClient = require('./helpers/DocstoreClient')
  16. describe('Getting a doc', function () {
  17. beforeEach(function (done) {
  18. this.project_id = new ObjectId()
  19. this.doc_id = new ObjectId()
  20. this.lines = ['original', 'lines']
  21. this.version = 42
  22. this.ranges = {
  23. changes: [
  24. {
  25. id: new ObjectId().toString(),
  26. op: { i: 'foo', p: 3 },
  27. meta: {
  28. user_id: new ObjectId().toString(),
  29. ts: new Date().toJSON(),
  30. },
  31. },
  32. ],
  33. comments: [
  34. {
  35. id: new ObjectId().toString(),
  36. op: { c: 'comment', p: 1, t: new ObjectId().toString() },
  37. metadata: {
  38. user_id: new ObjectId().toString(),
  39. ts: new Date().toJSON(),
  40. },
  41. },
  42. ],
  43. }
  44. this.fixedRanges = {
  45. ...this.ranges,
  46. comments: [
  47. { ...this.ranges.comments[0], id: this.ranges.comments[0].op.t },
  48. ],
  49. }
  50. return DocstoreApp.ensureRunning(() => {
  51. return DocstoreClient.createDoc(
  52. this.project_id,
  53. this.doc_id,
  54. this.lines,
  55. this.version,
  56. this.ranges,
  57. error => {
  58. if (error != null) {
  59. throw error
  60. }
  61. return done()
  62. }
  63. )
  64. })
  65. })
  66. describe('when the doc exists', function () {
  67. return it('should get the doc lines and version', function (done) {
  68. return DocstoreClient.getDoc(
  69. this.project_id,
  70. this.doc_id,
  71. {},
  72. (error, res, doc) => {
  73. if (error) return done(error)
  74. doc.lines.should.deep.equal(this.lines)
  75. doc.version.should.equal(this.version)
  76. doc.ranges.should.deep.equal(this.fixedRanges)
  77. return done()
  78. }
  79. )
  80. })
  81. })
  82. describe('when the doc does not exist', function () {
  83. return it('should return a 404', function (done) {
  84. const missingDocId = new ObjectId()
  85. return DocstoreClient.getDoc(
  86. this.project_id,
  87. missingDocId,
  88. {},
  89. (error, res, doc) => {
  90. if (error) return done(error)
  91. res.statusCode.should.equal(404)
  92. return done()
  93. }
  94. )
  95. })
  96. })
  97. return describe('when the doc is a deleted doc', function () {
  98. beforeEach(function (done) {
  99. this.deleted_doc_id = new ObjectId()
  100. return DocstoreClient.createDoc(
  101. this.project_id,
  102. this.deleted_doc_id,
  103. this.lines,
  104. this.version,
  105. this.ranges,
  106. error => {
  107. if (error != null) {
  108. throw error
  109. }
  110. return DocstoreClient.deleteDoc(
  111. this.project_id,
  112. this.deleted_doc_id,
  113. done
  114. )
  115. }
  116. )
  117. })
  118. it('should return the doc', function (done) {
  119. return DocstoreClient.getDoc(
  120. this.project_id,
  121. this.deleted_doc_id,
  122. { include_deleted: true },
  123. (error, res, doc) => {
  124. if (error) return done(error)
  125. doc.lines.should.deep.equal(this.lines)
  126. doc.version.should.equal(this.version)
  127. doc.ranges.should.deep.equal(this.fixedRanges)
  128. doc.deleted.should.equal(true)
  129. return done()
  130. }
  131. )
  132. })
  133. return it('should return a 404 when the query string is not set', function (done) {
  134. return DocstoreClient.getDoc(
  135. this.project_id,
  136. this.deleted_doc_id,
  137. {},
  138. (error, res, doc) => {
  139. if (error) return done(error)
  140. res.statusCode.should.equal(404)
  141. return done()
  142. }
  143. )
  144. })
  145. })
  146. })