DeletingADocumentTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* eslint-disable
  2. handle-callback-err,
  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 chai = require("chai");
  15. chai.should();
  16. const MockTrackChangesApi = require("./helpers/MockTrackChangesApi");
  17. const MockProjectHistoryApi = require("./helpers/MockProjectHistoryApi");
  18. const MockWebApi = require("./helpers/MockWebApi");
  19. const DocUpdaterClient = require("./helpers/DocUpdaterClient");
  20. const DocUpdaterApp = require("./helpers/DocUpdaterApp");
  21. describe("Deleting a document", function() {
  22. before(function(done) {
  23. this.lines = ["one", "two", "three"];
  24. this.version = 42;
  25. this.update = {
  26. doc: this.doc_id,
  27. op: [{
  28. i: "one and a half\n",
  29. p: 4
  30. }],
  31. v: this.version
  32. };
  33. this.result = ["one", "one and a half", "two", "three"];
  34. sinon.spy(MockTrackChangesApi, "flushDoc");
  35. sinon.spy(MockProjectHistoryApi, "flushProject");
  36. return DocUpdaterApp.ensureRunning(done);
  37. });
  38. after(function() {
  39. MockTrackChangesApi.flushDoc.restore();
  40. return MockProjectHistoryApi.flushProject.restore();
  41. });
  42. describe("when the updated doc exists in the doc updater", function() {
  43. before(function(done) {
  44. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  45. sinon.spy(MockWebApi, "setDocument");
  46. sinon.spy(MockWebApi, "getDocument");
  47. MockWebApi.insertDoc(this.project_id, this.doc_id, {lines: this.lines, version: this.version});
  48. return DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  49. if (error != null) { throw error; }
  50. return DocUpdaterClient.sendUpdate(this.project_id, this.doc_id, this.update, error => {
  51. if (error != null) { throw error; }
  52. return setTimeout(() => {
  53. return DocUpdaterClient.deleteDoc(this.project_id, this.doc_id, (error, res, body) => {
  54. this.statusCode = res.statusCode;
  55. return setTimeout(done, 200);
  56. });
  57. }
  58. , 200);
  59. });
  60. });
  61. });
  62. after(function() {
  63. MockWebApi.setDocument.restore();
  64. return MockWebApi.getDocument.restore();
  65. });
  66. it("should return a 204 status code", function() {
  67. return this.statusCode.should.equal(204);
  68. });
  69. it("should send the updated document and version to the web api", function() {
  70. return MockWebApi.setDocument
  71. .calledWith(this.project_id, this.doc_id, this.result, this.version + 1)
  72. .should.equal(true);
  73. });
  74. it("should need to reload the doc if read again", function(done) {
  75. MockWebApi.getDocument.called.should.equal.false;
  76. return DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res, doc) => {
  77. MockWebApi.getDocument
  78. .calledWith(this.project_id, this.doc_id)
  79. .should.equal(true);
  80. return done();
  81. });
  82. });
  83. it("should flush track changes", function() {
  84. return MockTrackChangesApi.flushDoc.calledWith(this.doc_id).should.equal(true);
  85. });
  86. return it("should flush project history", function() {
  87. return MockProjectHistoryApi.flushProject.calledWith(this.project_id).should.equal(true);
  88. });
  89. });
  90. return describe("when the doc is not in the doc updater", function() {
  91. before(function(done) {
  92. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  93. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  94. lines: this.lines
  95. });
  96. sinon.spy(MockWebApi, "setDocument");
  97. sinon.spy(MockWebApi, "getDocument");
  98. return DocUpdaterClient.deleteDoc(this.project_id, this.doc_id, (error, res, body) => {
  99. this.statusCode = res.statusCode;
  100. return setTimeout(done, 200);
  101. });
  102. });
  103. after(function() {
  104. MockWebApi.setDocument.restore();
  105. return MockWebApi.getDocument.restore();
  106. });
  107. it("should return a 204 status code", function() {
  108. return this.statusCode.should.equal(204);
  109. });
  110. it("should not need to send the updated document to the web api", function() { return MockWebApi.setDocument.called.should.equal(false); });
  111. it("should need to reload the doc if read again", function(done) {
  112. MockWebApi.getDocument.called.should.equal.false;
  113. return DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res, doc) => {
  114. MockWebApi.getDocument
  115. .calledWith(this.project_id, this.doc_id)
  116. .should.equal(true);
  117. return done();
  118. });
  119. });
  120. it("should flush track changes", function() {
  121. return MockTrackChangesApi.flushDoc.calledWith(this.doc_id).should.equal(true);
  122. });
  123. return it("should flush project history", function() {
  124. return MockProjectHistoryApi.flushProject.calledWith(this.project_id).should.equal(true);
  125. });
  126. });
  127. });