8
0

componentfactory.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  6. import ComponentFactory from '../src/componentfactory';
  7. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. describe( 'ComponentFactory', () => {
  9. let editor, factory;
  10. beforeEach( () => {
  11. editor = new Editor();
  12. factory = new ComponentFactory( editor );
  13. } );
  14. describe( 'constructor()', () => {
  15. it( 'sets all the properties', () => {
  16. expect( factory ).to.have.property( 'editor', editor );
  17. } );
  18. } );
  19. describe( 'add', () => {
  20. it( 'throws when trying to override already registered component', () => {
  21. factory.add( 'foo', () => {} );
  22. expect( () => {
  23. factory.add( 'foo', () => {} );
  24. } ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
  25. } );
  26. } );
  27. describe( 'create', () => {
  28. it( 'throws when trying to create a component which has not been registered', () => {
  29. expect( () => {
  30. factory.create( 'foo' );
  31. } ).to.throw( CKEditorError, /^componentfactory-item-missing/ );
  32. } );
  33. it( 'creates an instance', () => {
  34. class View {
  35. constructor( locale ) {
  36. this.locale = locale;
  37. }
  38. }
  39. const locale = editor.locale = {};
  40. factory.add( 'foo', locale => new View( locale ) );
  41. const instance = factory.create( 'foo' );
  42. expect( instance ).to.be.instanceof( View );
  43. expect( instance.locale ).to.equal( locale );
  44. } );
  45. } );
  46. } );