8
0

plugincollection.js 9.3 KB

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