| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import count from '@ckeditor/ckeditor5-utils/src/count';
- import Delta from '../../../src/model/delta/delta';
- import Operation from '../../../src/model/operation/operation';
- import AttributeOperation from '../../../src/model/operation/attributeoperation';
- import InsertOperation from '../../../src/model/operation/insertoperation';
- import MoveOperation from '../../../src/model/operation/moveoperation';
- import NoOperation from '../../../src/model/operation/nooperation';
- import ReinsertOperation from '../../../src/model/operation/reinsertoperation';
- import RemoveOperation from '../../../src/model/operation/removeoperation';
- import RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
- import DeltaFactory from '../../../src/model/delta/deltafactory';
- import Document from '../../../src/model/document';
- import Position from '../../../src/model/position';
- import Range from '../../../src/model/range';
- import { jsonParseStringify } from '../../../tests/model/_utils/utils';
- // Some test examples of operations.
- class FooOperation extends Operation {
- constructor( string, baseVersion ) {
- super( baseVersion );
- this.string = string;
- }
- getReversed() {
- return new BarOperation( this.string, this.baseVersion );
- }
- }
- class BarOperation extends FooOperation {
- getReversed() {
- return new FooOperation( this.string, this.baseVersion );
- }
- }
- class FooDelta extends Delta {
- static get className() {
- return 'tets.delta.foo';
- }
- }
- describe( 'Delta', () => {
- describe( 'constructor()', () => {
- it( 'should create an delta with empty properties', () => {
- const delta = new Delta();
- expect( delta ).to.have.property( 'batch' ).that.is.null;
- expect( delta ).to.have.property( 'operations' ).that.a( 'array' ).and.have.length( 0 );
- } );
- } );
- describe( 'baseVersion', () => {
- it( 'should return baseVersion of first operation in the delta', () => {
- const delta = new Delta();
- delta.addOperation( { baseVersion: 0 } );
- delta.addOperation( { baseVersion: 1 } );
- delta.addOperation( { baseVersion: 2 } );
- expect( delta.baseVersion ).to.equal( 0 );
- } );
- it( 'should change baseVersion of it\'s operations', () => {
- const delta = new Delta();
- delta.addOperation( { baseVersion: 0 } );
- delta.addOperation( { baseVersion: 1 } );
- delta.addOperation( { baseVersion: 2 } );
- delta.baseVersion = 10;
- expect( delta.operations[ 0 ].baseVersion ).to.equal( 10 );
- expect( delta.operations[ 1 ].baseVersion ).to.equal( 11 );
- expect( delta.operations[ 2 ].baseVersion ).to.equal( 12 );
- } );
- } );
- describe( 'addOperation', () => {
- it( 'should add operation to the delta', () => {
- const delta = new Delta();
- const operation = {};
- delta.addOperation( operation );
- expect( delta.operations ).to.have.length( 1 );
- expect( delta.operations[ 0 ] ).to.equal( operation );
- } );
- it( 'should add delta property to the operation', () => {
- const delta = new Delta();
- const operation = {};
- delta.addOperation( operation );
- expect( operation.delta ).to.equal( delta );
- } );
- } );
- describe( 'iterator', () => {
- it( 'should iterate over delta operations', () => {
- const delta = new Delta();
- delta.addOperation( {} );
- delta.addOperation( {} );
- delta.addOperation( {} );
- const totalNumber = count( delta.operations );
- expect( totalNumber ).to.equal( 3 );
- } );
- } );
- describe( 'getReversed', () => {
- it( 'should return empty Delta if there are no operations in delta', () => {
- const delta = new Delta();
- const reversed = delta.getReversed();
- expect( reversed ).to.be.instanceof( Delta );
- expect( reversed.operations.length ).to.equal( 0 );
- } );
- it( 'should return Delta with all operations reversed and their order reversed', () => {
- const delta = new Delta();
- delta.addOperation( new FooOperation( 'a', 1 ) );
- delta.addOperation( new BarOperation( 'b', 2 ) );
- const reversed = delta.getReversed();
- expect( reversed ).to.be.instanceof( Delta );
- expect( reversed.operations.length ).to.equal( 2 );
- expect( reversed.operations[ 0 ] ).to.be.instanceOf( FooOperation );
- expect( reversed.operations[ 0 ].string ).to.equal( 'b' );
- expect( reversed.operations[ 1 ] ).to.be.instanceOf( BarOperation );
- expect( reversed.operations[ 1 ].string ).to.equal( 'a' );
- } );
- } );
- describe( 'toJSON', () => {
- let delta, root, doc;
- before( () => {
- DeltaFactory.register( FooDelta );
- } );
- beforeEach( () => {
- delta = new FooDelta();
- doc = new Document();
- root = doc.createRoot();
- } );
- it( 'should return JSON representation for empty delta', () => {
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: []
- } );
- } );
- it( 'should create delta with AttributeOperation', () => {
- const operation = new AttributeOperation(
- new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
- 'foo',
- true,
- null,
- doc.version
- );
- delta.addOperation( operation );
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: [ jsonParseStringify( operation ) ]
- } );
- } );
- it( 'should create delta with InsertOperation', () => {
- const operation = new InsertOperation(
- new Position( root, [ 0 ] ),
- 'x',
- doc.version
- );
- delta.addOperation( operation );
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: [ jsonParseStringify( operation ) ]
- } );
- } );
- it( 'should create delta with MoveOperation', () => {
- const operation = new MoveOperation(
- new Position( root, [ 0, 0 ] ),
- 1,
- new Position( root, [ 1, 0 ] ),
- doc.version
- );
- delta.addOperation( operation );
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: [ jsonParseStringify( operation ) ]
- } );
- } );
- it( 'should create delta with NoOperation', () => {
- const operation = new NoOperation( 0 );
- delta.addOperation( operation );
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: [ jsonParseStringify( operation ) ]
- } );
- } );
- it( 'should create delta with ReinsertOperation', () => {
- const operation = new ReinsertOperation(
- new Position( doc.graveyard, [ 0 ] ),
- 2,
- new Position( root, [ 0 ] ),
- doc.version
- );
- delta.addOperation( operation );
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: [ jsonParseStringify( operation ) ]
- } );
- } );
- it( 'should create delta with RemoveOperation', () => {
- const operation = new RemoveOperation(
- new Position( root, [ 2 ] ),
- 2,
- new Position( doc.graveyard, [ 0 ] ),
- doc.version
- );
- delta.addOperation( operation );
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: [ jsonParseStringify( operation ) ]
- } );
- } );
- it( 'should create delta with RootAttributeOperation', () => {
- const operation = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
- delta.addOperation( operation );
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: [ jsonParseStringify( operation ) ]
- } );
- } );
- it( 'should remove batch reference', () => {
- delta.batch = { foo: 'bar' };
- expect( jsonParseStringify( delta ) ).to.deep.equal( {
- __className: FooDelta.className,
- operations: []
- } );
- } );
- } );
- } );
|