Selaa lähdekoodia

Add tests for TableSelection class methods.

Maciej Gołaszewski 7 vuotta sitten
vanhempi
commit
7667207b71

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

@@ -51,19 +51,19 @@ export default class TableSelection extends Plugin {
 	}
 
 	updateSelection( tableCell ) {
-		if ( tableCell && tableCell.parent.parent === this._startElement.parent.parent ) {
+		if ( this.isSelecting && tableCell && tableCell.parent.parent === this._startElement.parent.parent ) {
 			this._endElement = tableCell;
 		}
+
 		this._redrawSelection();
 	}
 
 	stopSelection( tableCell ) {
-		this._isSelecting = false;
-
-		if ( tableCell && tableCell.parent.parent === this._startElement.parent.parent ) {
+		if ( this.isSelecting && tableCell && tableCell.parent.parent === this._startElement.parent.parent ) {
 			this._endElement = tableCell;
 		}
 
+		this._isSelecting = false;
 		this._redrawSelection();
 	}
 

+ 180 - 0
packages/ckeditor5-table/tests/tableselection.js

@@ -64,4 +64,184 @@ describe( 'TableSelection', () => {
 			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ nodeByPath ] );
 		} );
 	} );
+	describe( 'stop()', () => {
+		it( 'should stop selection', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01' ],
+				[ '10', '11' ]
+			] ) );
+
+			const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
+
+			tableSelection.startSelection( nodeByPath );
+			expect( tableSelection.isSelecting ).to.be.true;
+
+			tableSelection.stopSelection( nodeByPath );
+
+			expect( tableSelection.isSelecting ).to.be.false;
+		} );
+
+		it( 'update selection to passed table cell', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01' ],
+				[ '10', '11' ]
+			] ) );
+
+			const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
+			const endNode = root.getNodeByPath( [ 0, 1, 1 ] );
+
+			tableSelection.startSelection( startNode );
+			tableSelection.stopSelection( endNode );
+
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
+				startNode,
+				root.getNodeByPath( [ 0, 0, 1 ] ),
+				root.getNodeByPath( [ 0, 1, 0 ] ),
+				endNode
+			] );
+		} );
+
+		it( 'should not update selection if alredy stopped', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01', '02' ],
+				[ '10', '11', '12' ]
+			] ) );
+
+			const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
+			const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
+
+			tableSelection.startSelection( startNode );
+			tableSelection.stopSelection( firstEndNode );
+
+			expect( tableSelection.isSelecting ).to.be.false;
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
+
+			const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
+			tableSelection.stopSelection( secondEndNode );
+
+			expect( tableSelection.isSelecting ).to.be.false;
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
+		} );
+
+		it( 'should not update selection if table cell is from another parent', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01' ]
+			] ) + modelTable( [
+				[ 'aa', 'bb' ]
+			] ) );
+
+			tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
+			tableSelection.stopSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
+
+			expect( tableSelection.isSelecting ).to.be.false;
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
+		} );
+	} );
+
+	describe( 'update()', () => {
+		it( 'should update selection', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01', '02' ],
+				[ '10', '11', '12' ]
+			] ) );
+
+			const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
+			const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
+
+			tableSelection.startSelection( startNode );
+			tableSelection.updateSelection( firstEndNode );
+
+			expect( tableSelection.isSelecting ).to.be.true;
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
+
+			const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
+			tableSelection.updateSelection( secondEndNode );
+
+			expect( tableSelection.isSelecting ).to.be.true;
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode, secondEndNode ] );
+		} );
+
+		it( 'should not update selection if stopped', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01', '02' ],
+				[ '10', '11', '12' ]
+			] ) );
+
+			const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
+			const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
+
+			tableSelection.startSelection( startNode );
+			tableSelection.updateSelection( firstEndNode );
+
+			expect( tableSelection.isSelecting ).to.be.true;
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
+
+			tableSelection.stopSelection( firstEndNode );
+			expect( tableSelection.isSelecting ).to.be.false;
+
+			const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
+			tableSelection.updateSelection( secondEndNode );
+
+			expect( tableSelection.isSelecting ).to.be.false;
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
+		} );
+
+		it( 'should not update selection if table cell is from another parent', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01' ]
+			] ) + modelTable( [
+				[ 'aa', 'bb' ]
+			] ) );
+
+			tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
+			tableSelection.updateSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
+
+			expect( tableSelection.isSelecting ).to.be.true;
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
+		} );
+	} );
+
+	describe( 'getSelection()', () => {
+		it( 'should return empty array if not started', () => {
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [] );
+		} );
+
+		it( 'should return block of selected nodes', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01', '02', '03' ],
+				[ '10', '11', '12', '13' ],
+				[ '20', '21', '22', '23' ],
+				[ '30', '31', '32', '33' ]
+			] ) );
+
+			tableSelection.startSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
+			tableSelection.updateSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
+
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
+				root.getNodeByPath( [ 0, 1, 1 ] ),
+				root.getNodeByPath( [ 0, 1, 2 ] ),
+				root.getNodeByPath( [ 0, 2, 1 ] ),
+				root.getNodeByPath( [ 0, 2, 2 ] )
+			] );
+		} );
+
+		it( 'should return block of selected nodes (inverted selection)', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01', '02', '03' ],
+				[ '10', '11', '12', '13' ],
+				[ '20', '21', '22', '23' ],
+				[ '30', '31', '32', '33' ]
+			] ) );
+
+			tableSelection.startSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
+			tableSelection.updateSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
+
+			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
+				root.getNodeByPath( [ 0, 1, 1 ] ),
+				root.getNodeByPath( [ 0, 1, 2 ] ),
+				root.getNodeByPath( [ 0, 2, 1 ] ),
+				root.getNodeByPath( [ 0, 2, 2 ] )
+			] );
+		} );
+	} );
 } );