ShareJsDBTests.js 4.6 KB

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