/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * 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 ChangeBuffer from '../src/changebuffer'; import Input from '../src/input'; describe( 'InputCommand', () => { let editor, doc; testUtils.createSinonSandbox(); before( () => { return ModelTestEditor.create( ) .then( newEditor => { editor = newEditor; doc = editor.document; const command = new InputCommand( editor ); editor.commands.set( 'input', command ); doc.schema.registerItem( 'p', '$block' ); doc.schema.registerItem( 'h1', '$block' ); } ); } ); 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 ); } ); } ); } ); describe( 'execute', () => { it( 'uses enqueueChanges', () => { setData( doc, '
foo[]bar
' ); const spy = testUtils.sinon.spy( doc, 'enqueueChanges' ); editor.execute( 'input' ); expect( spy.calledOnce ).to.be.true; } ); it( 'inserts text for collapsed range', () => { setData( doc, 'foo[]
' ); editor.execute( 'input', { text: 'bar', range: editor.document.selection.getFirstRange() } ); expect( getData( doc, { selection: true } ) ).to.be.equal( 'foobar[]
' ); expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 3 ); } ); it( 'replaces text for range within single element on the beginning', () => { setData( doc, '[fooba]r
' ); editor.execute( 'input', { text: 'rab', range: editor.document.selection.getFirstRange() } ); expect( getData( doc, { selection: true } ) ).to.be.equal( 'rab[]r
' ); expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 0 ); } ); it( 'replaces text for range within single element in the middle', () => { setData( doc, 'fo[oba]r
' ); editor.execute( 'input', { text: 'bazz', range: editor.document.selection.getFirstRange() } ); expect( getData( doc, { selection: true } ) ).to.be.equal( 'fobazz[]r
' ); expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 1 ); } ); it( 'replaces text for range within single element on the end', () => { setData( doc, 'fooba[r]
' ); editor.execute( 'input', { text: 'zzz', range: editor.document.selection.getFirstRange() } ); expect( getData( doc, { selection: true } ) ).to.be.equal( 'foobazzz[]
' ); expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 2 ); } ); it( 'replaces text for range within multiple elements', () => { setData( doc, 'b]ar
' ); editor.execute( 'input', { text: 'unny c', range: editor.document.selection.getFirstRange() } ); expect( getData( doc, { selection: true } ) ).to.be.equal( ']ar
' ); expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 3 ); } ); it( 'uses current selection when range is not given', () => { setData( doc, 'foob[ar]
' ); editor.execute( 'input', { text: 'az' } ); expect( getData( doc, { selection: true } ) ).to.be.equal( 'foobaz[]
' ); expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 0 ); } ); it( 'only removes content when text is not given', () => { setData( doc, '[fo]obar
' ); editor.execute( 'input', { range: editor.document.selection.getFirstRange() } ); expect( getData( doc, { selection: true } ) ).to.be.equal( '[]obar
' ); expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 0 ); } ); it( 'does nothing when there is no range', () => { setData( doc, '[fo]obar
' ); testUtils.sinon.stub( editor.document.selection, 'getFirstRange' ).returns( null ); editor.execute( 'input', { text: 'baz' } ); const data = getData( doc, { selection: true } ); editor.document.selection.getFirstRange.restore(); expect( data ).to.be.equal( '[fo]obar
' ); expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 0 ); } ); } ); describe( '_getTextWithinRange', () => { it( 'returns empty text for collapsed selection', () => { setData( doc, '[]foo
' ); expect( editor.commands.get( 'input' )._getTextWithinRange( editor.document.selection.getFirstRange() ) ).to.be.empty; } ); it( 'returns valid text for selection within single element', () => { setData( doc, '[fooBA]R
' ); expect( editor.commands.get( 'input' )._getTextWithinRange( editor.document.selection.getFirstRange() ) ).to.be.equal( 'fooBA' ); } ); it( 'returns valid text for selection within mulitple elements', () => { setData( doc, 'BARb]az
' ); expect( editor.commands.get( 'input' )._getTextWithinRange( editor.document.selection.getFirstRange() ) ).to.be.equal( 'oBARb' ); } ); } ); describe( 'destroy', () => { it( 'should destroy change buffer', () => { const command = editor.commands.get( 'input' ); const destroy = command._buffer.destroy = testUtils.sinon.spy(); command.destroy(); expect( destroy.calledOnce ).to.be.true; expect( command._buffer ).to.be.null; } ); } ); } );