8
0

plugincollection.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 moduleUtils from '/tests/ckeditor5/_utils/module.js';
  7. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  8. import Editor from '/ckeditor5/editor/editor.js';
  9. import PluginCollection from '/ckeditor5/plugincollection.js';
  10. import Plugin from '/ckeditor5/plugin.js';
  11. import Feature from '/ckeditor5/feature.js';
  12. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  13. import log from '/ckeditor5/utils/log.js';
  14. let editor;
  15. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI;
  16. class TestError extends Error {}
  17. class GrandPlugin extends Feature {}
  18. testUtils.createSinonSandbox();
  19. before( () => {
  20. PluginA = createPlugin( 'A' );
  21. PluginB = createPlugin( 'B' );
  22. PluginC = createPlugin( 'C' );
  23. PluginD = createPlugin( 'D' );
  24. PluginE = createPlugin( 'E' );
  25. PluginF = createPlugin( 'F' );
  26. PluginG = createPlugin( 'G', GrandPlugin );
  27. PluginH = createPlugin( 'H' );
  28. PluginI = createPlugin( 'I' );
  29. PluginC.requires = [ PluginB ];
  30. PluginD.requires = [ PluginA, PluginC ];
  31. PluginF.requires = [ PluginE ];
  32. PluginE.requires = [ PluginF ];
  33. PluginH.requires = [ PluginI ];
  34. editor = new Editor();
  35. } );
  36. // Create fake plugins that will be used on tests.
  37. moduleUtils.define( 'A/A', () => {
  38. return PluginA;
  39. } );
  40. moduleUtils.define( 'B/B', () => {
  41. return PluginB;
  42. } );
  43. moduleUtils.define( 'C/C', [ 'editor/editor', 'B/B' ], () => {
  44. return PluginC;
  45. } );
  46. moduleUtils.define( 'D/D', [ 'editor/editor', 'A/A', 'C/C' ], () => {
  47. return PluginD;
  48. } );
  49. moduleUtils.define( 'E/E', [ 'editor/editor', 'F/F' ], () => {
  50. return PluginE;
  51. } );
  52. moduleUtils.define( 'F/F', [ 'editor/editor', 'E/E' ], () => {
  53. return PluginF;
  54. } );
  55. moduleUtils.define( 'G/G', () => {
  56. return PluginG;
  57. } );
  58. moduleUtils.define( 'H/H', () => {
  59. return PluginH;
  60. } );
  61. moduleUtils.define( 'I/I', () => {
  62. return PluginI;
  63. } );
  64. // Erroneous cases.
  65. moduleUtils.define( 'X/X', () => {
  66. throw new TestError( 'Some error inside a plugin' );
  67. } );
  68. moduleUtils.define( 'Y/Y', () => {
  69. return class {};
  70. } );
  71. /////////////
  72. describe( 'load', () => {
  73. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  74. let plugins = new PluginCollection( editor );
  75. return plugins.load( [] )
  76. .then( () => {
  77. expect( getPlugins( plugins ) ).to.be.empty();
  78. } );
  79. } );
  80. it( 'should add collection items for loaded plugins', () => {
  81. let plugins = new PluginCollection( editor );
  82. return plugins.load( [ 'A', 'B' ] )
  83. .then( () => {
  84. expect( getPlugins( plugins ).length ).to.equal( 2 );
  85. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  86. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  87. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  88. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  89. } );
  90. } );
  91. it( 'should load dependency plugins', () => {
  92. let plugins = new PluginCollection( editor );
  93. let spy = sinon.spy( plugins, '_add' );
  94. return plugins.load( [ 'A', 'C' ] )
  95. .then( ( loadedPlugins ) => {
  96. expect( getPlugins( plugins ).length ).to.equal( 3 );
  97. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  98. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  99. expect( getPluginNames( loadedPlugins ) )
  100. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  101. } );
  102. } );
  103. it( 'should be ok when dependencies are loaded first', () => {
  104. let plugins = new PluginCollection( editor );
  105. let spy = sinon.spy( plugins, '_add' );
  106. return plugins.load( [ 'A', 'B', 'C' ] )
  107. .then( ( loadedPlugins ) => {
  108. expect( getPlugins( plugins ).length ).to.equal( 3 );
  109. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  110. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  111. expect( getPluginNames( loadedPlugins ) )
  112. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  113. } );
  114. } );
  115. it( 'should load deep dependency plugins', () => {
  116. let plugins = new PluginCollection( editor );
  117. let spy = sinon.spy( plugins, '_add' );
  118. return plugins.load( [ 'D' ] )
  119. .then( ( loadedPlugins ) => {
  120. expect( getPlugins( plugins ).length ).to.equal( 4 );
  121. // The order must have dependencies first.
  122. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  123. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  124. expect( getPluginNames( loadedPlugins ) )
  125. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  126. } );
  127. } );
  128. it( 'should handle cross dependency plugins', () => {
  129. let plugins = new PluginCollection( editor );
  130. let spy = sinon.spy( plugins, '_add' );
  131. return plugins.load( [ 'A', 'E' ] )
  132. .then( ( loadedPlugins ) => {
  133. expect( getPlugins( plugins ).length ).to.equal( 3 );
  134. // The order must have dependencies first.
  135. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  136. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  137. expect( getPluginNames( loadedPlugins ) )
  138. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  139. } );
  140. } );
  141. it( 'should load grand child classes', () => {
  142. let plugins = new PluginCollection( editor );
  143. return plugins.load( [ 'G' ] )
  144. .then( () => {
  145. expect( getPlugins( plugins ).length ).to.equal( 1 );
  146. } );
  147. } );
  148. it( 'should make plugin available to get by name when plugin was loaded as dependency first', () => {
  149. let plugins = new PluginCollection( editor );
  150. return plugins.load( [ 'H', 'I' ] )
  151. .then( () => {
  152. expect( plugins.get( 'I' ) ).to.be.instanceof( PluginI );
  153. } );
  154. } );
  155. it( 'should set the `editor` property on loaded plugins', () => {
  156. let plugins = new PluginCollection( editor );
  157. return plugins.load( [ 'A', 'B' ] )
  158. .then( () => {
  159. expect( plugins.get( 'A' ).editor ).to.equal( editor );
  160. expect( plugins.get( 'B' ).editor ).to.equal( editor );
  161. } );
  162. } );
  163. it( 'should reject on invalid plugin names (forward require.js loading error)', () => {
  164. let logSpy = testUtils.sinon.stub( log, 'error' );
  165. let plugins = new PluginCollection( editor );
  166. return plugins.load( [ 'A', 'BAD', 'B' ] )
  167. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  168. .then( () => {
  169. throw new Error( 'Test error: this promise should not be resolved successfully' );
  170. } )
  171. .catch( ( err ) => {
  172. expect( err ).to.be.an.instanceof( Error );
  173. // Make sure it's the Require.JS error, not the one thrown above.
  174. expect( err.message ).to.match( /^Script error for/ );
  175. sinon.assert.calledOnce( logSpy );
  176. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  177. } );
  178. } );
  179. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  180. let logSpy = testUtils.sinon.stub( log, 'error' );
  181. let plugins = new PluginCollection( editor );
  182. return plugins.load( [ 'A', 'X', 'B' ] )
  183. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  184. .then( () => {
  185. throw new Error( 'Test error: this promise should not be resolved successfully' );
  186. } )
  187. .catch( ( err ) => {
  188. expect( err ).to.be.an.instanceof( TestError );
  189. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  190. sinon.assert.calledOnce( logSpy );
  191. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  192. } );
  193. } );
  194. it( 'should reject when loading a module which is not a plugin', () => {
  195. let logSpy = testUtils.sinon.stub( log, 'error' );
  196. let plugins = new PluginCollection( editor );
  197. return plugins.load( [ 'Y' ] )
  198. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  199. .then( () => {
  200. throw new Error( 'Test error: this promise should not be resolved successfully' );
  201. } )
  202. .catch( ( err ) => {
  203. expect( err ).to.be.an.instanceof( CKEditorError );
  204. expect( err.message ).to.match( /^plugincollection-instance/ );
  205. sinon.assert.calledOnce( logSpy );
  206. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  207. } );
  208. } );
  209. } );
  210. describe( 'getPluginPath()', () => {
  211. it( 'generates path for modules within some package', () => {
  212. const p = PluginCollection.getPluginPath( 'some/ba' );
  213. expect( p ).to.equal( 'ckeditor5/some/ba.js' );
  214. } );
  215. it( 'generates path from simplified feature name', () => {
  216. const p = PluginCollection.getPluginPath( 'foo' );
  217. expect( p ).to.equal( 'ckeditor5/foo/foo.js' );
  218. } );
  219. } );
  220. function createPlugin( name, baseClass ) {
  221. baseClass = baseClass || Plugin;
  222. const P = class extends baseClass {
  223. constructor( editor ) {
  224. super( editor );
  225. this._pluginName = name;
  226. }
  227. };
  228. P._pluginName = name;
  229. return P;
  230. }
  231. function getPlugins( pluginCollection ) {
  232. const plugins = [];
  233. for ( let entry of pluginCollection ) {
  234. // Keep only plugins kept under their classes.
  235. if ( typeof entry[ 0 ] == 'function' ) {
  236. plugins.push( entry[ 1 ] );
  237. }
  238. }
  239. return plugins;
  240. }
  241. function getPluginsFromSpy( addSpy ) {
  242. return addSpy.args
  243. .map( ( arg ) => arg[ 0 ] )
  244. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  245. // Return only pluginClass => plugin entries as these will always represent all plugins.
  246. .filter( ( plugin ) => typeof plugin == 'function' );
  247. }
  248. function getPluginNames( plugins ) {
  249. return plugins.map( ( plugin ) => plugin._pluginName );
  250. }