plugincollection.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const modules = bender.amd.require(
  7. 'core/editor',
  8. 'core/plugincollection',
  9. 'core/plugin',
  10. 'core/creator',
  11. 'core/ckeditorerror',
  12. 'core/log'
  13. );
  14. let PluginCollection, Plugin, Editor, Creator, CKEditorError, log;
  15. let editor;
  16. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF;
  17. class TestError extends Error {}
  18. bender.tools.createSinonSandbox();
  19. before( () => {
  20. PluginCollection = modules[ 'core/plugincollection' ];
  21. Editor = modules[ 'core/editor' ];
  22. Plugin = modules[ 'core/plugin' ];
  23. Creator = modules[ 'core/creator' ];
  24. CKEditorError = modules[ 'core/ckeditorerror' ];
  25. log = modules[ 'core/log' ];
  26. PluginA = createPlugin( 'A' );
  27. PluginB = createPlugin( 'B' );
  28. PluginC = createPlugin( 'C' );
  29. PluginD = createPlugin( 'D' );
  30. PluginE = createPlugin( 'E' );
  31. PluginF = createPlugin( 'F' );
  32. PluginC.requires = [ PluginB ];
  33. PluginD.requires = [ PluginA, PluginC ];
  34. PluginF.requires = [ PluginE ];
  35. PluginE.requires = [ PluginF ];
  36. editor = new Editor( document.body.appendChild( document.createElement( 'div' ) ) );
  37. } );
  38. // Create fake plugins that will be used on tests.
  39. bender.amd.define( 'A', () => {
  40. return PluginA;
  41. } );
  42. bender.amd.define( 'B', () => {
  43. return PluginB;
  44. } );
  45. bender.amd.define( 'C', [ 'core/plugin', 'B' ], () => {
  46. return PluginC;
  47. } );
  48. bender.amd.define( 'D', [ 'core/plugin', 'A', 'C' ], () => {
  49. return PluginD;
  50. } );
  51. bender.amd.define( 'E', [ 'core/plugin', 'F' ], () => {
  52. return PluginE;
  53. } );
  54. bender.amd.define( 'F', [ 'core/plugin', 'E' ], () => {
  55. return PluginF;
  56. } );
  57. // Erroneous cases.
  58. bender.amd.define( 'G', () => {
  59. throw new TestError( 'Some error inside a plugin' );
  60. } );
  61. bender.amd.define( 'I', () => {
  62. return class {};
  63. } );
  64. /////////////
  65. describe( 'load', () => {
  66. it( 'should not fail when trying to load 0 plugins (empty string)', () => {
  67. let plugins = new PluginCollection( editor );
  68. return plugins.load( '' )
  69. .then( () => {
  70. expect( getPlugins( plugins ) ).to.be.empty();
  71. } );
  72. } );
  73. it( 'should not fail when trying to load 0 plugins (undefined)', () => {
  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, 'set' );
  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.set()' );
  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, 'set' );
  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.set()' );
  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, 'set' );
  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.set()' );
  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, 'set' );
  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.set()' );
  137. expect( getPluginNames( loadedPlugins ) )
  138. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  139. } );
  140. } );
  141. it( 'should set the `editor` property on loaded plugins', () => {
  142. let plugins = new PluginCollection( editor );
  143. return plugins.load( 'A,B' )
  144. .then( () => {
  145. expect( plugins.get( 'A' ).editor ).to.equal( editor );
  146. expect( plugins.get( 'B' ).editor ).to.equal( editor );
  147. } );
  148. } );
  149. it( 'should reject on invalid plugin names (forward require.js loading error)', () => {
  150. let logSpy = bender.sinon.stub( log, 'error' );
  151. let plugins = new PluginCollection( editor );
  152. return plugins.load( 'A,BAD,B' )
  153. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  154. .then( () => {
  155. throw new Error( 'Test error: this promise should not be resolved successfully' );
  156. } )
  157. .catch( ( err ) => {
  158. expect( err ).to.be.an.instanceof( Error );
  159. // Make sure it's the Require.JS error, not the one thrown above.
  160. expect( err.message ).to.match( /^Script error for/ );
  161. sinon.assert.calledOnce( logSpy );
  162. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  163. } );
  164. } );
  165. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  166. let logSpy = bender.sinon.stub( log, 'error' );
  167. let plugins = new PluginCollection( editor );
  168. return plugins.load( 'A,G,B' )
  169. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  170. .then( () => {
  171. throw new Error( 'Test error: this promise should not be resolved successfully' );
  172. } )
  173. .catch( ( err ) => {
  174. expect( err ).to.be.an.instanceof( TestError );
  175. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  176. sinon.assert.calledOnce( logSpy );
  177. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  178. } );
  179. } );
  180. it( 'should reject when loading a module which is not a plugin', () => {
  181. let logSpy = bender.sinon.stub( log, 'error' );
  182. let plugins = new PluginCollection( editor );
  183. return plugins.load( 'I' )
  184. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  185. .then( () => {
  186. throw new Error( 'Test error: this promise should not be resolved successfully' );
  187. } )
  188. .catch( ( err ) => {
  189. expect( err ).to.be.an.instanceof( CKEditorError );
  190. expect( err.message ).to.match( /^plugincollection-instance/ );
  191. sinon.assert.calledOnce( logSpy );
  192. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  193. } );
  194. } );
  195. } );
  196. function createPlugin( name ) {
  197. const P = class extends Plugin {
  198. constructor( editor ) {
  199. super( editor );
  200. this._pluginName = name;
  201. }
  202. };
  203. P._pluginName = name;
  204. return P;
  205. }
  206. function getPlugins( pluginCollection ) {
  207. const plugins = [];
  208. for ( let entry of pluginCollection.entries() ) {
  209. if ( typeof entry[ 0 ] == 'function' ) {
  210. plugins.push( entry[ 1 ] );
  211. }
  212. }
  213. return plugins;
  214. }
  215. function getPluginsFromSpy( addSpy ) {
  216. return addSpy.args
  217. .map( ( arg ) => arg[ 0 ] )
  218. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  219. // Return only pluginClass => plugin entries as these will always represent all plugins.
  220. .filter( ( plugin ) => typeof plugin == 'function' );
  221. }
  222. function getPluginNames( plugins ) {
  223. return plugins.map( ( plugin ) => plugin._pluginName );
  224. }