FlushingAProjectTests.js 3.9 KB

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