| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: document */
- 'use strict';
- var modules = bender.amd.require(
- 'document/document',
- 'document/insertoperation',
- 'document/removeoperation',
- 'document/position',
- 'document/character',
- 'document/nodelist' );
- describe( 'InsertOperation', function() {
- var Document, InsertOperation, RemoveOperation, Position, Character;
- before( function() {
- Document = modules[ 'document/document' ];
- InsertOperation = modules[ 'document/insertoperation' ];
- RemoveOperation = modules[ 'document/removeoperation' ];
- Position = modules[ 'document/position' ];
- Character = modules[ 'document/character' ];
- } );
- var doc, root;
- beforeEach( function() {
- doc = new Document();
- root = doc.createRoot( 'root' );
- } );
- it( 'should insert node', function() {
- doc.applyOperation( new InsertOperation(
- new Position( [ 0 ], root ),
- new Character( 'x' ),
- doc.version ) );
- expect( doc.version ).to.equal( 1 );
- expect( root.getChildCount() ).to.equal( 1 );
- expect( root.getChild( 0 ).character ).to.equal( 'x' );
- } );
- it( 'should insert set of nodes', function() {
- doc.applyOperation( new InsertOperation( new Position( [ 0 ], root ), 'bar', doc.version ) );
- expect( doc.version ).to.equal( 1 );
- expect( root.getChildCount() ).to.equal( 3 );
- expect( root.getChild( 0 ).character ).to.equal( 'b' );
- expect( root.getChild( 1 ).character ).to.equal( 'a' );
- expect( root.getChild( 2 ).character ).to.equal( 'r' );
- } );
- it( 'should insert between existing nodes', function() {
- root.insertChildren( 0, 'xy' );
- doc.applyOperation( new InsertOperation( new Position( [ 1 ], root ), 'bar', doc.version ) );
- expect( doc.version ).to.equal( 1 );
- expect( root.getChildCount() ).to.equal( 5 );
- expect( root.getChild( 0 ).character ).to.equal( 'x' );
- expect( root.getChild( 1 ).character ).to.equal( 'b' );
- expect( root.getChild( 2 ).character ).to.equal( 'a' );
- expect( root.getChild( 3 ).character ).to.equal( 'r' );
- expect( root.getChild( 4 ).character ).to.equal( 'y' );
- } );
- it( 'should insert text', function() {
- doc.applyOperation( new InsertOperation(
- new Position( [ 0 ], root ),
- [ 'foo', new Character( 'x' ), 'bar' ],
- doc.version ) );
- expect( doc.version ).to.equal( 1 );
- expect( root.getChildCount() ).to.equal( 7 );
- expect( root.getChild( 0 ).character ).to.equal( 'f' );
- expect( root.getChild( 1 ).character ).to.equal( 'o' );
- expect( root.getChild( 2 ).character ).to.equal( 'o' );
- expect( root.getChild( 3 ).character ).to.equal( 'x' );
- expect( root.getChild( 4 ).character ).to.equal( 'b' );
- expect( root.getChild( 5 ).character ).to.equal( 'a' );
- expect( root.getChild( 6 ).character ).to.equal( 'r' );
- } );
- it( 'should create a remove operation as a reverse', function() {
- var position = new Position( [ 0 ], root );
- var operation = new InsertOperation( position, [ 'foo', new Character( 'x' ), 'bar' ], 0 );
- var reverse = operation.reverseOperation();
- expect( reverse ).to.be.an.instanceof( RemoveOperation );
- expect( reverse.baseVersion ).to.equal( 1 );
- expect( reverse.sourcePosition ).to.equal( position );
- expect( reverse.howMany ).to.equal( 7 );
- } );
- it( 'should undo insert node by applying reverse operation', function() {
- var operation = new InsertOperation(
- new Position( [ 0 ], root ),
- new Character( 'x' ),
- doc.version );
- var reverse = operation.reverseOperation();
- doc.applyOperation( operation );
- expect( doc.version ).to.equal( 1 );
- doc.applyOperation( reverse );
- expect( doc.version ).to.equal( 2 );
- expect( root.getChildCount() ).to.equal( 0 );
- } );
- it( 'should undo insert set of nodes by applying reverse operation', function() {
- var operation = new InsertOperation( new Position( [ 0 ], root ), 'bar', doc.version );
- var reverse = operation.reverseOperation();
- doc.applyOperation( operation );
- expect( doc.version ).to.equal( 1 );
- doc.applyOperation( reverse );
- expect( doc.version ).to.equal( 2 );
- expect( root.getChildCount() ).to.equal( 0 );
- } );
- } );
|