8
0

attributedelta.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import Range from '../../../src/model/range';
  7. import Position from '../../../src/model/position';
  8. import AttributeDelta from '../../../src/model/delta/attributedelta';
  9. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  10. import NoOperation from '../../../src/model/operation/nooperation';
  11. import { jsonParseStringify } from '../../../tests/model/_utils/utils';
  12. describe( 'AttributeDelta', () => {
  13. let root, delta;
  14. beforeEach( () => {
  15. const model = new Model();
  16. root = model.document.createRoot();
  17. delta = new AttributeDelta();
  18. } );
  19. describe( 'type', () => {
  20. it( 'should be equal to attribute', () => {
  21. expect( delta.type ).to.equal( 'attribute' );
  22. } );
  23. } );
  24. describe( 'key', () => {
  25. it( 'should be null if there are no operations in delta', () => {
  26. expect( delta.key ).to.be.null;
  27. } );
  28. it( 'should be equal to attribute operations key that are in delta', () => {
  29. const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  30. delta.addOperation( new AttributeOperation( range, 'key', 'old', 'new', 0 ) );
  31. expect( delta.key ).to.equal( 'key' );
  32. } );
  33. } );
  34. describe( 'value', () => {
  35. it( 'should be null if there are no operations in delta', () => {
  36. expect( delta.value ).to.be.null;
  37. } );
  38. it( 'should be equal to the value set by the delta operations', () => {
  39. const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  40. delta.addOperation( new AttributeOperation( range, 'key', 'old', 'new', 0 ) );
  41. expect( delta.value ).to.equal( 'new' );
  42. } );
  43. } );
  44. describe( 'range', () => {
  45. it( 'should be null if there are no operations in delta', () => {
  46. expect( delta.range ).to.be.null;
  47. } );
  48. it( 'start and end should be equal to first and last changed position', () => {
  49. const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  50. const rangeB = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  51. const rangeC = new Range( new Position( root, [ 5 ] ), new Position( root, [ 6 ] ) );
  52. delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
  53. delta.addOperation( new AttributeOperation( rangeB, 'key', 'oldB', 'new', 1 ) );
  54. delta.addOperation( new AttributeOperation( rangeC, 'key', 'oldC', 'new', 2 ) );
  55. expect( delta.range.start.path ).to.deep.equal( [ 1 ] );
  56. expect( delta.range.end.path ).to.deep.equal( [ 6 ] );
  57. } );
  58. it( 'should return correct values when some operations are NoOperations', () => {
  59. const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  60. const rangeB = new Range( new Position( root, [ 5 ] ), new Position( root, [ 6 ] ) );
  61. delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
  62. delta.addOperation( new NoOperation( 1 ) );
  63. delta.addOperation( new AttributeOperation( rangeB, 'key', 'oldC', 'new', 2 ) );
  64. expect( delta.range.start.path ).to.deep.equal( [ 2 ] );
  65. expect( delta.range.end.path ).to.deep.equal( [ 6 ] );
  66. } );
  67. } );
  68. describe( 'getReversed', () => {
  69. it( 'should return empty AttributeDelta if there are no operations in delta', () => {
  70. const reversed = delta.getReversed();
  71. expect( reversed ).to.be.instanceof( AttributeDelta );
  72. expect( reversed.operations.length ).to.equal( 0 );
  73. } );
  74. it( 'should return correct AttributeDelta', () => {
  75. const rangeA = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  76. const rangeB = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  77. delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
  78. delta.addOperation( new AttributeOperation( rangeB, 'key', 'oldB', 'new', 1 ) );
  79. const reversed = delta.getReversed();
  80. expect( reversed ).to.be.instanceof( AttributeDelta );
  81. expect( reversed.operations.length ).to.equal( 2 );
  82. // Remember about reversed operations order.
  83. expect( reversed.operations[ 0 ] ).to.be.instanceof( AttributeOperation );
  84. expect( reversed.operations[ 0 ].range.isEqual( rangeB ) ).to.be.true;
  85. expect( reversed.operations[ 0 ].key ).to.equal( 'key' );
  86. expect( reversed.operations[ 0 ].oldValue ).to.equal( 'new' );
  87. expect( reversed.operations[ 0 ].newValue ).to.equal( 'oldB' );
  88. expect( reversed.operations[ 1 ] ).to.be.instanceof( AttributeOperation );
  89. expect( reversed.operations[ 1 ].range.isEqual( rangeA ) ).to.be.true;
  90. expect( reversed.operations[ 1 ].key ).to.equal( 'key' );
  91. expect( reversed.operations[ 1 ].oldValue ).to.equal( 'new' );
  92. expect( reversed.operations[ 1 ].newValue ).to.equal( 'oldA' );
  93. } );
  94. } );
  95. it( 'should provide proper className', () => {
  96. expect( AttributeDelta.className ).to.equal( 'engine.model.delta.AttributeDelta' );
  97. } );
  98. it( 'should not have _range property when converted to JSON', () => {
  99. const json = jsonParseStringify( delta );
  100. expect( json ).not.to.have.property( '_range' );
  101. } );
  102. } );