FlushingAProjectTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* eslint-disable
  2. camelcase,
  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 async = require('async')
  15. const MockWebApi = require('./helpers/MockWebApi')
  16. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  17. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  18. describe('Flushing a project', function () {
  19. before(function (done) {
  20. let doc_id0, doc_id1
  21. this.project_id = DocUpdaterClient.randomId()
  22. this.docs = [
  23. {
  24. id: (doc_id0 = DocUpdaterClient.randomId()),
  25. lines: ['one', 'two', 'three'],
  26. update: {
  27. doc: doc_id0,
  28. op: [
  29. {
  30. i: 'one and a half\n',
  31. p: 4,
  32. },
  33. ],
  34. v: 0,
  35. },
  36. updatedLines: ['one', 'one and a half', 'two', 'three'],
  37. },
  38. {
  39. id: (doc_id1 = DocUpdaterClient.randomId()),
  40. lines: ['four', 'five', 'six'],
  41. update: {
  42. doc: doc_id1,
  43. op: [
  44. {
  45. i: 'four and a half\n',
  46. p: 5,
  47. },
  48. ],
  49. v: 0,
  50. },
  51. updatedLines: ['four', 'four and a half', 'five', 'six'],
  52. },
  53. ]
  54. for (const doc of Array.from(this.docs)) {
  55. MockWebApi.insertDoc(this.project_id, doc.id, {
  56. lines: doc.lines,
  57. version: doc.update.v,
  58. })
  59. }
  60. return DocUpdaterApp.ensureRunning(done)
  61. })
  62. return describe('with documents which have been updated', function () {
  63. before(function (done) {
  64. sinon.spy(MockWebApi, 'setDocument')
  65. return async.series(
  66. this.docs.map(doc => {
  67. return callback => {
  68. return DocUpdaterClient.preloadDoc(
  69. this.project_id,
  70. doc.id,
  71. error => {
  72. if (error != null) {
  73. return callback(error)
  74. }
  75. return DocUpdaterClient.sendUpdate(
  76. this.project_id,
  77. doc.id,
  78. doc.update,
  79. error => {
  80. return callback(error)
  81. }
  82. )
  83. }
  84. )
  85. }
  86. }),
  87. error => {
  88. if (error != null) {
  89. throw error
  90. }
  91. return setTimeout(() => {
  92. return DocUpdaterClient.flushProject(
  93. this.project_id,
  94. (error, res, body) => {
  95. if (error) return done(error)
  96. this.statusCode = res.statusCode
  97. return done()
  98. }
  99. )
  100. }, 200)
  101. }
  102. )
  103. })
  104. after(function () {
  105. return MockWebApi.setDocument.restore()
  106. })
  107. it('should return a 204 status code', function () {
  108. return this.statusCode.should.equal(204)
  109. })
  110. it('should send each document to the web api', function () {
  111. return Array.from(this.docs).map(doc =>
  112. MockWebApi.setDocument
  113. .calledWith(this.project_id, doc.id, doc.updatedLines)
  114. .should.equal(true)
  115. )
  116. })
  117. return it('should update the lines in the doc updater', function (done) {
  118. return async.series(
  119. this.docs.map(doc => {
  120. return callback => {
  121. return DocUpdaterClient.getDoc(
  122. this.project_id,
  123. doc.id,
  124. (error, res, returnedDoc) => {
  125. if (error) return done(error)
  126. returnedDoc.lines.should.deep.equal(doc.updatedLines)
  127. return callback()
  128. }
  129. )
  130. }
  131. }),
  132. done
  133. )
  134. })
  135. })
  136. })