8
0

plugincollection.js 23 KB

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