ShareJsDBTests.js 4.5 KB

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