| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: document */
- /* global describe, before, beforeEach, it, expect */
- 'use strict';
- const modules = bender.amd.require(
- 'document/document',
- 'document/node',
- 'document/nodelist',
- 'document/operation/insertoperation',
- 'document/operation/removeoperation',
- 'document/position',
- 'document/character',
- 'document/nodelist'
- );
- describe( 'InsertOperation', () => {
- let Document, Node, NodeList, InsertOperation, RemoveOperation, Position, Character;
- before( () => {
- Document = modules[ 'document/document' ];
- Node = modules[ 'document/node' ];
- NodeList = modules[ 'document/nodelist' ];
- InsertOperation = modules[ 'document/operation/insertoperation' ];
- RemoveOperation = modules[ 'document/operation/removeoperation' ];
- Position = modules[ 'document/position' ];
- Character = modules[ 'document/character' ];
- } );
- let doc, root;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot( 'root' );
- } );
- it( 'should have proper type', () => {
- const opp = new InsertOperation(
- new Position( [ 0 ], root ),
- new Character( 'x' ),
- doc.version
- );
- expect( opp.type ).to.equal( 'insert' );
- } );
- it( 'should insert node', () => {
- 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', () => {
- 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', () => {
- 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', () => {
- 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', () => {
- let position = new Position( [ 0 ], root );
- let operation = new InsertOperation(
- position,
- [ 'foo', new Character( 'x' ), 'bar' ],
- 0
- );
- let reverse = operation.getReversed();
- 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', () => {
- let operation = new InsertOperation(
- new Position( [ 0 ], root ),
- new Character( 'x' ),
- doc.version
- );
- let reverse = operation.getReversed();
- 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', () => {
- let operation = new InsertOperation(
- new Position( [ 0 ], root ),
- 'bar',
- doc.version
- );
- let reverse = operation.getReversed();
- 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 create operation with the same parameters when cloned', () => {
- let position = new Position( [ 0 ], root );
- let nodeA = new Node();
- let nodeB = new Node();
- let nodes = new NodeList( [ nodeA, nodeB ] );
- let baseVersion = doc.version;
- let op = new InsertOperation( position, nodes, baseVersion );
- let clone = op.clone();
- // New instance rather than a pointer to the old instance.
- expect( clone ).not.to.be.equal( op );
- expect( clone ).to.be.instanceof( InsertOperation );
- expect( clone.position.isEqual( position ) ).to.be.true;
- expect( clone.nodeList.get( 0 ) ).to.equal( nodeA );
- expect( clone.nodeList.get( 1 ) ).to.equal( nodeB );
- expect( clone.nodeList.length ).to.equal( 2 );
- expect( clone.baseVersion ).to.equal( baseVersion );
- } );
- } );
|