edit_file_operation.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict'
  2. const { expect } = require('chai')
  3. const ot = require('..')
  4. const File = ot.File
  5. const Operation = ot.Operation
  6. const TextOperation = ot.TextOperation
  7. describe('EditFileOperation', function () {
  8. function edit(pathname, textOperationJsonObject) {
  9. return Operation.editFile(
  10. pathname,
  11. TextOperation.fromJSON(textOperationJsonObject)
  12. )
  13. }
  14. describe('canBeComposedWith', function () {
  15. it('on the same file', function () {
  16. const editFileOperation1 = edit('foo.tex', ['x'])
  17. const editFileOperation2 = edit('foo.tex', [1, 'y'])
  18. expect(editFileOperation1.canBeComposedWith(editFileOperation2)).to.be
  19. .true
  20. })
  21. it('on different files', function () {
  22. const editFileOperation1 = edit('foo.tex', ['x'])
  23. const editFileOperation2 = edit('bar.tex', ['y'])
  24. expect(editFileOperation1.canBeComposedWith(editFileOperation2)).to.be
  25. .false
  26. })
  27. it('with a different type of opperation', function () {
  28. const editFileOperation1 = edit('foo.tex', ['x'])
  29. const editFileOperation2 = Operation.addFile(
  30. 'bar.tex',
  31. File.fromString('')
  32. )
  33. expect(editFileOperation1.canBeComposedWith(editFileOperation2)).to.be
  34. .false
  35. })
  36. it('with incompatible lengths', function () {
  37. const editFileOperation1 = edit('foo.tex', ['x'])
  38. const editFileOperation2 = edit('foo.tex', [2, 'y'])
  39. expect(editFileOperation1.canBeComposedWith(editFileOperation2)).to.be
  40. .false
  41. })
  42. })
  43. describe('canBeComposedWithForUndo', function () {
  44. it('can', function () {
  45. const editFileOperation1 = edit('foo.tex', ['x'])
  46. const editFileOperation2 = edit('foo.tex', [1, 'y'])
  47. expect(editFileOperation1.canBeComposedWithForUndo(editFileOperation2)).to
  48. .be.true
  49. })
  50. it('cannot', function () {
  51. const editFileOperation1 = edit('foo.tex', ['x'])
  52. const editFileOperation2 = edit('foo.tex', ['y', 1, 'z'])
  53. expect(editFileOperation1.canBeComposedWithForUndo(editFileOperation2)).to
  54. .be.false
  55. })
  56. })
  57. describe('compose', function () {
  58. it('composes text operations', function () {
  59. const editFileOperation1 = edit('foo.tex', ['x'])
  60. const editFileOperation2 = edit('foo.tex', [1, 'y'])
  61. const composedFileOperation =
  62. editFileOperation1.compose(editFileOperation2)
  63. const expectedComposedFileOperation = edit('foo.tex', ['xy'])
  64. expect(composedFileOperation).to.deep.equal(expectedComposedFileOperation)
  65. // check that the original operation wasn't modified
  66. expect(editFileOperation1).to.deep.equal(edit('foo.tex', ['x']))
  67. })
  68. })
  69. })