componentfactory.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @license Copyright (c) 2003-2018, 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. factory.add( 'Baz', () => {} );
  28. expect( Array.from( factory.names() ) ).to.have.members( [ 'foo', 'bar', 'baz' ] );
  29. } );
  30. } );
  31. describe( 'add()', () => {
  32. it( 'throws when trying to override already registered component', () => {
  33. factory.add( 'foo', () => {} );
  34. expect( () => {
  35. factory.add( 'foo', () => {} );
  36. } ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
  37. } );
  38. it( 'throws when trying to override already registered component added with different case', () => {
  39. factory.add( 'Foo', () => {} );
  40. expect( () => {
  41. factory.add( 'foo', () => {} );
  42. } ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
  43. } );
  44. } );
  45. describe( 'create()', () => {
  46. it( 'throws when trying to create a component which has not been registered', () => {
  47. expect( () => {
  48. factory.create( 'foo' );
  49. } ).to.throw( CKEditorError, /^componentfactory-item-missing/ );
  50. } );
  51. it( 'creates an instance', () => {
  52. class View {
  53. constructor( locale ) {
  54. this.locale = locale;
  55. }
  56. }
  57. const locale = editor.locale = {};
  58. factory.add( 'foo', locale => new View( locale ) );
  59. const instance = factory.create( 'foo' );
  60. expect( instance ).to.be.instanceof( View );
  61. expect( instance.locale ).to.equal( locale );
  62. } );
  63. it( 'creates an instance even with different case', () => {
  64. class View {
  65. constructor( locale ) {
  66. this.locale = locale;
  67. }
  68. }
  69. const locale = editor.locale = {};
  70. factory.add( 'Foo', locale => new View( locale ) );
  71. const instance = factory.create( 'foo' );
  72. expect( instance ).to.be.instanceof( View );
  73. expect( instance.locale ).to.equal( locale );
  74. } );
  75. } );
  76. describe( 'has()', () => {
  77. it( 'checks if the factory contains a component of a given name', () => {
  78. factory.add( 'foo', () => {} );
  79. factory.add( 'bar', () => {} );
  80. expect( factory.has( 'foo' ) ).to.be.true;
  81. expect( factory.has( 'bar' ) ).to.be.true;
  82. expect( factory.has( 'baz' ) ).to.be.false;
  83. expect( factory.has( 'Foo' ) ).to.be.true;
  84. expect( factory.has( 'fOO' ) ).to.be.true;
  85. } );
  86. } );
  87. } );