/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /* bender-tags: treemodel */ 'use strict'; import Document from '/ckeditor5/core/treemodel/document.js'; import RootElement from '/ckeditor5/core/treemodel/rootelement.js'; import Batch from '/ckeditor5/core/treemodel/batch.js'; import CKEditorError from '/ckeditor5/core/ckeditorerror.js'; describe( 'Document', () => { let doc; beforeEach( () => { doc = new Document(); } ); describe( 'constructor', () => { it( 'should create Document with no data, empty graveyard and empty selection', () => { expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Map ); expect( doc.roots.size ).to.equal( 1 ); expect( doc.graveyard ).to.be.instanceof( RootElement ); expect( doc.graveyard.getChildCount() ).to.equal( 0 ); expect( doc.selection.getRanges().length ).to.equal( 0 ); } ); } ); describe( 'createRoot', () => { it( 'should create a new RootElement, add it to roots map and return it', () => { let root = doc.createRoot( 'root', 'root' ); expect( doc.roots.size ).to.equal( 2 ); expect( root ).to.be.instanceof( RootElement ); expect( root.getChildCount() ).to.equal( 0 ); } ); it( 'should throw an error when trying to create a second root with the same id', () => { doc.createRoot( 'root', 'root' ); expect( () => { doc.createRoot( 'root', 'root' ); } ).to.throw( CKEditorError, /document-createRoot-id-exists/ ); } ); } ); describe( 'getRoot', () => { it( 'should return a RootElement previously created with given id', () => { let newRoot = doc.createRoot( 'root' ); let getRoot = doc.getRoot( 'root' ); expect( getRoot ).to.equal( newRoot ); } ); it( 'should throw an error when trying to get non-existent root', () => { expect( () => { doc.getRoot( 'root' ); } ).to.throw( CKEditorError, /document-getRoot-root-not-exist/ ); } ); } ); describe( 'applyOperation', () => { it( 'should increase document version, execute operation and fire event with proper data', () => { const changeCallback = sinon.spy(); const type = 't'; const data = { data: 'x' }; const batch = 'batch'; let operation = { type: type, delta: { batch: batch }, baseVersion: 0, _execute: sinon.stub().returns( data ) }; doc.on( 'change', changeCallback ); doc.applyOperation( operation ); expect( doc.version ).to.equal( 1 ); sinon.assert.calledOnce( operation._execute ); sinon.assert.calledOnce( changeCallback ); expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type ); expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data ); expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch ); } ); it( 'should throw an error on the operation base version and the document version is different', () => { let operation = { baseVersion: 1 }; expect( () => { doc.applyOperation( operation ); } ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ ); } ); } ); describe( 'batch', () => { it( 'should create a new batch with the document property', () => { const batch = doc.batch(); expect( batch ).to.be.instanceof( Batch ); expect( batch ).to.have.property( 'doc' ).that.equals( doc ); } ); } ); describe( 'enqueue', () => { it( 'should be executed immediately and fire changesDone event', () => { let order = []; doc.on( 'changesDone', () => order.push( 'done' ) ); doc.enqueueChanges( () => order.push( 'enqueue1' ) ); expect( order ).to.have.length( 2 ); expect( order[ 0 ] ).to.equal( 'enqueue1' ); expect( order[ 1 ] ).to.equal( 'done' ); } ); it( 'should fire done every time queue is empty', () => { let order = []; doc.on( 'changesDone', () => order.push( 'done' ) ); doc.enqueueChanges( () => order.push( 'enqueue1' ) ); doc.enqueueChanges( () => order.push( 'enqueue2' ) ); expect( order ).to.have.length( 4 ); expect( order[ 0 ] ).to.equal( 'enqueue1' ); expect( order[ 1 ] ).to.equal( 'done' ); expect( order[ 2 ] ).to.equal( 'enqueue2' ); expect( order[ 3 ] ).to.equal( 'done' ); } ); it( 'should put callbacks in the proper order', () => { let order = []; doc.on( 'changesDone', () => order.push( 'done' ) ); doc.enqueueChanges( () => { order.push( 'enqueue1 start' ); doc.enqueueChanges( () => { order.push( 'enqueue2 start' ); doc.enqueueChanges( () => order.push( 'enqueue4' ) ); order.push( 'enqueue2 end' ); } ); doc.enqueueChanges( () => order.push( 'enqueue3' ) ); order.push( 'enqueue1 end' ); } ); expect( order ).to.have.length( 7 ); expect( order[ 0 ] ).to.equal( 'enqueue1 start' ); expect( order[ 1 ] ).to.equal( 'enqueue1 end' ); expect( order[ 2 ] ).to.equal( 'enqueue2 start' ); expect( order[ 3 ] ).to.equal( 'enqueue2 end' ); expect( order[ 4 ] ).to.equal( 'enqueue3' ); expect( order[ 5 ] ).to.equal( 'enqueue4' ); expect( order[ 6 ] ).to.equal( 'done' ); } ); } ); it( 'should update selection attributes whenever selection gets updated', () => { sinon.spy( doc.selection, '_updateAttributes' ); doc.selection.fire( 'update' ); expect( doc.selection._updateAttributes.called ).to.be.true; } ); it( 'should update selection attributes whenever changes to the document are applied', () => { sinon.spy( doc.selection, '_updateAttributes' ); doc.fire( 'changesDone' ); expect( doc.selection._updateAttributes.called ).to.be.true; } ); } );