move_file_operation.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict'
  2. const { expect } = require('chai')
  3. const ot = require('..')
  4. const File = ot.File
  5. const MoveFileOperation = ot.MoveFileOperation
  6. const Snapshot = ot.Snapshot
  7. const Operation = ot.Operation
  8. const V2DocVersions = ot.V2DocVersions
  9. const TextOperation = ot.TextOperation
  10. describe('MoveFileOperation', function () {
  11. function makeEmptySnapshot() {
  12. return new Snapshot()
  13. }
  14. function makeOneFileSnapshot() {
  15. const snapshot = makeEmptySnapshot()
  16. snapshot.addFile('foo', File.fromString('test: foo'))
  17. return snapshot
  18. }
  19. function makeTwoFileSnapshot() {
  20. const snapshot = makeOneFileSnapshot()
  21. snapshot.addFile('bar', File.fromString('test: bar'))
  22. return snapshot
  23. }
  24. it('moves a file over another', function () {
  25. const snapshot = makeOneFileSnapshot()
  26. const operation = new MoveFileOperation('foo', 'bar')
  27. operation.applyTo(snapshot)
  28. expect(snapshot.countFiles()).to.equal(1)
  29. expect(snapshot.getFile('bar').getContent()).to.equal('test: foo')
  30. })
  31. it('moves a file to another pathname', function () {
  32. const snapshot = makeTwoFileSnapshot()
  33. const operation = new MoveFileOperation('foo', 'a')
  34. operation.applyTo(snapshot)
  35. expect(snapshot.countFiles()).to.equal(2)
  36. expect(snapshot.getFile('a').getContent()).to.equal('test: foo')
  37. expect(snapshot.getFile('bar').getContent()).to.equal('test: bar')
  38. })
  39. it('should keep v2DocVersions in-sync', function () {
  40. const snapshot = makeTwoFileSnapshot()
  41. snapshot.setV2DocVersions(
  42. V2DocVersions.fromRaw({
  43. id1: { pathname: 'foo', v: 1 },
  44. id2: { pathname: 'bar', v: 1 },
  45. })
  46. )
  47. Operation.moveFile('foo', 'foo-after').applyTo(snapshot)
  48. Operation.editFile(
  49. 'foo-after',
  50. TextOperation.fromJSON({ textOperation: [9, 'edit'] })
  51. ).applyTo(snapshot)
  52. Operation.removeFile('bar').applyTo(snapshot)
  53. expect(snapshot.getV2DocVersions().toRaw()).to.deep.equal({
  54. id1: { pathname: 'foo-after', v: 1 },
  55. })
  56. })
  57. })