Procházet zdrojové kódy

Add unit test for link commands with manual decorator.

Mateusz Samsel před 6 roky
rodič
revize
3cfff35cec

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

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

+ 76 - 0
packages/ckeditor5-link/tests/linkcommand.js

@@ -5,6 +5,7 @@
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
 import LinkCommand from '../src/linkcommand';
+import { ManualDecorator } from '../src/linkediting';
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 describe( 'LinkCommand', () => {
@@ -259,4 +260,79 @@ describe( 'LinkCommand', () => {
 			} );
 		} );
 	} );
+
+	describe( 'manual decorators', () => {
+		beforeEach( () => {
+			editor.destroy();
+			return ModelTestEditor.create()
+				.then( newEditor => {
+					editor = newEditor;
+					model = editor.model;
+					command = new LinkCommand( editor );
+
+					command.customAttributes.add( new ManualDecorator( {
+						id: 'linkManualDecorator0',
+						value: undefined,
+						label: 'Foo',
+						attributes: {
+							class: 'Foo'
+						}
+					} ) );
+					command.customAttributes.add( new ManualDecorator( {
+						id: 'linkManualDecorator1',
+						value: undefined,
+						label: 'Bar',
+						attributes: {
+							target: '_blank'
+						}
+					} ) );
+
+					model.schema.extend( '$text', {
+						allowIn: '$root',
+						allowAttributes: 'linkHref'
+					} );
+
+					model.schema.register( 'p', { inheritAllFrom: '$block' } );
+				} );
+		} );
+
+		afterEach( () => {
+			return editor.destroy();
+		} );
+
+		it( 'should insert additional attributes to link when is created', () => {
+			setData( model, 'foo[]bar' );
+
+			command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
+
+			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' );
+
+			command.execute( 'url', { linkManualDecorator0: false, linkManualDecorator1: false } );
+
+			expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">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: true, linkManualDecorator1: true } );
+
+			expect( getData( model ) ).to
+				.equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
+		} );
+
+		it( 'should insert additional attributes for range selection', () => {
+			setData( model, 'f[ooba]r' );
+
+			command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
+
+			expect( getData( model ) ).to
+				.equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
+		} );
+	} );
 } );

+ 53 - 0
packages/ckeditor5-link/tests/unlinkcommand.js

@@ -5,6 +5,7 @@
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
 import UnlinkCommand from '../src/unlinkcommand';
+import LinkEditing from '../src/linkediting';
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
@@ -227,4 +228,56 @@ describe( 'UnlinkCommand', () => {
 			} );
 		} );
 	} );
+
+	describe.skip( 'manual decorators', () => {
+		beforeEach( () => {
+			editor.destroy();
+			return ModelTestEditor.create( {
+				extraPlugins: [ LinkEditing ],
+				link: {
+					decorators: [
+						{
+							mode: 'manual',
+							label: 'Foo',
+							attributes: {
+								class: 'foo'
+							}
+						},
+						{
+							mode: 'manual',
+							label: 'Bar',
+							attributes: {
+								target: '_blank'
+							}
+						}
+					]
+				}
+			} )
+				.then( newEditor => {
+					editor = newEditor;
+					model = editor.model;
+					document = model.document;
+					command = new UnlinkCommand( editor );
+
+					model.schema.extend( '$text', {
+						allowIn: '$root',
+						allowAttributes: 'linkHref'
+					} );
+
+					model.schema.register( 'p', { inheritAllFrom: '$block' } );
+				} );
+		} );
+
+		afterEach( () => {
+			return editor.destroy();
+		} );
+
+		it( 'should remove manual decorators from links together with linkHref', () => {
+			setData( model, '<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >f[]oobar</$text>' );
+
+			command.execute();
+
+			expect( getData( model ) ).to.equal( 'f[]oobar' );
+		} );
+	} );
 } );