Browse Source

Add unit test for automaticdispatcher, fix typos.

Mateusz Samsel 6 years ago
parent
commit
8f2200555a

+ 5 - 2
packages/ckeditor5-link/src/linkediting.js

@@ -78,6 +78,7 @@ export default class LinkEditing extends Plugin {
 		editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
 
 		const linkDecorators = editor.config.get( 'link.decorators' ) || [];
+
 		this.enableAutomaticDecorators( linkDecorators.filter( item => item.mode === AUTO ) );
 		this.enableManualDecorators( linkDecorators.filter( item => item.mode === MANUAL ) );
 
@@ -108,15 +109,17 @@ export default class LinkEditing extends Plugin {
 		}
 
 		automaticDecorators.add( automaticDecoratorDefinitions );
-		editor.conversion.for( 'downcast' ).add( automaticDecorators.getDispatcher() );
+		if ( automaticDecorators.length ) {
+			editor.conversion.for( 'downcast' ).add( automaticDecorators.getDispatcher() );
+		}
 	}
 
 	enableManualDecorators( manualDecoratorDefinitions ) {
-		const editor = this.editor;
 		if ( !manualDecoratorDefinitions.length ) {
 			return;
 		}
 
+		const editor = this.editor;
 		const command = editor.commands.get( 'link' );
 		const attrCollection = command.customAttributes;
 

+ 1 - 1
packages/ckeditor5-link/src/utils/automaticdecorators.js

@@ -23,7 +23,7 @@ export default class AutomaticDecorators {
 	}
 
 	/**
-	 * Add item or array of items with autoamtic rules for applying decorators to link plugin.
+	 * Add item or array of items with automatic rules for applying decorators to link plugin.
 	 *
 	 * @param {Object|Array.<Object>} item configuration object of automatic rules for decorating links.
 	 * It might be also array of such objects.

+ 94 - 0
packages/ckeditor5-link/tests/utils/automaticdecorators.js

@@ -0,0 +1,94 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import AutomaticDecorators from '../../src/utils/automaticdecorators';
+
+describe( 'Automatic Decorators', () => {
+	let automaticDecorators;
+	beforeEach( () => {
+		automaticDecorators = new AutomaticDecorators();
+	} );
+
+	describe( 'constructor()', () => {
+		it( 'initialise with empty Set', () => {
+			expect( automaticDecorators._definitions ).to.be.instanceOf( Set );
+		} );
+	} );
+
+	describe( 'add()', () => {
+		const tests = [
+			{
+				mode: 'automatic',
+				callback: () => {},
+				attributes: {
+					foo: 'bar'
+				}
+			},
+			{
+				mode: 'automatic',
+				callback: () => {},
+				attributes: {
+					bar: 'baz'
+				}
+			},
+			{
+				mode: 'automatic',
+				callback: () => {},
+				attributes: {
+					test1: 'one',
+					test2: 'two',
+					test3: 'three'
+				}
+			}
+		];
+		it( 'can accept single object', () => {
+			expect( automaticDecorators._definitions.size ).to.equal( 0 );
+
+			automaticDecorators.add( tests[ 0 ] );
+			expect( automaticDecorators._definitions.size ).to.equal( 1 );
+
+			const firstValue = automaticDecorators._definitions.values().next().value;
+
+			expect( firstValue ).to.deep.include( {
+				mode: 'automatic',
+				attributes: {
+					foo: 'bar'
+				}
+			} );
+			expect( firstValue ).to.have.property( 'callback' );
+			expect( firstValue.callback ).to.be.a( 'function' );
+		} );
+
+		it( 'can accept array of objects', () => {
+			expect( automaticDecorators._definitions.size ).to.equal( 0 );
+
+			automaticDecorators.add( tests );
+
+			expect( automaticDecorators._definitions.size ).to.equal( 3 );
+
+			const setIterator = automaticDecorators._definitions.values();
+			setIterator.next();
+			setIterator.next();
+			const thirdValue = setIterator.next().value;
+
+			expect( thirdValue ).to.deep.include( {
+				mode: 'automatic',
+				attributes: {
+					test1: 'one',
+					test2: 'two',
+					test3: 'three'
+				}
+			} );
+			expect( thirdValue ).to.have.property( 'callback' );
+			expect( thirdValue.callback ).to.be.a( 'function' );
+		} );
+	} );
+
+	describe( 'getDispatcher()', () => {
+		it( 'should return a dispatcher function', () => {
+			expect( automaticDecorators.getDispatcher() ).to.be.a( 'function' );
+		} );
+	} );
+} );