Kaynağa Gözat

Add tests for TableSelection view selection.

Maciej Gołaszewski 7 yıl önce
ebeveyn
işleme
cc86fe1a0e

+ 1 - 0
packages/ckeditor5-table/src/tableselection.js

@@ -118,6 +118,7 @@ export default class TableSelection extends Plugin {
 		this.editor.editing.view.change( writer => {
 			for ( const previouslyHighlighted of previous ) {
 				if ( !selected.includes( previouslyHighlighted ) ) {
+					// TODO: unify somewhere...
 					writer.removeClass( 'selected', previouslyHighlighted );
 				}
 			}

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

@@ -9,6 +9,7 @@ import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { defaultConversion, defaultSchema, modelTable } from './_utils/utils';
 
 import TableSelection from '../src/tableselection';
+import { getData as getViewData } from '../../ckeditor5-engine/src/dev-utils/view';
 
 describe( 'TableSelection', () => {
 	let editor, model, root, tableSelection;
@@ -63,7 +64,32 @@ describe( 'TableSelection', () => {
 
 			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ nodeByPath ] );
 		} );
+
+		it( 'should set view selection', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01', '02' ],
+				[ '10', '11', '12' ]
+			] ) );
+
+			tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
+
+			expect( getViewData( editor.editing.view ) ).to.equal(
+				'<figure class="table">' +
+					'<table>' +
+						'<tbody>' +
+							'<tr>' +
+								'[<td>00</td>]<td>01</td><td>02</td>' +
+							'</tr>' +
+							'<tr>' +
+								'<td>10</td><td>11</td><td>12</td>' +
+							'</tr>' +
+						'</tbody>' +
+					'</table>' +
+				'</figure>'
+			);
+		} );
 	} );
+
 	describe( 'stop()', () => {
 		it( 'should stop selection', () => {
 			setData( model, modelTable( [
@@ -136,6 +162,34 @@ describe( 'TableSelection', () => {
 			expect( tableSelection.isSelecting ).to.be.false;
 			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
 		} );
+
+		it( 'should update view 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.stopSelection( firstEndNode );
+
+			expect( getViewData( editor.editing.view ) ).to.equal(
+				'<figure class="table">' +
+					'<table>' +
+						'<tbody>' +
+							'<tr>' +
+								'[<td>00</td>][<td>01</td>]<td>02</td>' +
+							'</tr>' +
+							'<tr>' +
+								'<td>10</td><td>11</td><td>12</td>' +
+							'</tr>' +
+						'</tbody>' +
+					'</table>' +
+				'</figure>'
+			);
+		} );
 	} );
 
 	describe( 'update()', () => {
@@ -199,6 +253,34 @@ describe( 'TableSelection', () => {
 			expect( tableSelection.isSelecting ).to.be.true;
 			expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
 		} );
+
+		it( 'should update view 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( getViewData( editor.editing.view ) ).to.equal(
+				'<figure class="table">' +
+					'<table>' +
+						'<tbody>' +
+							'<tr>' +
+								'[<td>00</td>][<td>01</td>]<td>02</td>' +
+							'</tr>' +
+							'<tr>' +
+								'<td>10</td><td>11</td><td>12</td>' +
+							'</tr>' +
+						'</tbody>' +
+					'</table>' +
+				'</figure>'
+			);
+		} );
 	} );
 
 	describe( 'getSelection()', () => {