ShareJsDBTests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  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. const should = chai.should();
  16. const {
  17. expect
  18. } = chai;
  19. const modulePath = "../../../../app/js/ShareJsDB.js";
  20. const SandboxedModule = require('sandboxed-module');
  21. const Errors = require("../../../../app/js/Errors");
  22. describe("ShareJsDB", function() {
  23. beforeEach(function() {
  24. this.doc_id = "document-id";
  25. this.project_id = "project-id";
  26. this.doc_key = `${this.project_id}:${this.doc_id}`;
  27. this.callback = sinon.stub();
  28. this.ShareJsDB = SandboxedModule.require(modulePath, { requires: {
  29. "./RedisManager": (this.RedisManager = {})
  30. }
  31. });
  32. this.version = 42;
  33. this.lines = ["one", "two", "three"];
  34. return this.db = new this.ShareJsDB(this.project_id, this.doc_id, this.lines, this.version);
  35. });
  36. describe("getSnapshot", function() {
  37. describe("successfully", function() {
  38. beforeEach(function() {
  39. return this.db.getSnapshot(this.doc_key, this.callback);
  40. });
  41. it("should return the doc lines", function() {
  42. return this.callback.args[0][1].snapshot.should.equal(this.lines.join("\n"));
  43. });
  44. it("should return the doc version", function() {
  45. return this.callback.args[0][1].v.should.equal(this.version);
  46. });
  47. return it("should return the type as text", function() {
  48. return this.callback.args[0][1].type.should.equal("text");
  49. });
  50. });
  51. return describe("when the key does not match", function() {
  52. beforeEach(function() {
  53. return this.db.getSnapshot("bad:key", this.callback);
  54. });
  55. return it("should return the callback with a NotFoundError", function() {
  56. return this.callback.calledWith(new Errors.NotFoundError("not found")).should.equal(true);
  57. });
  58. });
  59. });
  60. describe("getOps", function() {
  61. describe("with start == end", function() {
  62. beforeEach(function() {
  63. this.start = (this.end = 42);
  64. return this.db.getOps(this.doc_key, this.start, this.end, this.callback);
  65. });
  66. return it("should return an empty array", function() {
  67. return this.callback.calledWith(null, []).should.equal(true);
  68. });
  69. });
  70. describe("with a non empty range", function() {
  71. beforeEach(function() {
  72. this.start = 35;
  73. this.end = 42;
  74. this.RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, this.ops);
  75. return this.db.getOps(this.doc_key, this.start, this.end, this.callback);
  76. });
  77. it("should get the range from redis", function() {
  78. return this.RedisManager.getPreviousDocOps
  79. .calledWith(this.doc_id, this.start, this.end-1)
  80. .should.equal(true);
  81. });
  82. return it("should return the ops", function() {
  83. return this.callback.calledWith(null, this.ops).should.equal(true);
  84. });
  85. });
  86. return describe("with no specified end", function() {
  87. beforeEach(function() {
  88. this.start = 35;
  89. this.end = null;
  90. this.RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, this.ops);
  91. return this.db.getOps(this.doc_key, this.start, this.end, this.callback);
  92. });
  93. return it("should get until the end of the list", function() {
  94. return this.RedisManager.getPreviousDocOps
  95. .calledWith(this.doc_id, this.start, -1)
  96. .should.equal(true);
  97. });
  98. });
  99. });
  100. return describe("writeOps", function() { return describe("writing an op", function() {
  101. beforeEach(function() {
  102. this.opData = {
  103. op: {p: 20, t: "foo"},
  104. meta: {source: "bar"},
  105. v: this.version
  106. };
  107. return this.db.writeOp(this.doc_key, this.opData, this.callback);
  108. });
  109. it("should write into appliedOps", function() {
  110. return expect(this.db.appliedOps[this.doc_key]).to.deep.equal([this.opData]);
  111. });
  112. return it("should call the callback without an error", function() {
  113. this.callback.called.should.equal(true);
  114. return (this.callback.args[0][0] != null).should.equal(false);
  115. });
  116. }); });
  117. });