FlushingAProjectTests.js 3.8 KB

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