componentfactory.js 3.2 KB

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