DiffCodecTests.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/js/DiffCodec.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. describe('DiffCodec', function () {
  6. beforeEach(function () {
  7. this.callback = sinon.stub()
  8. this.DiffCodec = SandboxedModule.require(modulePath)
  9. })
  10. describe('diffAsShareJsOps', function () {
  11. it('should insert new text correctly', function () {
  12. this.before = ['hello world']
  13. this.after = ['hello beautiful world']
  14. const ops = this.DiffCodec.diffAsShareJsOp(this.before, this.after)
  15. expect(ops).to.deep.equal([
  16. {
  17. i: 'beautiful ',
  18. p: 6,
  19. },
  20. ])
  21. })
  22. it('should shift later inserts by previous inserts', function () {
  23. this.before = ['the boy played with the ball']
  24. this.after = ['the tall boy played with the red ball']
  25. const ops = this.DiffCodec.diffAsShareJsOp(this.before, this.after)
  26. expect(ops).to.deep.equal([
  27. { i: 'tall ', p: 4 },
  28. { i: 'red ', p: 29 },
  29. ])
  30. })
  31. it('should delete text correctly', function () {
  32. this.before = ['hello beautiful world']
  33. this.after = ['hello world']
  34. const ops = this.DiffCodec.diffAsShareJsOp(this.before, this.after)
  35. expect(ops).to.deep.equal([
  36. {
  37. d: 'beautiful ',
  38. p: 6,
  39. },
  40. ])
  41. })
  42. it('should shift later deletes by the first deletes', function () {
  43. this.before = ['the tall boy played with the red ball']
  44. this.after = ['the boy played with the ball']
  45. const ops = this.DiffCodec.diffAsShareJsOp(this.before, this.after)
  46. expect(ops).to.deep.equal([
  47. { d: 'tall ', p: 4 },
  48. { d: 'red ', p: 24 },
  49. ])
  50. })
  51. })
  52. })