componentfactory.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  6. import ComponentFactory from '../src/componentfactory';
  7. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  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( 'does not normalize component names', () => {
  33. factory.add( 'FoO', () => {} );
  34. expect( Array.from( factory.names() ) ).to.have.members( [ 'FoO' ] );
  35. } );
  36. it( 'should allow overriding already registered components', () => {
  37. factory.add( 'foo', () => 'old' );
  38. factory.add( 'foo', () => 'new' );
  39. expect( factory.create( 'foo' ) ).to.equal( 'new' );
  40. } );
  41. it( 'should allow overriding already registered components (same name, different case)', () => {
  42. factory.add( 'foo', () => 'old' );
  43. factory.add( 'Foo', () => 'new' );
  44. expect( factory.create( 'foo' ) ).to.equal( 'new' );
  45. } );
  46. } );
  47. describe( 'create()', () => {
  48. it( 'throws when trying to create a component which has not been registered', () => {
  49. expectToThrowCKEditorError( () => {
  50. factory.create( 'foo' );
  51. }, /^componentfactory-item-missing/, editor );
  52. } );
  53. it( 'creates an instance', () => {
  54. class View {
  55. constructor( locale ) {
  56. this.locale = locale;
  57. }
  58. }
  59. const locale = editor.locale = {};
  60. factory.add( 'foo', locale => new View( locale ) );
  61. const instance = factory.create( 'foo' );
  62. expect( instance ).to.be.instanceof( View );
  63. expect( instance.locale ).to.equal( locale );
  64. } );
  65. it( 'creates an instance even with different case', () => {
  66. class View {
  67. constructor( locale ) {
  68. this.locale = locale;
  69. }
  70. }
  71. const locale = editor.locale = {};
  72. factory.add( 'Foo', locale => new View( locale ) );
  73. const instance = factory.create( 'foo' );
  74. expect( instance ).to.be.instanceof( View );
  75. expect( instance.locale ).to.equal( locale );
  76. } );
  77. } );
  78. describe( 'has()', () => {
  79. it( 'checks if the factory contains a component of a given name', () => {
  80. factory.add( 'foo', () => {} );
  81. factory.add( 'bar', () => {} );
  82. expect( factory.has( 'foo' ) ).to.be.true;
  83. expect( factory.has( 'bar' ) ).to.be.true;
  84. expect( factory.has( 'baz' ) ).to.be.false;
  85. expect( factory.has( 'Foo' ) ).to.be.true;
  86. expect( factory.has( 'fOO' ) ).to.be.true;
  87. } );
  88. } );
  89. } );