|
|
@@ -3,16 +3,14 @@
|
|
|
* For licensing, see LICENSE.md.
|
|
|
*/
|
|
|
|
|
|
-import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
|
|
|
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
|
|
|
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
|
|
|
-import InputCommand from '../src/inputcommand';
|
|
|
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
|
+import InputCommand from '../src/inputcommand';
|
|
|
import ChangeBuffer from '../src/changebuffer';
|
|
|
-import Input from '../src/input';
|
|
|
|
|
|
describe( 'InputCommand', () => {
|
|
|
- let editor, doc;
|
|
|
+ let editor, doc, buffer;
|
|
|
|
|
|
testUtils.createSinonSandbox();
|
|
|
|
|
|
@@ -21,9 +19,9 @@ describe( 'InputCommand', () => {
|
|
|
.then( newEditor => {
|
|
|
editor = newEditor;
|
|
|
doc = editor.document;
|
|
|
+ buffer = new ChangeBuffer( doc, 20 );
|
|
|
|
|
|
- const command = new InputCommand( editor );
|
|
|
- editor.commands.set( 'input', command );
|
|
|
+ editor.commands.set( 'input', new InputCommand( editor ) );
|
|
|
|
|
|
doc.schema.registerItem( 'p', '$block' );
|
|
|
doc.schema.registerItem( 'h1', '$block' );
|
|
|
@@ -31,29 +29,7 @@ describe( 'InputCommand', () => {
|
|
|
} );
|
|
|
|
|
|
beforeEach( () => {
|
|
|
- editor.commands.get( 'input' )._buffer.size = 0;
|
|
|
- } );
|
|
|
-
|
|
|
- describe( 'buffer', () => {
|
|
|
- it( 'has buffer getter', () => {
|
|
|
- expect( editor.commands.get( 'input' ).buffer ).to.be.an.instanceof( ChangeBuffer );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'has a buffer configured to default value of config.typing.undoStep', () => {
|
|
|
- expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 20 );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'has a buffer configured to config.typing.undoStep', () => {
|
|
|
- return VirtualTestEditor.create( {
|
|
|
- plugins: [ Input ],
|
|
|
- typing: {
|
|
|
- undoStep: 5
|
|
|
- }
|
|
|
- } )
|
|
|
- .then( editor => {
|
|
|
- expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 5 );
|
|
|
- } );
|
|
|
- } );
|
|
|
+ buffer.size = 0;
|
|
|
} );
|
|
|
|
|
|
describe( 'execute', () => {
|
|
|
@@ -62,7 +38,9 @@ describe( 'InputCommand', () => {
|
|
|
|
|
|
const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
|
|
|
|
|
|
- editor.execute( 'input' );
|
|
|
+ editor.execute( 'input', {
|
|
|
+ buffer: buffer
|
|
|
+ } );
|
|
|
|
|
|
expect( spy.calledOnce ).to.be.true;
|
|
|
} );
|
|
|
@@ -71,82 +49,89 @@ describe( 'InputCommand', () => {
|
|
|
setData( doc, '<p>foo[]</p>' );
|
|
|
|
|
|
editor.execute( 'input', {
|
|
|
+ buffer: buffer,
|
|
|
text: 'bar',
|
|
|
range: editor.document.selection.getFirstRange()
|
|
|
} );
|
|
|
|
|
|
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobar[]</p>' );
|
|
|
- expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 3 );
|
|
|
+ expect( buffer.size ).to.be.equal( 3 );
|
|
|
} );
|
|
|
|
|
|
it( 'replaces text for range within single element on the beginning', () => {
|
|
|
setData( doc, '<p>[fooba]r</p>' );
|
|
|
|
|
|
editor.execute( 'input', {
|
|
|
+ buffer: buffer,
|
|
|
text: 'rab',
|
|
|
range: editor.document.selection.getFirstRange()
|
|
|
} );
|
|
|
|
|
|
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>rab[]r</p>' );
|
|
|
- expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 3 );
|
|
|
+ expect( buffer.size ).to.be.equal( 3 );
|
|
|
} );
|
|
|
|
|
|
it( 'replaces text for range within single element in the middle', () => {
|
|
|
setData( doc, '<p>fo[oba]r</p>' );
|
|
|
|
|
|
editor.execute( 'input', {
|
|
|
+ buffer: buffer,
|
|
|
text: 'bazz',
|
|
|
range: editor.document.selection.getFirstRange()
|
|
|
} );
|
|
|
|
|
|
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fobazz[]r</p>' );
|
|
|
- expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 4 );
|
|
|
+ expect( buffer.size ).to.be.equal( 4 );
|
|
|
} );
|
|
|
|
|
|
it( 'replaces text for range within single element on the end', () => {
|
|
|
setData( doc, '<p>fooba[r]</p>' );
|
|
|
|
|
|
editor.execute( 'input', {
|
|
|
+ buffer: buffer,
|
|
|
text: 'zzz',
|
|
|
range: editor.document.selection.getFirstRange()
|
|
|
} );
|
|
|
|
|
|
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobazzz[]</p>' );
|
|
|
- expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 3 );
|
|
|
+ expect( buffer.size ).to.be.equal( 3 );
|
|
|
} );
|
|
|
|
|
|
it( 'replaces text for range within multiple elements', () => {
|
|
|
setData( doc, '<h1>F[OO</h1><p>b]ar</p>' );
|
|
|
|
|
|
editor.execute( 'input', {
|
|
|
+ buffer: buffer,
|
|
|
text: 'unny c',
|
|
|
range: editor.document.selection.getFirstRange()
|
|
|
} );
|
|
|
|
|
|
expect( getData( doc, { selection: true } ) ).to.be.equal( '<h1>Funny c[</h1><p>]ar</p>' );
|
|
|
- expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 6 );
|
|
|
+ expect( buffer.size ).to.be.equal( 6 );
|
|
|
} );
|
|
|
|
|
|
it( 'uses current selection when range is not given', () => {
|
|
|
setData( doc, '<p>foob[ar]</p>' );
|
|
|
|
|
|
editor.execute( 'input', {
|
|
|
+ buffer: buffer,
|
|
|
text: 'az'
|
|
|
} );
|
|
|
|
|
|
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobaz[]</p>' );
|
|
|
- expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 2 );
|
|
|
+ expect( buffer.size ).to.be.equal( 2 );
|
|
|
} );
|
|
|
|
|
|
it( 'only removes content when text is not given', () => {
|
|
|
setData( doc, '<p>[fo]obar</p>' );
|
|
|
|
|
|
editor.execute( 'input', {
|
|
|
+ buffer: buffer,
|
|
|
range: editor.document.selection.getFirstRange()
|
|
|
} );
|
|
|
|
|
|
expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[]obar</p>' );
|
|
|
- expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 0 );
|
|
|
+ expect( buffer.size ).to.be.equal( 0 );
|
|
|
} );
|
|
|
|
|
|
it( 'does nothing when there is no range', () => {
|
|
|
@@ -155,27 +140,36 @@ describe( 'InputCommand', () => {
|
|
|
testUtils.sinon.stub( editor.document.selection, 'getFirstRange' ).returns( null );
|
|
|
|
|
|
editor.execute( 'input', {
|
|
|
+ buffer: buffer,
|
|
|
text: 'baz'
|
|
|
} );
|
|
|
|
|
|
- const data = getData( doc, { selection: true } );
|
|
|
+ expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[fo]obar</p>' );
|
|
|
+ expect( buffer.size ).to.be.equal( 0 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'does nothing when there is no buffer', () => {
|
|
|
+ setData( doc, '<p>[fo]obar</p>' );
|
|
|
|
|
|
- editor.document.selection.getFirstRange.restore();
|
|
|
+ editor.execute( 'input', {
|
|
|
+ text: 'baz',
|
|
|
+ range: editor.document.selection.getFirstRange()
|
|
|
+ } );
|
|
|
|
|
|
- expect( data ).to.be.equal( '<p>[fo]obar</p>' );
|
|
|
- expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 0 );
|
|
|
+ expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[fo]obar</p>' );
|
|
|
+ expect( buffer.size ).to.be.equal( 0 );
|
|
|
} );
|
|
|
- } );
|
|
|
|
|
|
- describe( 'destroy', () => {
|
|
|
- it( 'should destroy change buffer', () => {
|
|
|
- const command = editor.commands.get( 'input' );
|
|
|
- const destroy = command._buffer.destroy = testUtils.sinon.spy();
|
|
|
+ it( 'does nothing when there is no options object provided', () => {
|
|
|
+ setData( doc, '<p>[fo]obar</p>' );
|
|
|
|
|
|
- command.destroy();
|
|
|
+ const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
|
|
|
+
|
|
|
+ editor.execute( 'input' );
|
|
|
|
|
|
- expect( destroy.calledOnce ).to.be.true;
|
|
|
- expect( command._buffer ).to.be.null;
|
|
|
+ expect( spy.callCount ).to.be.equal( 0 );
|
|
|
+ expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[fo]obar</p>' );
|
|
|
+ expect( buffer.size ).to.be.equal( 0 );
|
|
|
} );
|
|
|
} );
|
|
|
} );
|