FlushingAProjectTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 async = require('async')
  16. const MockWebApi = require('./helpers/MockWebApi')
  17. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  18. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  19. describe('Flushing a project', function () {
  20. before(function (done) {
  21. let doc_id0, doc_id1
  22. this.project_id = DocUpdaterClient.randomId()
  23. this.docs = [
  24. {
  25. id: (doc_id0 = DocUpdaterClient.randomId()),
  26. lines: ['one', 'two', 'three'],
  27. update: {
  28. doc: doc_id0,
  29. op: [
  30. {
  31. i: 'one and a half\n',
  32. p: 4
  33. }
  34. ],
  35. v: 0
  36. },
  37. updatedLines: ['one', 'one and a half', 'two', 'three']
  38. },
  39. {
  40. id: (doc_id1 = DocUpdaterClient.randomId()),
  41. lines: ['four', 'five', 'six'],
  42. update: {
  43. doc: doc_id1,
  44. op: [
  45. {
  46. i: 'four and a half\n',
  47. p: 5
  48. }
  49. ],
  50. v: 0
  51. },
  52. updatedLines: ['four', 'four and a half', 'five', 'six']
  53. }
  54. ]
  55. for (const doc of Array.from(this.docs)) {
  56. MockWebApi.insertDoc(this.project_id, doc.id, {
  57. lines: doc.lines,
  58. version: doc.update.v
  59. })
  60. }
  61. return DocUpdaterApp.ensureRunning(done)
  62. })
  63. return describe('with documents which have been updated', function () {
  64. before(function (done) {
  65. sinon.spy(MockWebApi, 'setDocument')
  66. return async.series(
  67. this.docs.map((doc) => {
  68. return (callback) => {
  69. return DocUpdaterClient.preloadDoc(
  70. this.project_id,
  71. doc.id,
  72. (error) => {
  73. if (error != null) {
  74. return callback(error)
  75. }
  76. return DocUpdaterClient.sendUpdate(
  77. this.project_id,
  78. doc.id,
  79. doc.update,
  80. (error) => {
  81. return callback(error)
  82. }
  83. )
  84. }
  85. )
  86. }
  87. }),
  88. (error) => {
  89. if (error != null) {
  90. throw error
  91. }
  92. return setTimeout(() => {
  93. return DocUpdaterClient.flushProject(
  94. this.project_id,
  95. (error, res, body) => {
  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. returnedDoc.lines.should.deep.equal(doc.updatedLines)
  126. return callback()
  127. }
  128. )
  129. }
  130. }),
  131. done
  132. )
  133. })
  134. })
  135. })