plugincollection.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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( 'PluginCollection', () => {
  42. describe( 'load', () => {
  43. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  44. let plugins = new PluginCollection( editor );
  45. return plugins.load( [] )
  46. .then( () => {
  47. expect( getPlugins( plugins ) ).to.be.empty;
  48. } );
  49. } );
  50. it( 'should add collection items for loaded plugins', () => {
  51. let plugins = new PluginCollection( editor );
  52. return plugins.load( [ PluginA, PluginB ] )
  53. .then( () => {
  54. expect( getPlugins( plugins ).length ).to.equal( 2 );
  55. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  56. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  57. } );
  58. } );
  59. it( 'should load dependency plugins', () => {
  60. let plugins = new PluginCollection( editor );
  61. let spy = sinon.spy( plugins, '_add' );
  62. return plugins.load( [ PluginA, PluginC ] )
  63. .then( ( loadedPlugins ) => {
  64. expect( getPlugins( plugins ).length ).to.equal( 3 );
  65. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  66. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  67. expect( getPluginNames( loadedPlugins ) )
  68. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  69. } );
  70. } );
  71. it( 'should be ok when dependencies are loaded first', () => {
  72. let plugins = new PluginCollection( editor );
  73. let spy = sinon.spy( plugins, '_add' );
  74. return plugins.load( [ PluginA, PluginB, PluginC ] )
  75. .then( ( loadedPlugins ) => {
  76. expect( getPlugins( plugins ).length ).to.equal( 3 );
  77. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  78. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  79. expect( getPluginNames( loadedPlugins ) )
  80. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  81. } );
  82. } );
  83. it( 'should load deep dependency plugins', () => {
  84. let plugins = new PluginCollection( editor );
  85. let spy = sinon.spy( plugins, '_add' );
  86. return plugins.load( [ PluginD ] )
  87. .then( ( loadedPlugins ) => {
  88. expect( getPlugins( plugins ).length ).to.equal( 4 );
  89. // The order must have dependencies first.
  90. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  91. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  92. expect( getPluginNames( loadedPlugins ) )
  93. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  94. } );
  95. } );
  96. it( 'should handle cross dependency plugins', () => {
  97. let plugins = new PluginCollection( editor );
  98. let spy = sinon.spy( plugins, '_add' );
  99. return plugins.load( [ PluginA, PluginE ] )
  100. .then( ( loadedPlugins ) => {
  101. expect( getPlugins( plugins ).length ).to.equal( 3 );
  102. // The order must have dependencies first.
  103. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  104. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  105. expect( getPluginNames( loadedPlugins ) )
  106. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  107. } );
  108. } );
  109. it( 'should load grand child classes', () => {
  110. let plugins = new PluginCollection( editor );
  111. return plugins.load( [ PluginG ] )
  112. .then( () => {
  113. expect( getPlugins( plugins ).length ).to.equal( 1 );
  114. } );
  115. } );
  116. it( 'should set the `editor` property on loaded plugins', () => {
  117. let plugins = new PluginCollection( editor );
  118. return plugins.load( [ PluginA, PluginB ] )
  119. .then( () => {
  120. expect( plugins.get( PluginA ).editor ).to.equal( editor );
  121. expect( plugins.get( PluginB ).editor ).to.equal( editor );
  122. } );
  123. } );
  124. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  125. let logSpy = testUtils.sinon.stub( log, 'error' );
  126. let plugins = new PluginCollection( editor );
  127. return plugins.load( [ PluginA, PluginX, PluginB ] )
  128. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  129. .then( () => {
  130. throw new Error( 'Test error: this promise should not be resolved successfully' );
  131. } )
  132. .catch( ( err ) => {
  133. expect( err ).to.be.an.instanceof( TestError );
  134. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  135. sinon.assert.calledOnce( logSpy );
  136. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  137. } );
  138. } );
  139. it( 'should reject when loading a module which is not a plugin', () => {
  140. let logSpy = testUtils.sinon.stub( log, 'error' );
  141. let plugins = new PluginCollection( editor );
  142. class Y {}
  143. return plugins.load( [ Y ] )
  144. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  145. .then( () => {
  146. throw new Error( 'Test error: this promise should not be resolved successfully' );
  147. } )
  148. .catch( ( err ) => {
  149. expect( err ).to.be.an.instanceof( CKEditorError );
  150. expect( err.message ).to.match( /^plugincollection-instance/ );
  151. sinon.assert.calledOnce( logSpy );
  152. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  153. } );
  154. } );
  155. } );
  156. } );
  157. function createPlugin( name, baseClass ) {
  158. baseClass = baseClass || Plugin;
  159. const P = class extends baseClass {
  160. constructor( editor ) {
  161. super( editor );
  162. this._pluginName = name;
  163. }
  164. };
  165. P._pluginName = name;
  166. return P;
  167. }
  168. function getPlugins( pluginCollection ) {
  169. return Array.from( pluginCollection )
  170. .map( entry => entry[ 1 ] ); // Get instances.
  171. }
  172. function getPluginsFromSpy( addSpy ) {
  173. return addSpy.args
  174. .map( ( arg ) => arg[ 0 ] )
  175. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  176. // Return only pluginClass => plugin entries as these will always represent all plugins.
  177. .filter( ( plugin ) => typeof plugin == 'function' );
  178. }
  179. function getPluginNames( plugins ) {
  180. return plugins.map( ( plugin ) => plugin._pluginName );
  181. }