8
0
Эх сурвалжийг харах

Tests: Add tests to cover custom keydown handler in restricted editing

panr 6 жил өмнө
parent
commit
38db1f0042

+ 110 - 1
packages/ckeditor5-restricted-editing/tests/restrictededitingmodeediting.js

@@ -17,7 +17,7 @@ import LinkEditing from '@ckeditor/ckeditor5-link/src/linkediting';
 import Typing from '@ckeditor/ckeditor5-typing/src/typing';
 import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
 
-import RestrictedEditingModeEditing from './../src/restrictededitingmodeediting';
+import RestrictedEditingModeEditing, { onKeyDown } from './../src/restrictededitingmodeediting';
 import RestrictedEditingModeNavigationCommand from '../src/restrictededitingmodenavigationcommand';
 import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
 import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
@@ -1378,6 +1378,115 @@ describe( 'RestrictedEditingModeEditing', () => {
 		} );
 	} );
 
+	describe( 'custom keydown behaviour', () => {
+		let view, evtData;
+
+		beforeEach( async () => {
+			editor = await VirtualTestEditor.create( {
+				plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
+			} );
+
+			model = editor.model;
+			view = editor.editing.view;
+
+			sinon.spy( onKeyDown );
+		} );
+
+		afterEach( () => {
+			return editor.destroy();
+		} );
+
+		describe( 'Ctrl+A handler', () => {
+			beforeEach( async () => {
+				evtData = {
+					keyCode: getCode( 'A' ),
+					ctrlKey: true,
+					preventDefault: sinon.spy(),
+					stopPropagation: sinon.spy()
+				};
+			} );
+
+			describe( 'collapsed selection', () => {
+				it( 'should select text only within an exception when selection is inside an exception', () => {
+					setModelData( model, '<paragraph>foo ba[]r baz</paragraph>' );
+
+					const paragraph = model.document.getRoot().getChild( 0 );
+
+					// <paragraph>foo <marker>ba[]r</marker> baz</paragraph>
+					addExceptionMarker( 4, 7, paragraph );
+
+					view.document.fire( 'keydown', evtData );
+
+					sinon.assert.calledOnce( evtData.preventDefault );
+					sinon.assert.calledOnce( evtData.stopPropagation );
+					expect( getModelData( model ) ).to.be.equal( '<paragraph>foo [bar] baz</paragraph>' );
+				} );
+
+				it( 'should select text only within an exception when selection is at the begining of an exception', () => {
+					setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
+
+					const paragraph = model.document.getRoot().getChild( 0 );
+
+					// <paragraph>foo <marker>[]bar</marker> baz</paragraph>
+					addExceptionMarker( 4, 7, paragraph );
+
+					view.document.fire( 'keydown', evtData );
+
+					sinon.assert.calledOnce( evtData.preventDefault );
+					sinon.assert.calledOnce( evtData.stopPropagation );
+					expect( getModelData( model ) ).to.be.equal( '<paragraph>foo [bar] baz</paragraph>' );
+				} );
+
+				it( 'should select text only within an exception when selection is at the end of an exception', () => {
+					setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
+
+					const paragraph = model.document.getRoot().getChild( 0 );
+
+					// <paragraph>foo <marker>bar[]</marker> baz</paragraph>
+					addExceptionMarker( 4, 7, paragraph );
+
+					view.document.fire( 'keydown', evtData );
+
+					sinon.assert.calledOnce( evtData.preventDefault );
+					sinon.assert.calledOnce( evtData.stopPropagation );
+					expect( getModelData( model ) ).to.be.equal( '<paragraph>foo [bar] baz</paragraph>' );
+				} );
+			} );
+
+			describe( 'non-collapsed selection', () => {
+				it( 'should select text within an exception when a whole selection range is inside an exception', () => {
+					setModelData( model, '<paragraph>fo[o ba]r baz</paragraph>' );
+
+					const paragraph = model.document.getRoot().getChild( 0 );
+
+					// <paragraph><marker>fo[o ba]r</marker> baz</paragraph>
+					addExceptionMarker( 0, 7, paragraph );
+
+					view.document.fire( 'keydown', evtData );
+
+					sinon.assert.calledOnce( evtData.preventDefault );
+					sinon.assert.calledOnce( evtData.stopPropagation );
+					expect( getModelData( model ) ).to.be.equal( '<paragraph>[foo bar] baz</paragraph>' );
+				} );
+
+				it( 'should not select text within an exception when a part of the selection range is outside an exception', () => {
+					setModelData( model, '<paragraph>fo[o ba]r baz</paragraph>' );
+
+					const paragraph = model.document.getRoot().getChild( 0 );
+
+					// <paragraph>fo[o <marker>ba]r</marker> baz</paragraph>
+					addExceptionMarker( 8, 11, paragraph );
+
+					view.document.fire( 'keydown', evtData );
+
+					sinon.assert.notCalled( evtData.preventDefault );
+					sinon.assert.notCalled( evtData.stopPropagation );
+					expect( getModelData( model ) ).to.be.equal( '<paragraph>fo[o ba]r baz</paragraph>' );
+				} );
+			} );
+		} );
+	} );
+
 	// Helper method that creates an exception marker inside given parent.
 	// Marker range is set to given position offsets (start, end).
 	function addExceptionMarker( startOffset, endOffset = startOffset, parent, id = 1 ) {