Parcourir la source

Added tests for changed API.

Kuba Niegowski il y a 5 ans
Parent
commit
fd9565017c

+ 146 - 1
packages/ckeditor5-table/tests/tableclipboard-paste.js

@@ -23,7 +23,7 @@ import TableEditing from '../src/tableediting';
 import TableCellPropertiesEditing from '../src/tablecellproperties/tablecellpropertiesediting';
 import TableWalker from '../src/tablewalker';
 
-import TableClipboard from '../src/tableclipboard';
+import TableClipboard, { getTableIfOnlyTableInContent } from '../src/tableclipboard';
 
 describe( 'table clipboard', () => {
 	let editor, model, modelRoot, tableSelection, viewDocument, element;
@@ -478,6 +478,30 @@ describe( 'table clipboard', () => {
 			] ) );
 		} );
 
+		it( '#_replaceTableSlotCell() should be overridable', () => {
+			const tableClipboard = editor.plugins.get( 'TableClipboard' );
+
+			tableClipboard.on( '_replaceTableSlotCell', ( evt, args ) => {
+				const [ /* tableSlot */, cellToInsert, /* insertPosition */, writer ] = args;
+
+				if ( cellToInsert ) {
+					writer.setAttribute( 'foo', 'bar', cellToInsert );
+				}
+			}, { priority: 'high' } );
+
+			pasteTable( [
+				[ 'aa', 'ab' ],
+				[ 'ba', 'bb' ]
+			] );
+
+			assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
+				[ { contents: 'aa', foo: 'bar' }, { contents: 'ab', foo: 'bar' }, '02', '03' ],
+				[ { contents: 'ba', foo: 'bar' }, { contents: 'bb', foo: 'bar' }, '12', '13' ],
+				[ '20', '21', '22', '23' ],
+				[ '30', '31', '32', '33' ]
+			] ) );
+		} );
+
 		describe( 'single cell selected', () => {
 			beforeEach( () => {
 				setModelData( model, modelTable( [
@@ -3949,6 +3973,127 @@ describe( 'table clipboard', () => {
 		} );
 	} );
 
+	describe( 'getTableIfOnlyTableInContent helper', () => {
+		beforeEach( async () => {
+			await createEditor();
+		} );
+
+		it( 'should return null for no table provided', () => {
+			setModelData( model, '<paragraph>foo</paragraph>' );
+
+			const content = modelRoot.getChild( 0 );
+
+			expect( getTableIfOnlyTableInContent( content, model ) ).to.be.null;
+		} );
+
+		it( 'should return null for a text node provided', async () => {
+			setModelData( model, '<paragraph>foo</paragraph>' );
+
+			const content = modelRoot.getNodeByPath( [ 0, 0 ] );
+
+			expect( getTableIfOnlyTableInContent( content, model ) ).to.be.null;
+		} );
+
+		it( 'should return null for mixed content provided (table + paragraph)', () => {
+			setModelData( model,
+				'<table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>' +
+				'<paragraph>foo</paragraph>'
+			);
+
+			const content = documentFragmentFromChildren( modelRoot );
+
+			expect( getTableIfOnlyTableInContent( content, model ) ).to.be.null;
+		} );
+
+		it( 'should return null for mixed content provided (paragraph + table)', () => {
+			setModelData( model,
+				'<paragraph>foo</paragraph>' +
+				'<table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>'
+			);
+
+			const content = documentFragmentFromChildren( modelRoot );
+
+			expect( getTableIfOnlyTableInContent( content, model ) ).to.be.null;
+		} );
+
+		it( 'should return table element for mixed content provided (table + empty paragraph)', () => {
+			setModelData( model,
+				'<table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>' +
+				'<paragraph></paragraph>'
+			);
+
+			const content = documentFragmentFromChildren( modelRoot );
+			const result = getTableIfOnlyTableInContent( content, model );
+
+			expect( result ).to.be.not.null;
+			expect( result.is( 'element', 'table' ) ).to.be.true;
+		} );
+
+		it( 'should return table element for mixed content provided (table + empty paragraph)', () => {
+			setModelData( model,
+				'<paragraph></paragraph>' +
+				'<table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>'
+			);
+
+			const content = documentFragmentFromChildren( modelRoot );
+			const result = getTableIfOnlyTableInContent( content, model );
+
+			expect( result ).to.be.not.null;
+			expect( result.is( 'element', 'table' ) ).to.be.true;
+		} );
+
+		it( 'should return table element for mixed content provided (p + p + table + p)', () => {
+			setModelData( model,
+				'<paragraph></paragraph>' +
+				'<paragraph></paragraph>' +
+				'<table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>' +
+				'<paragraph></paragraph>'
+			);
+
+			const content = documentFragmentFromChildren( modelRoot );
+			const result = getTableIfOnlyTableInContent( content, model );
+
+			expect( result ).to.be.not.null;
+			expect( result.is( 'element', 'table' ) ).to.be.true;
+		} );
+
+		it( 'should return table element for if table is the only element provided in document fragment', () => {
+			setModelData( model,
+				'<table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>'
+			);
+
+			const content = documentFragmentFromChildren( modelRoot );
+			const result = getTableIfOnlyTableInContent( content, model );
+
+			expect( result ).to.be.not.null;
+			expect( result.is( 'element', 'table' ) ).to.be.true;
+		} );
+
+		it( 'should return table element for if table is the only element provided directly', () => {
+			setModelData( model,
+				'<table><tableRow><tableCell><paragraph>bar</paragraph></tableCell></tableRow></table>'
+			);
+
+			const content = modelRoot.getChild( 0 );
+			const result = getTableIfOnlyTableInContent( content, model );
+
+			expect( result ).to.be.not.null;
+			expect( result.is( 'element', 'table' ) ).to.be.true;
+		} );
+
+		function documentFragmentFromChildren( element ) {
+			return model.change( writer => {
+				const documentFragment = writer.createDocumentFragment();
+
+				for ( const child of element.getChildren() ) {
+					writer.insert( writer.cloneElement( child ), documentFragment, 'end' );
+				}
+
+				return documentFragment;
+			} );
+		}
+	} );
+
 	async function createEditor( extraPlugins = [] ) {
 		editor = await ClassicTestEditor.create( element, {
 			plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard, ...extraPlugins ]

+ 44 - 0
packages/ckeditor5-table/tests/tableutils.js

@@ -5,6 +5,7 @@
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
@@ -16,6 +17,8 @@ import TableUtils from '../src/tableutils';
 describe( 'TableUtils', () => {
 	let editor, model, root, tableUtils;
 
+	testUtils.createSinonSandbox();
+
 	beforeEach( () => {
 		return ModelTestEditor.create( {
 			plugins: [ Paragraph, TableEditing, TableUtils ]
@@ -51,6 +54,27 @@ describe( 'TableUtils', () => {
 	} );
 
 	describe( 'insertRows()', () => {
+		it( 'should be decorated', () => {
+			const spy = sinon.spy();
+
+			setData( model, modelTable( [
+				[ '11[]', '12' ],
+				[ '21', '22' ]
+			] ) );
+
+			tableUtils.on( 'insertRows', spy );
+
+			tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
+
+			assertEqualMarkup( getData( model ), modelTable( [
+				[ '11[]', '12' ],
+				[ '', '' ],
+				[ '21', '22' ]
+			] ) );
+
+			expect( spy.calledOnce ).to.be.true;
+		} );
+
 		it( 'should insert row in given table at given index', () => {
 			setData( model, modelTable( [
 				[ '11[]', '12' ],
@@ -335,6 +359,26 @@ describe( 'TableUtils', () => {
 	} );
 
 	describe( 'insertColumns()', () => {
+		it( 'should be decorated', () => {
+			const spy = sinon.spy();
+
+			setData( model, modelTable( [
+				[ '11[]', '12' ],
+				[ '21', '22' ]
+			] ) );
+
+			tableUtils.on( 'insertColumns', spy );
+
+			tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
+
+			assertEqualMarkup( getData( model ), modelTable( [
+				[ '11[]', '', '12' ],
+				[ '21', '', '22' ]
+			] ) );
+
+			expect( spy.calledOnce ).to.be.true;
+		} );
+
 		it( 'should insert column in given table at given index', () => {
 			setData( model, modelTable( [
 				[ '11[]', '12' ],