8
0

insertoperation.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. var modules = bender.amd.require(
  8. 'document/document',
  9. 'document/insertoperation',
  10. 'document/removeoperation',
  11. 'document/position',
  12. 'document/character' );
  13. describe( 'InsertOperation', function() {
  14. it( 'should insert node', function() {
  15. var Document = modules[ 'document/document' ];
  16. var InsertOperation = modules[ 'document/insertoperation' ];
  17. var Position = modules[ 'document/position' ];
  18. var Character = modules[ 'document/character' ];
  19. var doc = new Document();
  20. doc.applyOperation( new InsertOperation(
  21. new Position( [ 0 ], doc ),
  22. new Character( null, 'x' ),
  23. doc.version ) );
  24. expect( doc.version ).to.be.equal( 1 );
  25. expect( doc.root.children.length ).to.be.equal( 1 );
  26. expect( doc.root.children[ 0 ].character ).to.be.equal( 'x' );
  27. } );
  28. it( 'should insert set of nodes', function() {
  29. var Document = modules[ 'document/document' ];
  30. var InsertOperation = modules[ 'document/insertoperation' ];
  31. var Position = modules[ 'document/position' ];
  32. var Character = modules[ 'document/character' ];
  33. var doc = new Document();
  34. doc.applyOperation( new InsertOperation(
  35. new Position( [ 0 ], doc ),
  36. [ new Character( null, 'b' ), new Character( null, 'a' ), new Character( null, 'r' ) ],
  37. doc.version ) );
  38. expect( doc.version ).to.be.equal( 1 );
  39. expect( doc.root.children.length ).to.be.equal( 3 );
  40. expect( doc.root.children[ 0 ].character ).to.be.equal( 'b' );
  41. expect( doc.root.children[ 1 ].character ).to.be.equal( 'a' );
  42. expect( doc.root.children[ 2 ].character ).to.be.equal( 'r' );
  43. } );
  44. it( 'should insert text', function() {
  45. var Document = modules[ 'document/document' ];
  46. var InsertOperation = modules[ 'document/insertoperation' ];
  47. var Position = modules[ 'document/position' ];
  48. var Character = modules[ 'document/character' ];
  49. var doc = new Document();
  50. doc.applyOperation( new InsertOperation(
  51. new Position( [ 0 ], doc ),
  52. [ 'foo', new Character( null, 'x' ), 'bar' ],
  53. doc.version ) );
  54. expect( doc.version ).to.be.equal( 1 );
  55. expect( doc.root.children.length ).to.be.equal( 7 );
  56. expect( doc.root.children[ 0 ].character ).to.be.equal( 'f' );
  57. expect( doc.root.children[ 1 ].character ).to.be.equal( 'o' );
  58. expect( doc.root.children[ 2 ].character ).to.be.equal( 'o' );
  59. expect( doc.root.children[ 3 ].character ).to.be.equal( 'x' );
  60. expect( doc.root.children[ 4 ].character ).to.be.equal( 'b' );
  61. expect( doc.root.children[ 5 ].character ).to.be.equal( 'a' );
  62. expect( doc.root.children[ 6 ].character ).to.be.equal( 'r' );
  63. } );
  64. it( 'should create a remove operation as a reverse', function() {
  65. var Document = modules[ 'document/document' ];
  66. var InsertOperation = modules[ 'document/insertoperation' ];
  67. var RemoveOperation = modules[ 'document/removeoperation' ];
  68. var Position = modules[ 'document/position' ];
  69. var Character = modules[ 'document/character' ];
  70. var doc = new Document();
  71. var nodes = [ new Character( null, 'b' ), new Character( null, 'a' ), new Character( null, 'r' ) ];
  72. var position = new Position( [ 0 ], doc );
  73. var operation = new InsertOperation( position, nodes, 0 );
  74. var reverse = operation.reverseOperation();
  75. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  76. expect( reverse.baseVersion ).to.be.equals( 1 );
  77. expect( reverse.nodes ).to.be.equals( nodes );
  78. expect( reverse.position ).to.be.equals( position );
  79. } );
  80. it( 'should undo insert node by applying reverse operation', function() {
  81. var Document = modules[ 'document/document' ];
  82. var InsertOperation = modules[ 'document/insertoperation' ];
  83. var Position = modules[ 'document/position' ];
  84. var Character = modules[ 'document/character' ];
  85. var doc = new Document();
  86. var operation = new InsertOperation(
  87. new Position( [ 0 ], doc ),
  88. new Character( null, 'x' ),
  89. doc.version );
  90. var reverse = operation.reverseOperation();
  91. doc.applyOperation( operation );
  92. expect( doc.version ).to.be.equal( 1 );
  93. doc.applyOperation( reverse );
  94. expect( doc.version ).to.be.equal( 2 );
  95. expect( doc.root.children.length ).to.be.equal( 0 );
  96. } );
  97. it( 'should undo insert set of nodes by applying reverse operation', function() {
  98. var Document = modules[ 'document/document' ];
  99. var InsertOperation = modules[ 'document/insertoperation' ];
  100. var Position = modules[ 'document/position' ];
  101. var Character = modules[ 'document/character' ];
  102. var doc = new Document();
  103. var operation = new InsertOperation(
  104. new Position( [ 0 ], doc ),
  105. [ new Character( null, 'b' ), new Character( null, 'a' ), new Character( null, 'r' ) ],
  106. doc.version );
  107. var reverse = operation.reverseOperation();
  108. doc.applyOperation( operation );
  109. expect( doc.version ).to.be.equal( 1 );
  110. doc.applyOperation( reverse );
  111. expect( doc.version ).to.be.equal( 2 );
  112. expect( doc.root.children.length ).to.be.equal( 0 );
  113. } );
  114. } );