plugincollection.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: browser-only */
  6. import testUtils from 'tests/core/_utils/utils.js';
  7. import Editor from 'ckeditor5/core/editor/editor.js';
  8. import PluginCollection from 'ckeditor5/core/plugincollection.js';
  9. import Plugin from 'ckeditor5/core/plugin.js';
  10. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  11. import log from 'ckeditor5/utils/log.js';
  12. let editor;
  13. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI, PluginX;
  14. class TestError extends Error {}
  15. class ChildPlugin extends Plugin {}
  16. class GrandPlugin extends ChildPlugin {}
  17. testUtils.createSinonSandbox();
  18. before( () => {
  19. PluginA = createPlugin( 'A' );
  20. PluginB = createPlugin( 'B' );
  21. PluginC = createPlugin( 'C' );
  22. PluginD = createPlugin( 'D' );
  23. PluginE = createPlugin( 'E' );
  24. PluginF = createPlugin( 'F' );
  25. PluginG = createPlugin( 'G', GrandPlugin );
  26. PluginH = createPlugin( 'H' );
  27. PluginI = createPlugin( 'I' );
  28. PluginX = class extends Plugin {
  29. constructor( editor ) {
  30. super( editor );
  31. throw new TestError( 'Some error inside a plugin' );
  32. }
  33. };
  34. PluginC.requires = [ PluginB ];
  35. PluginD.requires = [ PluginA, PluginC ];
  36. PluginF.requires = [ PluginE ];
  37. PluginE.requires = [ PluginF ];
  38. PluginH.requires = [ PluginI ];
  39. editor = new Editor();
  40. } );
  41. describe( 'load', () => {
  42. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  43. let plugins = new PluginCollection( editor );
  44. return plugins.load( [] )
  45. .then( () => {
  46. expect( getPlugins( plugins ) ).to.be.empty;
  47. } );
  48. } );
  49. it( 'should add collection items for loaded plugins', () => {
  50. let plugins = new PluginCollection( editor );
  51. return plugins.load( [ PluginA, PluginB ] )
  52. .then( () => {
  53. expect( getPlugins( plugins ).length ).to.equal( 2 );
  54. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  55. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  56. } );
  57. } );
  58. it( 'should load dependency plugins', () => {
  59. let plugins = new PluginCollection( editor );
  60. let spy = sinon.spy( plugins, '_add' );
  61. return plugins.load( [ PluginA, PluginC ] )
  62. .then( ( loadedPlugins ) => {
  63. expect( getPlugins( plugins ).length ).to.equal( 3 );
  64. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  65. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  66. expect( getPluginNames( loadedPlugins ) )
  67. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  68. } );
  69. } );
  70. it( 'should be ok when dependencies are loaded first', () => {
  71. let plugins = new PluginCollection( editor );
  72. let spy = sinon.spy( plugins, '_add' );
  73. return plugins.load( [ PluginA, PluginB, PluginC ] )
  74. .then( ( loadedPlugins ) => {
  75. expect( getPlugins( plugins ).length ).to.equal( 3 );
  76. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  77. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  78. expect( getPluginNames( loadedPlugins ) )
  79. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  80. } );
  81. } );
  82. it( 'should load deep dependency plugins', () => {
  83. let plugins = new PluginCollection( editor );
  84. let spy = sinon.spy( plugins, '_add' );
  85. return plugins.load( [ PluginD ] )
  86. .then( ( loadedPlugins ) => {
  87. expect( getPlugins( plugins ).length ).to.equal( 4 );
  88. // The order must have dependencies first.
  89. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  90. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  91. expect( getPluginNames( loadedPlugins ) )
  92. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  93. } );
  94. } );
  95. it( 'should handle cross dependency plugins', () => {
  96. let plugins = new PluginCollection( editor );
  97. let spy = sinon.spy( plugins, '_add' );
  98. return plugins.load( [ PluginA, PluginE ] )
  99. .then( ( loadedPlugins ) => {
  100. expect( getPlugins( plugins ).length ).to.equal( 3 );
  101. // The order must have dependencies first.
  102. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  103. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  104. expect( getPluginNames( loadedPlugins ) )
  105. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  106. } );
  107. } );
  108. it( 'should load grand child classes', () => {
  109. let plugins = new PluginCollection( editor );
  110. return plugins.load( [ PluginG ] )
  111. .then( () => {
  112. expect( getPlugins( plugins ).length ).to.equal( 1 );
  113. } );
  114. } );
  115. it( 'should set the `editor` property on loaded plugins', () => {
  116. let plugins = new PluginCollection( editor );
  117. return plugins.load( [ PluginA, PluginB ] )
  118. .then( () => {
  119. expect( plugins.get( PluginA ).editor ).to.equal( editor );
  120. expect( plugins.get( PluginB ).editor ).to.equal( editor );
  121. } );
  122. } );
  123. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  124. let logSpy = testUtils.sinon.stub( log, 'error' );
  125. let plugins = new PluginCollection( editor );
  126. return plugins.load( [ PluginA, PluginX, PluginB ] )
  127. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  128. .then( () => {
  129. throw new Error( 'Test error: this promise should not be resolved successfully' );
  130. } )
  131. .catch( ( err ) => {
  132. expect( err ).to.be.an.instanceof( TestError );
  133. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  134. sinon.assert.calledOnce( logSpy );
  135. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  136. } );
  137. } );
  138. it( 'should reject when loading a module which is not a plugin', () => {
  139. let logSpy = testUtils.sinon.stub( log, 'error' );
  140. let plugins = new PluginCollection( editor );
  141. class Y {}
  142. return plugins.load( [ Y ] )
  143. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  144. .then( () => {
  145. throw new Error( 'Test error: this promise should not be resolved successfully' );
  146. } )
  147. .catch( ( err ) => {
  148. expect( err ).to.be.an.instanceof( CKEditorError );
  149. expect( err.message ).to.match( /^plugincollection-instance/ );
  150. sinon.assert.calledOnce( logSpy );
  151. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  152. } );
  153. } );
  154. } );
  155. function createPlugin( name, baseClass ) {
  156. baseClass = baseClass || Plugin;
  157. const P = class extends baseClass {
  158. constructor( editor ) {
  159. super( editor );
  160. this._pluginName = name;
  161. }
  162. };
  163. P._pluginName = name;
  164. return P;
  165. }
  166. function getPlugins( pluginCollection ) {
  167. return Array.from( pluginCollection )
  168. .map( entry => entry[ 1 ] ); // Get instances.
  169. }
  170. function getPluginsFromSpy( addSpy ) {
  171. return addSpy.args
  172. .map( ( arg ) => arg[ 0 ] )
  173. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  174. // Return only pluginClass => plugin entries as these will always represent all plugins.
  175. .filter( ( plugin ) => typeof plugin == 'function' );
  176. }
  177. function getPluginNames( plugins ) {
  178. return plugins.map( ( plugin ) => plugin._pluginName );
  179. }