plugincollection.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 'ckeditor5-core/tests/_utils/utils';
  7. import Editor from 'ckeditor5-core/src/editor/editor';
  8. import PluginCollection from 'ckeditor5-core/src/plugincollection';
  9. import Plugin from 'ckeditor5-core/src/plugin';
  10. import CKEditorError from 'ckeditor5-utils/src/ckeditorerror';
  11. import log from 'ckeditor5-utils/src/log';
  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. describe( 'get()', () => {
  157. it( 'retrieves plugin by its constructor', () => {
  158. let plugins = new PluginCollection( editor );
  159. class SomePlugin extends Plugin {}
  160. return plugins.load( [ SomePlugin ] )
  161. .then( () => {
  162. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  163. } );
  164. } );
  165. it( 'retrieves plugin by its name and constructor', () => {
  166. let plugins = new PluginCollection( editor );
  167. class SomePlugin extends Plugin {}
  168. SomePlugin.pluginName = 'foo/bar';
  169. return plugins.load( [ SomePlugin ] )
  170. .then( () => {
  171. expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
  172. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  173. } );
  174. } );
  175. } );
  176. describe( 'iterator', () => {
  177. it( 'exists', () => {
  178. let plugins = new PluginCollection( editor );
  179. expect( plugins ).to.have.property( Symbol.iterator );
  180. } );
  181. it( 'returns only plugins by constructors', () => {
  182. let plugins = new PluginCollection( editor );
  183. class SomePlugin1 extends Plugin {}
  184. class SomePlugin2 extends Plugin {}
  185. SomePlugin2.pluginName = 'foo/bar';
  186. return plugins.load( [ SomePlugin1, SomePlugin2 ] )
  187. .then( () => {
  188. const pluginConstructors = Array.from( plugins )
  189. .map( entry => entry[ 0 ] );
  190. expect( pluginConstructors ).to.have.members( [ SomePlugin1, SomePlugin2 ] );
  191. } );
  192. } );
  193. } );
  194. } );
  195. function createPlugin( name ) {
  196. const P = class extends Plugin {
  197. constructor( editor ) {
  198. super( editor );
  199. this.pluginName = name;
  200. }
  201. };
  202. P.pluginName = name;
  203. return P;
  204. }
  205. function getPlugins( pluginCollection ) {
  206. return Array.from( pluginCollection )
  207. .map( entry => entry[ 1 ] ); // Get instances.
  208. }
  209. function getPluginsFromSpy( addSpy ) {
  210. return addSpy.args
  211. .map( ( arg ) => arg[ 0 ] )
  212. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  213. // Return only pluginClass => plugin entries as these will always represent all plugins.
  214. .filter( ( plugin ) => typeof plugin == 'function' );
  215. }
  216. function getPluginNames( plugins ) {
  217. return plugins.map( ( plugin ) => plugin.pluginName );
  218. }