plugincollection.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import testUtils from '../tests/_utils/utils';
  6. import Editor from '../src/editor/editor';
  7. import PluginCollection from '../src/plugincollection';
  8. import Plugin from '../src/plugin';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import log from '@ckeditor/ckeditor5-utils/src/log';
  11. let editor, availablePlugins;
  12. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI, PluginJ, PluginK, PluginX;
  13. class TestError extends Error {}
  14. class ChildPlugin extends Plugin {}
  15. class GrandPlugin extends ChildPlugin {}
  16. testUtils.createSinonSandbox();
  17. before( () => {
  18. PluginA = createPlugin( 'A' );
  19. PluginB = createPlugin( 'B' );
  20. PluginC = createPlugin( 'C' );
  21. PluginD = createPlugin( 'D' );
  22. PluginE = createPlugin( 'E' );
  23. PluginF = createPlugin( 'F' );
  24. PluginG = createPlugin( 'G', GrandPlugin );
  25. PluginH = createPlugin( 'H' );
  26. PluginI = createPlugin( 'I' );
  27. PluginJ = createPlugin( 'J' );
  28. PluginK = createPlugin( 'K' );
  29. PluginX = class extends Plugin {
  30. constructor( editor ) {
  31. super( editor );
  32. throw new TestError( 'Some error inside a plugin' );
  33. }
  34. };
  35. PluginC.requires = [ PluginB ];
  36. PluginD.requires = [ PluginA, PluginC ];
  37. PluginF.requires = [ PluginE ];
  38. PluginE.requires = [ PluginF ];
  39. PluginH.requires = [ PluginI ];
  40. PluginJ.requires = [ 'K' ];
  41. PluginK.requires = [ PluginA ];
  42. editor = new Editor();
  43. } );
  44. describe( 'PluginCollection', () => {
  45. beforeEach( () => {
  46. availablePlugins = [
  47. PluginA,
  48. PluginB,
  49. PluginC,
  50. PluginD,
  51. PluginE,
  52. PluginF,
  53. PluginG,
  54. PluginH,
  55. PluginI,
  56. PluginJ,
  57. PluginK,
  58. PluginX
  59. ];
  60. } );
  61. describe( 'load()', () => {
  62. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  63. let plugins = new PluginCollection( editor, availablePlugins );
  64. return plugins.load( [] )
  65. .then( () => {
  66. expect( getPlugins( plugins ) ).to.be.empty;
  67. } );
  68. } );
  69. it( 'should add collection items for loaded plugins', () => {
  70. let plugins = new PluginCollection( editor, availablePlugins );
  71. return plugins.load( [ PluginA, PluginB ] )
  72. .then( () => {
  73. expect( getPlugins( plugins ).length ).to.equal( 2 );
  74. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  75. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  76. } );
  77. } );
  78. it( 'should add collection items for loaded plugins using plugin names', () => {
  79. let plugins = new PluginCollection( editor, availablePlugins );
  80. return plugins.load( [ 'A', 'B' ] )
  81. .then( () => {
  82. expect( getPlugins( plugins ).length ).to.equal( 2 );
  83. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  84. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  85. } );
  86. } );
  87. it( 'should load dependency plugins', () => {
  88. let plugins = new PluginCollection( editor, availablePlugins );
  89. let spy = sinon.spy( plugins, '_add' );
  90. return plugins.load( [ PluginA, PluginC ] )
  91. .then( ( loadedPlugins ) => {
  92. expect( getPlugins( plugins ).length ).to.equal( 3 );
  93. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  94. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  95. expect( getPluginNames( loadedPlugins ) )
  96. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  97. } );
  98. } );
  99. it( 'should load dependency plugins defined by plugin names', () => {
  100. let plugins = new PluginCollection( editor, availablePlugins );
  101. let spy = sinon.spy( plugins, '_add' );
  102. return plugins.load( [ 'J' ] )
  103. .then( ( loadedPlugins ) => {
  104. expect( getPlugins( plugins ).length ).to.equal( 3 );
  105. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  106. .to.deep.equal( [ 'A', 'K', 'J' ], 'order by plugins._add()' );
  107. expect( getPluginNames( loadedPlugins ) )
  108. .to.deep.equal( [ 'A', 'K', 'J' ], 'order by returned value' );
  109. } );
  110. } );
  111. it( 'should be ok when dependencies are loaded first', () => {
  112. let plugins = new PluginCollection( editor, availablePlugins );
  113. let spy = sinon.spy( plugins, '_add' );
  114. return plugins.load( [ PluginA, PluginB, PluginC ] )
  115. .then( ( loadedPlugins ) => {
  116. expect( getPlugins( plugins ).length ).to.equal( 3 );
  117. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  118. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  119. expect( getPluginNames( loadedPlugins ) )
  120. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  121. } );
  122. } );
  123. it( 'should load deep dependency plugins', () => {
  124. let plugins = new PluginCollection( editor, availablePlugins );
  125. let spy = sinon.spy( plugins, '_add' );
  126. return plugins.load( [ PluginD ] )
  127. .then( ( loadedPlugins ) => {
  128. expect( getPlugins( plugins ).length ).to.equal( 4 );
  129. // The order must have dependencies first.
  130. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  131. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  132. expect( getPluginNames( loadedPlugins ) )
  133. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  134. } );
  135. } );
  136. it( 'should handle cross dependency plugins', () => {
  137. let plugins = new PluginCollection( editor, availablePlugins );
  138. let spy = sinon.spy( plugins, '_add' );
  139. return plugins.load( [ PluginA, PluginE ] )
  140. .then( ( loadedPlugins ) => {
  141. expect( getPlugins( plugins ).length ).to.equal( 3 );
  142. // The order must have dependencies first.
  143. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  144. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  145. expect( getPluginNames( loadedPlugins ) )
  146. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  147. } );
  148. } );
  149. it( 'should load grand child classes', () => {
  150. let plugins = new PluginCollection( editor, availablePlugins );
  151. return plugins.load( [ PluginG ] )
  152. .then( () => {
  153. expect( getPlugins( plugins ).length ).to.equal( 1 );
  154. } );
  155. } );
  156. it( 'should set the `editor` property on loaded plugins', () => {
  157. let plugins = new PluginCollection( editor, availablePlugins );
  158. return plugins.load( [ PluginA, PluginB ] )
  159. .then( () => {
  160. expect( plugins.get( PluginA ).editor ).to.equal( editor );
  161. expect( plugins.get( PluginB ).editor ).to.equal( editor );
  162. } );
  163. } );
  164. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  165. let logSpy = testUtils.sinon.stub( log, 'error' );
  166. let plugins = new PluginCollection( editor, availablePlugins );
  167. return plugins.load( [ PluginA, PluginX, PluginB ] )
  168. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  169. .then( () => {
  170. throw new Error( 'Test error: this promise should not be resolved successfully' );
  171. } )
  172. .catch( ( err ) => {
  173. expect( err ).to.be.an.instanceof( TestError );
  174. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  175. sinon.assert.calledOnce( logSpy );
  176. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  177. } );
  178. } );
  179. it( 'should reject when loading a module which is not a plugin', () => {
  180. class Y {}
  181. availablePlugins.push( Y );
  182. let logSpy = testUtils.sinon.stub( log, 'error' );
  183. let plugins = new PluginCollection( editor, availablePlugins );
  184. return plugins.load( [ Y ] )
  185. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  186. .then( () => {
  187. throw new Error( 'Test error: this promise should not be resolved successfully' );
  188. } )
  189. .catch( ( err ) => {
  190. expect( err ).to.be.an.instanceof( CKEditorError );
  191. expect( err.message ).to.match( /^plugincollection-instance/ );
  192. sinon.assert.calledOnce( logSpy );
  193. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  194. } );
  195. } );
  196. it( 'should reject when loading non-existing plugin', () => {
  197. let logSpy = testUtils.sinon.stub( log, 'error' );
  198. let plugins = new PluginCollection( editor, availablePlugins );
  199. return plugins.load( [ 'NonExistentPlugin' ] )
  200. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  201. .then( () => {
  202. throw new Error( 'Test error: this promise should not be resolved successfully' );
  203. } )
  204. .catch( ( err ) => {
  205. expect( err ).to.be.an.instanceof( CKEditorError );
  206. expect( err.message ).to.match( /^plugincollection-plugin-not-found/ );
  207. sinon.assert.calledOnce( logSpy );
  208. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  209. } );
  210. } );
  211. it( 'should load chosen plugins (plugins and removePlugins are constructors)', () => {
  212. let plugins = new PluginCollection( editor, availablePlugins );
  213. return plugins.load( [ PluginA, PluginB, PluginC ], [ PluginA ] )
  214. .then( () => {
  215. expect( getPlugins( plugins ).length ).to.equal( 2 );
  216. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  217. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  218. } );
  219. } );
  220. it( 'should load chosen plugins (plugins are constructors, removePlugins are names)', () => {
  221. let plugins = new PluginCollection( editor, availablePlugins );
  222. return plugins.load( [ PluginA, PluginB, PluginC ], [ 'A' ] )
  223. .then( () => {
  224. expect( getPlugins( plugins ).length ).to.equal( 2 );
  225. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  226. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  227. } );
  228. } );
  229. it( 'should load chosen plugins (plugins and removePlugins are names)', () => {
  230. let plugins = new PluginCollection( editor, availablePlugins );
  231. return plugins.load( [ 'A', 'B', 'C' ], [ 'A' ] )
  232. .then( () => {
  233. expect( getPlugins( plugins ).length ).to.equal( 2 );
  234. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  235. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  236. } );
  237. } );
  238. it( 'should load chosen plugins (plugins are names, removePlugins are constructors)', () => {
  239. let plugins = new PluginCollection( editor, availablePlugins );
  240. return plugins.load( [ 'A', 'B', 'C' ], [ PluginA ] )
  241. .then( () => {
  242. expect( getPlugins( plugins ).length ).to.equal( 2 );
  243. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  244. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  245. } );
  246. } );
  247. it( 'should load chosen plugins (plugins are names, removePlugins contains an anonymous plugin)', () => {
  248. class AnonymousPlugin extends Plugin {}
  249. let plugins = new PluginCollection( editor, [ AnonymousPlugin ].concat( availablePlugins ) );
  250. return plugins.load( [ AnonymousPlugin, 'A', 'B' ], [ AnonymousPlugin ] )
  251. .then( () => {
  252. expect( getPlugins( plugins ).length ).to.equal( 2 );
  253. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  254. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  255. } );
  256. } );
  257. it( 'should reject when loaded plugin requires not allowed plugins', () => {
  258. let logSpy = testUtils.sinon.stub( log, 'error' );
  259. let plugins = new PluginCollection( editor, availablePlugins );
  260. return plugins.load( [ PluginA, PluginB, PluginC, PluginD ], [ PluginA, PluginB ] )
  261. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  262. .then( () => {
  263. throw new Error( 'Test error: this promise should not be resolved successfully' );
  264. } )
  265. .catch( ( err ) => {
  266. expect( err ).to.be.an.instanceof( CKEditorError );
  267. expect( err.message ).to.match( /^plugincollection-required/ );
  268. expect( logSpy.calledTwice ).to.equal( true );
  269. } );
  270. } );
  271. it( 'should reject when loaded plugin requires non-existing plugin', () => {
  272. let logSpy = testUtils.sinon.stub( log, 'error' );
  273. let plugins = new PluginCollection( editor, availablePlugins );
  274. PluginA.requires = [ 'NonExistentPlugin' ];
  275. return plugins.load( [ PluginA ] )
  276. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  277. .then( () => {
  278. throw new Error( 'Test error: this promise should not be resolved successfully' );
  279. } )
  280. .catch( ( err ) => {
  281. expect( err ).to.be.an.instanceof( CKEditorError );
  282. expect( err.message ).to.match( /^plugincollection-plugin-not-found/ );
  283. sinon.assert.calledOnce( logSpy );
  284. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  285. } );
  286. } );
  287. } );
  288. describe( 'get()', () => {
  289. it( 'retrieves plugin by its constructor', () => {
  290. class SomePlugin extends Plugin {}
  291. availablePlugins.push( SomePlugin );
  292. let plugins = new PluginCollection( editor, availablePlugins );
  293. return plugins.load( [ SomePlugin ] )
  294. .then( () => {
  295. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  296. } );
  297. } );
  298. it( 'retrieves plugin by its name and constructor', () => {
  299. class SomePlugin extends Plugin {}
  300. SomePlugin.pluginName = 'foo/bar';
  301. availablePlugins.push( SomePlugin );
  302. let plugins = new PluginCollection( editor, availablePlugins );
  303. return plugins.load( [ SomePlugin ] )
  304. .then( () => {
  305. expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
  306. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  307. } );
  308. } );
  309. } );
  310. describe( 'iterator', () => {
  311. it( 'exists', () => {
  312. let plugins = new PluginCollection( editor, availablePlugins );
  313. expect( plugins ).to.have.property( Symbol.iterator );
  314. } );
  315. it( 'returns only plugins by constructors', () => {
  316. class SomePlugin1 extends Plugin {}
  317. class SomePlugin2 extends Plugin {}
  318. SomePlugin2.pluginName = 'foo/bar';
  319. availablePlugins.push( SomePlugin1 );
  320. availablePlugins.push( SomePlugin2 );
  321. let plugins = new PluginCollection( editor, availablePlugins );
  322. return plugins.load( [ SomePlugin1, SomePlugin2 ] )
  323. .then( () => {
  324. const pluginConstructors = Array.from( plugins )
  325. .map( entry => entry[ 0 ] );
  326. expect( pluginConstructors ).to.have.members( [ SomePlugin1, SomePlugin2 ] );
  327. } );
  328. } );
  329. } );
  330. } );
  331. function createPlugin( name ) {
  332. const P = class extends Plugin {
  333. constructor( editor ) {
  334. super( editor );
  335. this.pluginName = name;
  336. }
  337. };
  338. P.pluginName = name;
  339. return P;
  340. }
  341. function getPlugins( pluginCollection ) {
  342. return Array.from( pluginCollection )
  343. .map( entry => entry[ 1 ] ); // Get instances.
  344. }
  345. function getPluginsFromSpy( addSpy ) {
  346. return addSpy.args
  347. .map( ( arg ) => arg[ 0 ] )
  348. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  349. // Return only pluginClass => plugin entries as these will always represent all plugins.
  350. .filter( ( plugin ) => typeof plugin == 'function' );
  351. }
  352. function getPluginNames( plugins ) {
  353. return plugins.map( ( plugin ) => plugin.pluginName );
  354. }