plugincollection.js 8.8 KB

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