8
0
فهرست منبع

Add unit tests for link to increase coverage.

Mateusz Samsel 6 سال پیش
والد
کامیت
e081a514b4

+ 1 - 1
packages/ckeditor5-link/src/linkediting.js

@@ -204,7 +204,7 @@ export default class LinkEditing extends Plugin {
 }
 
 export class ManualDecorator {
-	constructor( { id, value, label, attributes } = {} ) {
+	constructor( { id, value, label, attributes } ) {
 		this.id = id;
 
 		this.set( 'value', value );

+ 42 - 21
packages/ckeditor5-link/tests/linkcommand.js

@@ -300,39 +300,60 @@ describe( 'LinkCommand', () => {
 			return editor.destroy();
 		} );
 
-		it( 'should insert additional attributes to link when is created', () => {
-			setData( model, 'foo[]bar' );
+		describe( 'collapsed selection', () => {
+			it( 'should insert additional attributes to link when it is created', () => {
+				setData( model, 'foo[]bar' );
 
-			command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
+				command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
 
-			expect( getData( model ) ).to
-				.equal( 'foo[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">url</$text>]bar' );
-		} );
+				expect( getData( model ) ).to
+					.equal( 'foo[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">url</$text>]bar' );
+			} );
 
-		it( 'should remove additional attributes to link if those are falsy', () => {
-			setData( model, 'foo[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >url</$text>]bar' );
+			it( 'should add additional attributes to link when link is modified', () => {
+				setData( model, 'f<$text linkHref="url">o[]oba</$text>r' );
 
-			command.execute( 'url', { linkManualDecorator0: false, linkManualDecorator1: false } );
+				command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
 
-			expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
-		} );
+				expect( getData( model ) ).to
+					.equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
+			} );
 
-		it( 'should add additional attributes to link when link is modified', () => {
-			setData( model, 'f<$text linkHref="url">o[]oba</$text>r' );
+			it( 'should remove additional attributes to link if those are falsy', () => {
+				setData( model, 'foo<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >u[]rl</$text>bar' );
 
-			command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
+				command.execute( 'url', { linkManualDecorator0: false, linkManualDecorator1: false } );
 
-			expect( getData( model ) ).to
-				.equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
+				expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
+			} );
 		} );
 
-		it( 'should insert additional attributes for range selection', () => {
-			setData( model, 'f[ooba]r' );
+		describe( 'range selection', () => {
+			it( 'should insert additional attributes to link when it is created', () => {
+				setData( model, 'f[ooba]r' );
 
-			command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
+				command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
 
-			expect( getData( model ) ).to
-				.equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
+				expect( getData( model ) ).to
+					.equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
+			} );
+
+			it( 'should add additional attributes to link when link is modified', () => {
+				setData( model, 'f[<$text linkHref="foo">ooba</$text>]r' );
+
+				command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
+
+				expect( getData( model ) ).to
+					.equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
+			} );
+
+			it( 'should remove additional attributes to link if those are falsy', () => {
+				setData( model, 'foo[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >url</$text>]bar' );
+
+				command.execute( 'url', { linkManualDecorator0: false, linkManualDecorator1: false } );
+
+				expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
+			} );
 		} );
 	} );
 } );

+ 55 - 0
packages/ckeditor5-link/tests/linkui.js

@@ -958,6 +958,61 @@ describe( 'LinkUI', () => {
 
 				expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
 			} );
+
+			it( 'should gather information about manual decorators', () => {
+				let editor, model, formView;
+				const editorElement = document.createElement( 'div' );
+				document.body.appendChild( editorElement );
+
+				return ClassicTestEditor
+					.create( editorElement, {
+						plugins: [ LinkEditing, LinkUI, Paragraph ],
+						link: {
+							decorators: [
+								{
+									mode: 'manual',
+									label: 'Foo',
+									attributes: {
+										foo: 'bar'
+									}
+								}
+							]
+						}
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+						model = editor.model;
+
+						model.schema.extend( '$text', {
+							allowIn: '$root',
+							allowAttributes: 'linkHref'
+						} );
+
+						const linkUIFeature = editor.plugins.get( LinkUI );
+						const balloon = editor.plugins.get( ContextualBalloon );
+
+						formView = linkUIFeature.formView;
+
+						// There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
+						testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
+						testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
+
+						formView.render();
+					} )
+					.then( () => {
+						const executeSpy = testUtils.sinon.spy( editor, 'execute' );
+
+						setModelData( model, 'f[<$text linkHref="url" linkManualDecorator0="true">ooba</$text>]r' );
+						expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
+						expect( formView.customAttributesView.length ).to.equal( 1 );
+						expect( formView.customAttributesView.first.isOn ).to.be.true;
+
+						formView.fire( 'submit' );
+
+						expect( executeSpy.calledOnce ).to.be.true;
+						expect( executeSpy.calledWithExactly( 'link', 'url', { linkManualDecorator0: true } ) ).to.be.true;
+					} );
+			} );
 		} );
 	} );
 } );