plugincollection.js 16 KB

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