8
0
Просмотр исходного кода

Add plugincollection.has() method.

Maciej Gołaszewski 7 лет назад
Родитель
Сommit
3b80af088f

+ 12 - 0
packages/ckeditor5-core/src/plugincollection.js

@@ -72,6 +72,8 @@ export default class PluginCollection {
 	/**
 	 * Gets the plugin instance by its constructor or name.
 	 *
+	 * **Note**: This method will throw error if plugin is not loaded. Use {@link #has} to check if plugin is available.
+	 *
 	 * @param {Function|String} key The plugin constructor or {@link module:core/plugin~PluginInterface.pluginName name}.
 	 * @returns {module:core/plugin~PluginInterface}
 	 */
@@ -104,6 +106,16 @@ export default class PluginCollection {
 		return plugin;
 	}
 
+	/**
+	 * Checks if plugin is loaded.
+	 *
+	 * @param {Function|String} key The plugin constructor or {@link module:core/plugin~PluginInterface.pluginName name}.
+	 * @returns {Boolean}
+	 */
+	has( key ) {
+		return this._plugins.has( key );
+	}
+
 	/**
 	 * Loads a set of plugins and adds them to the collection.
 	 *

+ 31 - 0
packages/ckeditor5-core/tests/plugincollection.js

@@ -485,6 +485,37 @@ describe( 'PluginCollection', () => {
 		} );
 	} );
 
+	describe( 'has()', () => {
+		let plugins;
+
+		beforeEach( () => {
+			plugins = new PluginCollection( editor, availablePlugins );
+		} );
+
+		it( 'returns false if plugins is not loaded (retrieved by name)', () => {
+			expect( plugins.has( 'foobar' ) ).to.be.false;
+		} );
+
+		it( 'returns false if plugins is not loaded (retrieved by class)', () => {
+			class SomePlugin extends Plugin {
+			}
+
+			expect( plugins.has( SomePlugin ) ).to.be.false;
+		} );
+
+		it( 'returns true if plugins is loaded (retrieved by name)', () => {
+			return plugins.load( [ PluginA ] ).then( () => {
+				expect( plugins.has( 'A' ) ).to.be.true;
+			} );
+		} );
+
+		it( 'returns true if plugins is loaded (retrieved by class)', () => {
+			return plugins.load( [ PluginA ] ).then( () => {
+				expect( plugins.has( PluginA ) ).to.be.true;
+			} );
+		} );
+	} );
+
 	describe( 'destroy()', () => {
 		it( 'calls Plugin#destroy() method on every loaded plugin', () => {
 			let destroySpyForPluginA, destroySpyForPluginB;