Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #175 from ckeditor/t/174
Browse files Browse the repository at this point in the history
Other: `ComponentFactory` will throw an error when attempting to create a non-existent component. Closes #174.
  • Loading branch information
Reinmar authored Mar 27, 2017
2 parents 5f1e584 + 28ee3df commit ef0a7f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/componentfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export default class ComponentFactory {
*/
add( name, callback ) {
if ( this._components.get( name ) ) {
/**
* The item already exists in the component factory.
*
* @error componentfactory-item-exists
* @param {String} name The name of the component.
*/
throw new CKEditorError(
'componentfactory-item-exists: The item already exists in the component factory.', { name }
);
Expand All @@ -68,6 +74,20 @@ export default class ComponentFactory {
* @returns {module:ui/view~View} The instantiated component view.
*/
create( name ) {
return this._components.get( name )( this.editor.locale );
const component = this._components.get( name );

if ( !component ) {
/**
* There is no such UI component in the factory.
*
* @error componentfactory-item-missing
* @param {String} name The name of the missing component.
*/
throw new CKEditorError(
'componentfactory-item-missing: There is no such UI component in the factory.', { name }
);
}

return component( this.editor.locale );
}
}
6 changes: 6 additions & 0 deletions tests/componentfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ describe( 'ComponentFactory', () => {
} );

describe( 'create', () => {
it( 'throws when trying to create a component which has not been registered', () => {
expect( () => {
factory.create( 'foo' );
} ).to.throw( CKEditorError, /^componentfactory-item-missing/ );
} );

it( 'creates an instance', () => {
class View {
constructor( locale ) {
Expand Down

0 comments on commit ef0a7f8

Please sign in to comment.