Ver código fonte

Add tests to other TableSelection methods.

Maciej Gołaszewski 5 anos atrás
pai
commit
2c8b2b9254

+ 4 - 4
packages/ckeditor5-table/src/tableselection.js

@@ -35,9 +35,9 @@ export default class TableSelection extends Plugin {
 	 * Flag indicating that table selection is selecting valid ranges in table cell.
 	 *
 	 * @readonly
-	 * @member {Boolean} #hasValidSelection
+	 * @member {Boolean} #isSelectingAndSomethingElse
 	 */
-	get hasValidSelection() {
+	get isSelectingAndSomethingElse() {
 		return this._isSelecting && this._startElement && this._endElement && this._startElement !== this._endElement;
 	}
 
@@ -53,7 +53,7 @@ export default class TableSelection extends Plugin {
 		editor.editing.view.addObserver( MouseSelectionObserver );
 
 		this.listenTo( viewDocument, 'keydown', () => {
-			if ( this.hasValidSelection ) {
+			if ( this.isSelectingAndSomethingElse ) {
 				this.stopSelection();
 				const tableCell = this._startElement;
 				this.clearSelection();
@@ -91,7 +91,7 @@ export default class TableSelection extends Plugin {
 
 			this.setSelectingTo( tableCell );
 
-			if ( this.hasValidSelection ) {
+			if ( this.isSelectingAndSomethingElse ) {
 				domEventData.preventDefault();
 
 				this._updateModelSelection();

+ 80 - 9
packages/ckeditor5-table/tests/tableselection.js

@@ -13,7 +13,7 @@ import TableEditing from '../src/tableediting';
 import TableSelection from '../src/tableselection';
 import { modelTable, viewTable } from './_utils/utils';
 
-describe( 'table selection', () => {
+describe.only( 'table selection', () => {
 	let editor, model, tableSelection, modelRoot;
 
 	beforeEach( async () => {
@@ -37,29 +37,29 @@ describe( 'table selection', () => {
 	} );
 
 	describe( 'TableSelection', () => {
-		describe( 'hasValidSelection()', () => {
+		describe( 'isSelectingAndSomethingElse()', () => {
 			it( 'should be false if selection is not started', () => {
-				expect( tableSelection.hasValidSelection ).to.be.false;
+				expect( tableSelection.isSelectingAndSomethingElse ).to.be.false;
 			} );
 
 			it( 'should be true if selection is selecting two different cells', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
 
-				expect( tableSelection.hasValidSelection ).to.be.true;
+				expect( tableSelection.isSelectingAndSomethingElse ).to.be.true;
 			} );
 
 			it( 'should be false if selection start/end is selecting the same table cell', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
 
-				expect( tableSelection.hasValidSelection ).to.be.false;
+				expect( tableSelection.isSelectingAndSomethingElse ).to.be.false;
 			} );
 
 			it( 'should be false if selection has no end cell', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
 
-				expect( tableSelection.hasValidSelection ).to.be.false;
+				expect( tableSelection.isSelectingAndSomethingElse ).to.be.false;
 			} );
 
 			it( 'should be false if selection has ended', () => {
@@ -67,7 +67,7 @@ describe( 'table selection', () => {
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
 				tableSelection.stopSelection();
 
-				expect( tableSelection.hasValidSelection ).to.be.false;
+				expect( tableSelection.isSelectingAndSomethingElse ).to.be.false;
 			} );
 		} );
 
@@ -205,9 +205,80 @@ describe( 'table selection', () => {
 			} );
 		} );
 
-		describe( 'stopSelection()', () => {} );
+		describe( 'stopSelection()', () => {
+			it( 'should not clear currently selected cells if not cell was passed', () => {
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
+
+				tableSelection.stopSelection();
+
+				assertSelectedCells( [
+					[ 1, 1, 0 ],
+					[ 0, 0, 0 ],
+					[ 0, 0, 0 ]
+				] );
+			} );
+
+			it( 'should change model selection if cell was passed', () => {
+				const spy = sinon.spy();
+
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+
+				model.document.selection.on( 'change', spy );
+				tableSelection.stopSelection( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
+
+				sinon.assert.calledOnce( spy );
+			} );
+
+			it( 'should extend selection to passed table cell', () => {
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+
+				tableSelection.stopSelection( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
+
+				assertSelectedCells( [
+					[ 1, 1, 0 ],
+					[ 0, 0, 0 ],
+					[ 0, 0, 0 ]
+				] );
+			} );
+		} );
+
+		describe( 'clearSelection()', () => {
+			it( 'should not change model selection', () => {
+				const spy = sinon.spy();
+
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
+
+				model.document.selection.on( 'change', spy );
+
+				tableSelection.clearSelection();
+
+				sinon.assert.notCalled( spy );
+			} );
+
+			it( 'should stop selecting mode', () => {
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
+
+				tableSelection.clearSelection();
+
+				expect( tableSelection.isSelectingAndSomethingElse ).to.be.false;
+			} );
+
+			it( 'should not reset model selections', () => {
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
+
+				tableSelection.clearSelection();
 
-		describe( 'clearSelection()', () => {} );
+				assertSelectedCells( [
+					[ 1, 1, 0 ],
+					[ 0, 0, 0 ],
+					[ 0, 0, 0 ]
+				] );
+			} );
+		} );
 
 		describe( '* getSelectedTableCells()', () => {} );
 	} );