/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* globals document */
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import Typing from '../src/typing';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
import Position from '@ckeditor/ckeditor5-engine/src/model/position';
import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
describe( 'InputCommand integration', () => {
let editor, doc, viewDocument, boldView, italicView;
beforeEach( () => {
const editorElement = document.createElement( 'div' );
document.body.appendChild( editorElement );
return ClassicTestEditor.create( editorElement, {
plugins: [ Typing, Paragraph, Undo, Bold, Italic, Enter ],
typing: { undoStep: 3 }
} )
.then( newEditor => {
editor = newEditor;
doc = editor.document;
viewDocument = editor.editing.view;
boldView = editor.ui.componentFactory.create( 'bold' );
italicView = editor.ui.componentFactory.create( 'italic' );
} );
} );
afterEach( () => {
return editor.destroy();
} );
function expectOutput( modelOutput, viewOutput ) {
expect( getModelData( editor.document ) ).to.equal( modelOutput );
expect( getViewData( viewDocument ) ).to.equal( viewOutput );
}
function simulateTyping( text ) {
// While typing, every character is an atomic change.
text.split( '' ).forEach( ( character ) => {
editor.execute( 'input', {
text: character
} );
} );
}
function simulateBatches( batches ) {
// Use longer text at once in input command.
batches.forEach( ( batch ) => {
editor.execute( 'input', {
text: batch
} );
} );
}
function setSelection( pathA, pathB ) {
doc.selection.setRanges( [ new Range( new Position( doc.getRoot(), pathA ), new Position( doc.getRoot(), pathB ) ) ] );
}
describe( 'InputCommand integration', () => {
it( 'resets the buffer on typing respecting typing.undoStep', () => {
setModelData( doc, '
0123456789{}
' ); editor.execute( 'undo' ); expectOutput( '0123456{}
' ); editor.execute( 'undo' ); expectOutput( '0123{}
' ); editor.execute( 'redo' ); expectOutput( '0123456{}
' ); } ); it( 'resets the buffer on text insertion respecting typing.undoStep', () => { setModelData( doc, '0123456789{}
' ); editor.execute( 'undo' ); expectOutput( '012345678{}
' ); editor.execute( 'undo' ); expectOutput( '01234{}
' ); editor.execute( 'redo' ); expectOutput( '012345678{}
' ); } ); it( 'resets the buffer when selection changes', () => { setModelData( doc, 'Foo B1a2{}
' ); editor.execute( 'undo' ); expectOutput( 'Foo B1a{r}
' ); editor.execute( 'undo' ); expectOutput( 'Foo B{}ar
' ); editor.execute( 'redo' ); expectOutput( 'Foo B1{}ar
' ); } ); it( 'resets the buffer when selection changes (with enter)', () => { setModelData( doc, 'Foo1
Bar2
Baz{}
' ); editor.execute( 'undo' ); expectOutput( 'Foo1
Bar2
[]
' ); editor.execute( 'undo' ); expectOutput( 'Foo1
Bar2{}
' ); editor.execute( 'undo' ); expectOutput( 'Foo1
Bar{}
' ); editor.execute( 'redo' ); expectOutput( 'Foo1
Bar2{}
' ); } ); it( 'resets the buffer when attribute changes', () => { setModelData( doc, 'Foo Baz{} Bar
' ); editor.execute( 'undo' ); expectOutput( 'Foo Ba{} Bar
' ); editor.execute( 'undo' ); expectOutput( 'Foo B{} Bar
' ); } ); } ); } );