8
0

componentfactory.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. describe( 'has()', () => {
  58. it( 'checks if the factory contains a component of a given name', () => {
  59. factory.add( 'foo', () => {} );
  60. factory.add( 'bar', () => {} );
  61. expect( factory.has( 'foo' ) ).to.be.true;
  62. expect( factory.has( 'bar' ) ).to.be.true;
  63. expect( factory.has( 'baz' ) ).to.be.false;
  64. } );
  65. } );
  66. } );