componentfactory.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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( 'names()', () => {
  20. it( 'returns iterator', () => {
  21. const names = factory.names();
  22. expect( names.next ).to.be.a( 'function' );
  23. } );
  24. it( 'returns iterator of command names', () => {
  25. factory.add( 'foo', () => {} );
  26. factory.add( 'bar', () => {} );
  27. expect( Array.from( factory.names() ) ).to.have.members( [ 'foo', 'bar' ] );
  28. } );
  29. } );
  30. describe( 'add()', () => {
  31. it( 'throws when trying to override already registered component', () => {
  32. factory.add( 'foo', () => {} );
  33. expect( () => {
  34. factory.add( 'foo', () => {} );
  35. } ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
  36. } );
  37. } );
  38. describe( 'create()', () => {
  39. it( 'throws when trying to create a component which has not been registered', () => {
  40. expect( () => {
  41. factory.create( 'foo' );
  42. } ).to.throw( CKEditorError, /^componentfactory-item-missing/ );
  43. } );
  44. it( 'creates an instance', () => {
  45. class View {
  46. constructor( locale ) {
  47. this.locale = locale;
  48. }
  49. }
  50. const locale = editor.locale = {};
  51. factory.add( 'foo', locale => new View( locale ) );
  52. const instance = factory.create( 'foo' );
  53. expect( instance ).to.be.instanceof( View );
  54. expect( instance.locale ).to.equal( locale );
  55. } );
  56. } );
  57. } );