plugincollection.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals setTimeout, console */
  6. import Editor from '../src/editor/editor';
  7. import PluginCollection from '../src/plugincollection';
  8. import Plugin from '../src/plugin';
  9. import { expectToThrowCKEditorError, assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. let editor, availablePlugins;
  11. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI, PluginJ, PluginK, PluginX, PluginFoo, AnotherPluginFoo;
  12. class TestError extends Error {}
  13. class ChildPlugin extends Plugin {}
  14. class GrandPlugin extends ChildPlugin {}
  15. describe( 'PluginCollection', () => {
  16. before( () => {
  17. PluginA = createPlugin( 'A' );
  18. PluginB = createPlugin( 'B' );
  19. PluginC = createPlugin( 'C' );
  20. PluginD = createPlugin( 'D' );
  21. PluginE = createPlugin( 'E' );
  22. PluginF = createPlugin( 'F' );
  23. PluginG = createPlugin( 'G', GrandPlugin );
  24. PluginH = createPlugin( 'H' );
  25. PluginI = createPlugin( 'I' );
  26. PluginJ = createPlugin( 'J' );
  27. PluginK = createPlugin( 'K' );
  28. PluginX = class extends Plugin {
  29. constructor( editor ) {
  30. super( editor );
  31. throw new TestError( 'Some error inside a plugin' );
  32. }
  33. };
  34. PluginFoo = createPlugin( 'Foo' );
  35. AnotherPluginFoo = createPlugin( 'Foo' );
  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. 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. PluginFoo.requires = [];
  61. } );
  62. afterEach( () => {
  63. sinon.restore();
  64. } );
  65. describe( 'load()', () => {
  66. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  67. const plugins = new PluginCollection( editor, availablePlugins );
  68. return plugins.init( [] )
  69. .then( () => {
  70. expect( getPlugins( plugins ) ).to.be.empty;
  71. } );
  72. } );
  73. it( 'should add collection items for loaded plugins', () => {
  74. const plugins = new PluginCollection( editor, availablePlugins );
  75. return plugins.init( [ PluginA, PluginB ] )
  76. .then( () => {
  77. expect( getPlugins( plugins ).length ).to.equal( 2 );
  78. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  79. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  80. } );
  81. } );
  82. it( 'should add collection items for loaded plugins using plugin names', () => {
  83. const plugins = new PluginCollection( editor, availablePlugins );
  84. return plugins.init( [ 'A', 'B' ] )
  85. .then( () => {
  86. expect( getPlugins( plugins ).length ).to.equal( 2 );
  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. const plugins = new PluginCollection( editor, availablePlugins );
  93. const spy = sinon.spy( plugins, '_add' );
  94. return plugins.init( [ PluginA, PluginC ] )
  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._add()' );
  99. expect( getPluginNames( loadedPlugins ) )
  100. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  101. } );
  102. } );
  103. it( 'should load dependency plugins defined by plugin names', () => {
  104. const plugins = new PluginCollection( editor, availablePlugins );
  105. const spy = sinon.spy( plugins, '_add' );
  106. return plugins.init( [ 'J' ] )
  107. .then( loadedPlugins => {
  108. expect( getPlugins( plugins ).length ).to.equal( 3 );
  109. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  110. .to.deep.equal( [ 'A', 'K', 'J' ], 'order by plugins._add()' );
  111. expect( getPluginNames( loadedPlugins ) )
  112. .to.deep.equal( [ 'A', 'K', 'J' ], 'order by returned value' );
  113. } );
  114. } );
  115. it( 'should be ok when dependencies are loaded first', () => {
  116. const plugins = new PluginCollection( editor, availablePlugins );
  117. const spy = sinon.spy( plugins, '_add' );
  118. return plugins.init( [ PluginA, PluginB, PluginC ] )
  119. .then( loadedPlugins => {
  120. expect( getPlugins( plugins ).length ).to.equal( 3 );
  121. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  122. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  123. expect( getPluginNames( loadedPlugins ) )
  124. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  125. } );
  126. } );
  127. it( 'should load deep dependency plugins', () => {
  128. const plugins = new PluginCollection( editor, availablePlugins );
  129. const spy = sinon.spy( plugins, '_add' );
  130. return plugins.init( [ PluginD ] )
  131. .then( loadedPlugins => {
  132. expect( getPlugins( plugins ).length ).to.equal( 4 );
  133. // The order must have dependencies first.
  134. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  135. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  136. expect( getPluginNames( loadedPlugins ) )
  137. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  138. } );
  139. } );
  140. it( 'should handle cross dependency plugins', () => {
  141. const plugins = new PluginCollection( editor, availablePlugins );
  142. const spy = sinon.spy( plugins, '_add' );
  143. return plugins.init( [ PluginA, PluginE ] )
  144. .then( loadedPlugins => {
  145. expect( getPlugins( plugins ).length ).to.equal( 3 );
  146. // The order must have dependencies first.
  147. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  148. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  149. expect( getPluginNames( loadedPlugins ) )
  150. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  151. } );
  152. } );
  153. it( 'should load grand child classes', () => {
  154. const plugins = new PluginCollection( editor, availablePlugins );
  155. return plugins.init( [ PluginG ] )
  156. .then( () => {
  157. expect( getPlugins( plugins ).length ).to.equal( 1 );
  158. } );
  159. } );
  160. it( 'should load plugin which does not extend the base Plugin class', () => {
  161. class Y {}
  162. const plugins = new PluginCollection( editor, availablePlugins );
  163. return plugins.init( [ Y ] )
  164. .then( () => {
  165. expect( getPlugins( plugins ).length ).to.equal( 1 );
  166. } );
  167. } );
  168. it( 'should load plugin which is a simple function', () => {
  169. function pluginAsFunction( editor ) {
  170. this.editor = editor;
  171. }
  172. const plugins = new PluginCollection( editor, availablePlugins );
  173. return plugins.init( [ pluginAsFunction ] )
  174. .then( () => {
  175. expect( getPlugins( plugins ).length ).to.equal( 1 );
  176. } );
  177. } );
  178. it( 'should set the `editor` property on loaded plugins', () => {
  179. const plugins = new PluginCollection( editor, availablePlugins );
  180. function pluginAsFunction( editor ) {
  181. this.editor = editor;
  182. }
  183. class Y {
  184. constructor( editor ) {
  185. this.editor = editor;
  186. }
  187. }
  188. return plugins.init( [ PluginA, PluginB, pluginAsFunction, Y ] )
  189. .then( () => {
  190. expect( plugins.get( PluginA ).editor ).to.equal( editor );
  191. expect( plugins.get( PluginB ).editor ).to.equal( editor );
  192. expect( plugins.get( pluginAsFunction ).editor ).to.equal( editor );
  193. expect( plugins.get( Y ).editor ).to.equal( editor );
  194. } );
  195. } );
  196. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  197. const consoleErrorStub = sinon.stub( console, 'error' );
  198. const plugins = new PluginCollection( editor, availablePlugins );
  199. return plugins.init( [ PluginA, PluginX, PluginB ] )
  200. // Throw here, so if by any chance plugins.init() 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( TestError );
  206. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  207. sinon.assert.calledOnce( consoleErrorStub );
  208. expect( consoleErrorStub.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  209. } );
  210. } );
  211. it( 'should reject when loading non-existent plugin', () => {
  212. const consoleErrorStub = sinon.stub( console, 'error' );
  213. const plugins = new PluginCollection( editor, availablePlugins );
  214. return plugins.init( [ 'NonExistentPlugin' ] )
  215. // Throw here, so if by any chance plugins.init() was resolved correctly catch() will be stil executed.
  216. .then( () => {
  217. throw new Error( 'Test error: this promise should not be resolved successfully' );
  218. } )
  219. .catch( err => {
  220. assertCKEditorError( err, /^plugincollection-plugin-not-found/, editor );
  221. sinon.assert.calledOnce( consoleErrorStub );
  222. expect( consoleErrorStub.args[ 0 ][ 0 ] ).to.match( /^plugincollection-plugin-not-found:/ );
  223. } );
  224. } );
  225. it( 'should load chosen plugins (plugins and removePlugins are constructors)', () => {
  226. const plugins = new PluginCollection( editor, availablePlugins );
  227. return plugins.init( [ PluginA, PluginB, PluginC ], [ PluginA ] )
  228. .then( () => {
  229. expect( getPlugins( plugins ).length ).to.equal( 2 );
  230. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  231. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  232. } );
  233. } );
  234. it( 'should load chosen plugins (plugins are constructors, removePlugins are names)', () => {
  235. const plugins = new PluginCollection( editor, availablePlugins );
  236. return plugins.init( [ PluginA, PluginB, PluginC ], [ 'A' ] )
  237. .then( () => {
  238. expect( getPlugins( plugins ).length ).to.equal( 2 );
  239. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  240. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  241. } );
  242. } );
  243. it( 'should load chosen plugins (plugins and removePlugins are names)', () => {
  244. const plugins = new PluginCollection( editor, availablePlugins );
  245. return plugins.init( [ 'A', 'B', 'C' ], [ 'A' ] )
  246. .then( () => {
  247. expect( getPlugins( plugins ).length ).to.equal( 2 );
  248. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  249. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  250. } );
  251. } );
  252. it( 'should load chosen plugins (plugins are names, removePlugins are constructors)', () => {
  253. const plugins = new PluginCollection( editor, availablePlugins );
  254. return plugins.init( [ 'A', 'B', 'C' ], [ PluginA ] )
  255. .then( () => {
  256. expect( getPlugins( plugins ).length ).to.equal( 2 );
  257. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  258. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  259. } );
  260. } );
  261. it( 'should load chosen plugins (plugins are names, removePlugins contains an anonymous plugin)', () => {
  262. class AnonymousPlugin {}
  263. const plugins = new PluginCollection( editor, [ AnonymousPlugin ].concat( availablePlugins ) );
  264. return plugins.init( [ AnonymousPlugin, 'A', 'B' ], [ AnonymousPlugin ] )
  265. .then( () => {
  266. expect( getPlugins( plugins ).length ).to.equal( 2 );
  267. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  268. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  269. } );
  270. } );
  271. it( 'should reject when loaded plugin requires not allowed plugins', () => {
  272. const consoleErrorStub = sinon.stub( console, 'error' );
  273. const plugins = new PluginCollection( editor, availablePlugins );
  274. return plugins.init( [ PluginA, PluginB, PluginC, PluginD ], [ PluginA, PluginB ] )
  275. // Throw here, so if by any chance plugins.init() was resolved correctly catch() will be still executed.
  276. .then( () => {
  277. throw new Error( 'Test error: this promise should not be resolved successfully' );
  278. } )
  279. .catch( err => {
  280. assertCKEditorError( err, /^plugincollection-required/, editor );
  281. sinon.assert.calledTwice( consoleErrorStub );
  282. } );
  283. } );
  284. it( 'should reject when loading more than one plugin with the same name', () => {
  285. const plugins = new PluginCollection( editor );
  286. const consoleErrorStub = sinon.stub( console, 'error' );
  287. return plugins.init( [ PluginFoo, AnotherPluginFoo ] )
  288. .then( () => {
  289. throw new Error( 'The `init()` method should fail.' );
  290. } )
  291. .catch( err => {
  292. assertCKEditorError( err, /^plugincollection-plugin-name-conflict:/, null, {
  293. pluginName: 'Foo',
  294. plugin1: PluginFoo,
  295. plugin2: AnotherPluginFoo
  296. } );
  297. sinon.assert.calledOnce( consoleErrorStub );
  298. } );
  299. } );
  300. it( 'should reject when loading more than one plugin with the same name (plugin requires plugin with the same name)', () => {
  301. PluginFoo.requires = [ AnotherPluginFoo ];
  302. const plugins = new PluginCollection( editor );
  303. const consoleErrorStub = sinon.stub( console, 'error' );
  304. return plugins.init( [ PluginFoo ] )
  305. .then( () => {
  306. throw new Error( 'The `init()` method should fail.' );
  307. } )
  308. .catch( err => {
  309. assertCKEditorError( err, /^plugincollection-plugin-name-conflict:/, null );
  310. sinon.assert.calledOnce( consoleErrorStub );
  311. } );
  312. } );
  313. it( 'should reject when loading more than one plugin with the same name' +
  314. '(plugin with the same name is built-in the PluginCollection)', () => {
  315. availablePlugins = [ PluginFoo ];
  316. const plugins = new PluginCollection( editor, availablePlugins );
  317. const consoleErrorStub = sinon.stub( console, 'error' );
  318. return plugins.init( [ 'Foo', AnotherPluginFoo ] )
  319. .then( () => {
  320. throw new Error( 'The `init()` method should fail.' );
  321. } )
  322. .catch( err => {
  323. assertCKEditorError( err, /^plugincollection-plugin-name-conflict:/, null );
  324. sinon.assert.calledOnce( consoleErrorStub );
  325. } );
  326. } );
  327. it( 'should get plugin from external plugins instead of creating new instance', async () => {
  328. const externalPlugins = new PluginCollection( editor );
  329. await externalPlugins.init( [ PluginA, PluginB ] );
  330. const plugins = new PluginCollection( editor, [], Array.from( externalPlugins ) );
  331. await plugins.init( [ PluginA ] );
  332. expect( getPlugins( plugins ) ).to.length( 1 );
  333. expect( plugins.get( PluginA ) ).to.equal( externalPlugins.get( PluginA ) ).to.instanceof( PluginA );
  334. } );
  335. it( 'should get plugin by name from external plugins instead of creating new instance', async () => {
  336. const externalPlugins = new PluginCollection( editor );
  337. await externalPlugins.init( [ PluginA, PluginB ] );
  338. const plugins = new PluginCollection( editor, [], Array.from( externalPlugins ) );
  339. await plugins.init( [ 'A' ] );
  340. expect( getPlugins( plugins ) ).to.length( 1 );
  341. expect( plugins.get( PluginA ) ).to.equal( externalPlugins.get( PluginA ) ).to.instanceof( PluginA );
  342. } );
  343. it( 'should get dependency of plugin from external plugins instead of creating new instance', async () => {
  344. const externalPlugins = new PluginCollection( editor );
  345. await externalPlugins.init( [ PluginA, PluginB ] );
  346. const plugins = new PluginCollection( editor, [], Array.from( externalPlugins ) );
  347. await plugins.init( [ PluginC ] );
  348. expect( getPlugins( plugins ) ).to.length( 2 );
  349. expect( plugins.get( PluginB ) ).to.equal( externalPlugins.get( PluginB ) ).to.instanceof( PluginB );
  350. expect( plugins.get( PluginC ) ).to.instanceof( PluginC );
  351. } );
  352. } );
  353. describe( 'get()', () => {
  354. it( 'retrieves plugin by its constructor', () => {
  355. class SomePlugin extends Plugin {}
  356. availablePlugins.push( SomePlugin );
  357. const plugins = new PluginCollection( editor, availablePlugins );
  358. return plugins.init( [ SomePlugin ] )
  359. .then( () => {
  360. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  361. } );
  362. } );
  363. it( 'retrieves plugin by its name and constructor', () => {
  364. class SomePlugin extends Plugin {}
  365. SomePlugin.pluginName = 'foo/bar';
  366. availablePlugins.push( SomePlugin );
  367. const plugins = new PluginCollection( editor, availablePlugins );
  368. return plugins.init( [ SomePlugin ] )
  369. .then( () => {
  370. expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
  371. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  372. } );
  373. } );
  374. it( 'throws if plugin cannot be retrieved by name', () => {
  375. const plugins = new PluginCollection( editor, availablePlugins );
  376. return plugins.init( [] ).then( () => {
  377. expectToThrowCKEditorError( () => plugins.get( 'foo' ),
  378. /^plugincollection-plugin-not-loaded:/, editor, { plugin: 'foo' }
  379. );
  380. } );
  381. } );
  382. it( 'throws if plugin cannot be retrieved by class', () => {
  383. class SomePlugin extends Plugin {}
  384. SomePlugin.pluginName = 'foo';
  385. const plugins = new PluginCollection( editor, availablePlugins );
  386. return plugins.init( [] ).then( () => {
  387. expectToThrowCKEditorError( () => plugins.get( SomePlugin ),
  388. /^plugincollection-plugin-not-loaded:/, editor, { plugin: 'foo' } );
  389. } );
  390. } );
  391. it( 'throws if plugin cannot be retrieved by class (class name in error)', () => {
  392. class SomePlugin extends Plugin {}
  393. const plugins = new PluginCollection( editor, availablePlugins );
  394. return plugins.init( [] ).then( () => {
  395. expectToThrowCKEditorError( () => plugins.get( SomePlugin ),
  396. /^plugincollection-plugin-not-loaded:/,
  397. editor, { plugin: 'SomePlugin' }
  398. );
  399. } );
  400. } );
  401. } );
  402. describe( 'has()', () => {
  403. let plugins;
  404. beforeEach( () => {
  405. plugins = new PluginCollection( editor, availablePlugins );
  406. } );
  407. it( 'returns false if plugins is not loaded (retrieved by name)', () => {
  408. expect( plugins.has( 'foobar' ) ).to.be.false;
  409. } );
  410. it( 'returns false if plugins is not loaded (retrieved by class)', () => {
  411. class SomePlugin extends Plugin {
  412. }
  413. expect( plugins.has( SomePlugin ) ).to.be.false;
  414. } );
  415. it( 'returns true if plugins is loaded (retrieved by name)', () => {
  416. return plugins.init( [ PluginA ] ).then( () => {
  417. expect( plugins.has( 'A' ) ).to.be.true;
  418. } );
  419. } );
  420. it( 'returns true if plugins is loaded (retrieved by class)', () => {
  421. return plugins.init( [ PluginA ] ).then( () => {
  422. expect( plugins.has( PluginA ) ).to.be.true;
  423. } );
  424. } );
  425. } );
  426. describe( 'destroy()', () => {
  427. it( 'calls Plugin#destroy() method on every loaded plugin', () => {
  428. let destroySpyForPluginA, destroySpyForPluginB;
  429. const plugins = new PluginCollection( editor, [] );
  430. return plugins.init( [ PluginA, PluginB ] )
  431. .then( () => {
  432. destroySpyForPluginA = sinon.spy( plugins.get( PluginA ), 'destroy' );
  433. destroySpyForPluginB = sinon.spy( plugins.get( PluginB ), 'destroy' );
  434. return plugins.destroy();
  435. } )
  436. .then( () => {
  437. expect( destroySpyForPluginA.calledOnce ).to.equal( true );
  438. expect( destroySpyForPluginB.calledOnce ).to.equal( true );
  439. } );
  440. } );
  441. it( 'waits until all plugins are destroyed', () => {
  442. const destroyedPlugins = [];
  443. class AsynchronousPluginA extends Plugin {
  444. destroy() {
  445. return new Promise( resolve => {
  446. setTimeout( () => {
  447. super.destroy();
  448. destroyedPlugins.push( 'AsynchronousPluginA.destroy()' );
  449. resolve();
  450. } );
  451. } );
  452. }
  453. }
  454. class AsynchronousPluginB extends Plugin {
  455. destroy() {
  456. return new Promise( resolve => {
  457. setTimeout( () => {
  458. super.destroy();
  459. destroyedPlugins.push( 'AsynchronousPluginB.destroy()' );
  460. resolve();
  461. } );
  462. } );
  463. }
  464. }
  465. const plugins = new PluginCollection( editor, [] );
  466. return plugins.init( [ AsynchronousPluginA, AsynchronousPluginB ] )
  467. .then( () => plugins.destroy() )
  468. .then( () => {
  469. expect( destroyedPlugins ).to.contain( 'AsynchronousPluginB.destroy()' );
  470. expect( destroyedPlugins ).to.contain( 'AsynchronousPluginA.destroy()' );
  471. } );
  472. } );
  473. it( 'does not execute Plugin#destroy() for plugins which do not have this method', () => {
  474. class FooPlugin {
  475. constructor( editor ) {
  476. this.editor = editor;
  477. }
  478. }
  479. const plugins = new PluginCollection( editor, [] );
  480. return plugins.init( [ PluginA, FooPlugin ] )
  481. .then( () => plugins.destroy() )
  482. .then( destroyedPlugins => {
  483. expect( destroyedPlugins.length ).to.equal( 1 );
  484. } );
  485. } );
  486. } );
  487. describe( 'iterator', () => {
  488. it( 'exists', () => {
  489. const plugins = new PluginCollection( editor, availablePlugins );
  490. expect( plugins ).to.have.property( Symbol.iterator );
  491. } );
  492. it( 'returns only plugins by constructors', () => {
  493. class SomePlugin1 extends Plugin {}
  494. class SomePlugin2 extends Plugin {}
  495. SomePlugin2.pluginName = 'foo/bar';
  496. availablePlugins.push( SomePlugin1 );
  497. availablePlugins.push( SomePlugin2 );
  498. const plugins = new PluginCollection( editor, availablePlugins );
  499. return plugins.init( [ SomePlugin1, SomePlugin2 ] )
  500. .then( () => {
  501. const pluginConstructors = Array.from( plugins )
  502. .map( entry => entry[ 0 ] );
  503. expect( pluginConstructors ).to.have.members( [ SomePlugin1, SomePlugin2 ] );
  504. } );
  505. } );
  506. } );
  507. } );
  508. function createPlugin( name ) {
  509. const P = class extends Plugin {
  510. constructor( editor ) {
  511. super( editor );
  512. this.pluginName = name;
  513. }
  514. };
  515. P.pluginName = name;
  516. return P;
  517. }
  518. function getPlugins( pluginCollection ) {
  519. return Array.from( pluginCollection )
  520. .map( entry => entry[ 1 ] ); // Get instances.
  521. }
  522. function getPluginsFromSpy( addSpy ) {
  523. return addSpy.args
  524. .map( arg => arg[ 0 ] )
  525. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  526. // Return only pluginClass => plugin entries as these will always represent all plugins.
  527. .filter( plugin => typeof plugin == 'function' );
  528. }
  529. function getPluginNames( plugins ) {
  530. return plugins.map( plugin => plugin.pluginName );
  531. }