plugincollection.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. const 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. const 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. const 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. const plugins = new PluginCollection( editor, availablePlugins );
  89. const 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. const plugins = new PluginCollection( editor, availablePlugins );
  101. const 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. const plugins = new PluginCollection( editor, availablePlugins );
  113. const 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. const plugins = new PluginCollection( editor, availablePlugins );
  125. const 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. const plugins = new PluginCollection( editor, availablePlugins );
  138. const 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. const 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 load plugin which does not extend the base Plugin class', () => {
  157. class Y { }
  158. const plugins = new PluginCollection( editor, availablePlugins );
  159. return plugins.load( [ Y ] )
  160. .then( () => {
  161. expect( getPlugins( plugins ).length ).to.equal( 1 );
  162. } );
  163. } );
  164. it( 'should load plugin which is a simple function', () => {
  165. function pluginAsFunction( editor ) {
  166. this.editor = editor;
  167. }
  168. const plugins = new PluginCollection( editor, availablePlugins );
  169. return plugins.load( [ pluginAsFunction ] )
  170. .then( () => {
  171. expect( getPlugins( plugins ).length ).to.equal( 1 );
  172. } );
  173. } );
  174. it( 'should set the `editor` property on loaded plugins', () => {
  175. const plugins = new PluginCollection( editor, availablePlugins );
  176. function pluginAsFunction( editor ) {
  177. this.editor = editor;
  178. }
  179. class Y {
  180. constructor( editor ) {
  181. this.editor = editor;
  182. }
  183. }
  184. return plugins.load( [ PluginA, PluginB, pluginAsFunction, Y ] )
  185. .then( () => {
  186. expect( plugins.get( PluginA ).editor ).to.equal( editor );
  187. expect( plugins.get( PluginB ).editor ).to.equal( editor );
  188. expect( plugins.get( pluginAsFunction ).editor ).to.equal( editor );
  189. expect( plugins.get( Y ).editor ).to.equal( editor );
  190. } );
  191. } );
  192. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  193. const logSpy = testUtils.sinon.stub( log, 'error' );
  194. const plugins = new PluginCollection( editor, availablePlugins );
  195. return plugins.load( [ PluginA, PluginX, PluginB ] )
  196. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  197. .then( () => {
  198. throw new Error( 'Test error: this promise should not be resolved successfully' );
  199. } )
  200. .catch( err => {
  201. expect( err ).to.be.an.instanceof( TestError );
  202. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  203. sinon.assert.calledOnce( logSpy );
  204. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  205. } );
  206. } );
  207. it( 'should reject when loading non-existent plugin', () => {
  208. const logSpy = testUtils.sinon.stub( log, 'error' );
  209. const plugins = new PluginCollection( editor, availablePlugins );
  210. return plugins.load( [ 'NonExistentPlugin' ] )
  211. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  212. .then( () => {
  213. throw new Error( 'Test error: this promise should not be resolved successfully' );
  214. } )
  215. .catch( err => {
  216. expect( err ).to.be.an.instanceof( CKEditorError );
  217. expect( err.message ).to.match( /^plugincollection-plugin-not-found/ );
  218. sinon.assert.calledOnce( logSpy );
  219. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-plugin-not-found:/ );
  220. } );
  221. } );
  222. it( 'should load chosen plugins (plugins and removePlugins are constructors)', () => {
  223. const plugins = new PluginCollection( editor, availablePlugins );
  224. return plugins.load( [ PluginA, PluginB, PluginC ], [ PluginA ] )
  225. .then( () => {
  226. expect( getPlugins( plugins ).length ).to.equal( 2 );
  227. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  228. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  229. } );
  230. } );
  231. it( 'should load chosen plugins (plugins are constructors, removePlugins are names)', () => {
  232. const plugins = new PluginCollection( editor, availablePlugins );
  233. return plugins.load( [ PluginA, PluginB, PluginC ], [ 'A' ] )
  234. .then( () => {
  235. expect( getPlugins( plugins ).length ).to.equal( 2 );
  236. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  237. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  238. } );
  239. } );
  240. it( 'should load chosen plugins (plugins and removePlugins are names)', () => {
  241. const plugins = new PluginCollection( editor, availablePlugins );
  242. return plugins.load( [ 'A', 'B', 'C' ], [ 'A' ] )
  243. .then( () => {
  244. expect( getPlugins( plugins ).length ).to.equal( 2 );
  245. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  246. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  247. } );
  248. } );
  249. it( 'should load chosen plugins (plugins are names, removePlugins are constructors)', () => {
  250. const plugins = new PluginCollection( editor, availablePlugins );
  251. return plugins.load( [ 'A', 'B', 'C' ], [ PluginA ] )
  252. .then( () => {
  253. expect( getPlugins( plugins ).length ).to.equal( 2 );
  254. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  255. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  256. } );
  257. } );
  258. it( 'should load chosen plugins (plugins are names, removePlugins contains an anonymous plugin)', () => {
  259. class AnonymousPlugin {}
  260. const plugins = new PluginCollection( editor, [ AnonymousPlugin ].concat( availablePlugins ) );
  261. return plugins.load( [ AnonymousPlugin, 'A', 'B' ], [ AnonymousPlugin ] )
  262. .then( () => {
  263. expect( getPlugins( plugins ).length ).to.equal( 2 );
  264. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  265. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  266. } );
  267. } );
  268. it( 'should reject when loaded plugin requires not allowed plugins', () => {
  269. const logSpy = testUtils.sinon.stub( log, 'error' );
  270. const plugins = new PluginCollection( editor, availablePlugins );
  271. return plugins.load( [ PluginA, PluginB, PluginC, PluginD ], [ PluginA, PluginB ] )
  272. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  273. .then( () => {
  274. throw new Error( 'Test error: this promise should not be resolved successfully' );
  275. } )
  276. .catch( err => {
  277. expect( err ).to.be.an.instanceof( CKEditorError );
  278. expect( err.message ).to.match( /^plugincollection-required/ );
  279. expect( logSpy.calledTwice ).to.equal( true );
  280. } );
  281. } );
  282. } );
  283. describe( 'get()', () => {
  284. it( 'retrieves plugin by its constructor', () => {
  285. class SomePlugin extends Plugin {}
  286. availablePlugins.push( SomePlugin );
  287. const plugins = new PluginCollection( editor, availablePlugins );
  288. return plugins.load( [ SomePlugin ] )
  289. .then( () => {
  290. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  291. } );
  292. } );
  293. it( 'retrieves plugin by its name and constructor', () => {
  294. class SomePlugin extends Plugin {}
  295. SomePlugin.pluginName = 'foo/bar';
  296. availablePlugins.push( SomePlugin );
  297. const plugins = new PluginCollection( editor, availablePlugins );
  298. return plugins.load( [ SomePlugin ] )
  299. .then( () => {
  300. expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
  301. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  302. } );
  303. } );
  304. } );
  305. describe( 'destroy()', () => {
  306. it( 'calls "destroy" method on each loaded plugin', () => {
  307. let destroySpyForPluginA, destroySpyForPluginB;
  308. const plugins = new PluginCollection( editor, [] );
  309. return plugins.load( [ PluginA, PluginB ] )
  310. .then( () => {
  311. destroySpyForPluginA = testUtils.sinon.spy( plugins.get( PluginA ), 'destroy' );
  312. destroySpyForPluginB = testUtils.sinon.spy( plugins.get( PluginB ), 'destroy' );
  313. return plugins.destroy();
  314. } )
  315. .then( () => {
  316. expect( destroySpyForPluginA.calledOnce ).to.equal( true );
  317. expect( destroySpyForPluginB.calledOnce ).to.equal( true );
  318. } );
  319. } );
  320. } );
  321. describe( 'iterator', () => {
  322. it( 'exists', () => {
  323. const plugins = new PluginCollection( editor, availablePlugins );
  324. expect( plugins ).to.have.property( Symbol.iterator );
  325. } );
  326. it( 'returns only plugins by constructors', () => {
  327. class SomePlugin1 extends Plugin {}
  328. class SomePlugin2 extends Plugin {}
  329. SomePlugin2.pluginName = 'foo/bar';
  330. availablePlugins.push( SomePlugin1 );
  331. availablePlugins.push( SomePlugin2 );
  332. const plugins = new PluginCollection( editor, availablePlugins );
  333. return plugins.load( [ SomePlugin1, SomePlugin2 ] )
  334. .then( () => {
  335. const pluginConstructors = Array.from( plugins )
  336. .map( entry => entry[ 0 ] );
  337. expect( pluginConstructors ).to.have.members( [ SomePlugin1, SomePlugin2 ] );
  338. } );
  339. } );
  340. } );
  341. } );
  342. function createPlugin( name ) {
  343. const P = class extends Plugin {
  344. constructor( editor ) {
  345. super( editor );
  346. this.pluginName = name;
  347. }
  348. };
  349. P.pluginName = name;
  350. return P;
  351. }
  352. function getPlugins( pluginCollection ) {
  353. return Array.from( pluginCollection )
  354. .map( entry => entry[ 1 ] ); // Get instances.
  355. }
  356. function getPluginsFromSpy( addSpy ) {
  357. return addSpy.args
  358. .map( arg => arg[ 0 ] )
  359. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  360. // Return only pluginClass => plugin entries as these will always represent all plugins.
  361. .filter( plugin => typeof plugin == 'function' );
  362. }
  363. function getPluginNames( plugins ) {
  364. return plugins.map( plugin => plugin.pluginName );
  365. }