ShareJsDBTests.js 4.5 KB

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