plugincollection.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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;
  12. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI, 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. PluginX = class extends Plugin {
  28. constructor( editor ) {
  29. super( editor );
  30. throw new TestError( 'Some error inside a plugin' );
  31. }
  32. };
  33. PluginC.requires = [ PluginB ];
  34. PluginD.requires = [ PluginA, PluginC ];
  35. PluginF.requires = [ PluginE ];
  36. PluginE.requires = [ PluginF ];
  37. PluginH.requires = [ PluginI ];
  38. editor = new Editor();
  39. } );
  40. describe( 'PluginCollection', () => {
  41. describe( 'load()', () => {
  42. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  43. let plugins = new PluginCollection( editor );
  44. return plugins.load( [] )
  45. .then( () => {
  46. expect( getPlugins( plugins ) ).to.be.empty;
  47. } );
  48. } );
  49. it( 'should add collection items for loaded plugins', () => {
  50. let plugins = new PluginCollection( editor );
  51. return plugins.load( [ PluginA, PluginB ] )
  52. .then( () => {
  53. expect( getPlugins( plugins ).length ).to.equal( 2 );
  54. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  55. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  56. } );
  57. } );
  58. it( 'should load dependency plugins', () => {
  59. let plugins = new PluginCollection( editor );
  60. let spy = sinon.spy( plugins, '_add' );
  61. return plugins.load( [ PluginA, PluginC ] )
  62. .then( ( loadedPlugins ) => {
  63. expect( getPlugins( plugins ).length ).to.equal( 3 );
  64. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  65. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  66. expect( getPluginNames( loadedPlugins ) )
  67. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  68. } );
  69. } );
  70. it( 'should be ok when dependencies are loaded first', () => {
  71. let plugins = new PluginCollection( editor );
  72. let spy = sinon.spy( plugins, '_add' );
  73. return plugins.load( [ PluginA, PluginB, PluginC ] )
  74. .then( ( loadedPlugins ) => {
  75. expect( getPlugins( plugins ).length ).to.equal( 3 );
  76. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  77. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  78. expect( getPluginNames( loadedPlugins ) )
  79. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  80. } );
  81. } );
  82. it( 'should load deep dependency plugins', () => {
  83. let plugins = new PluginCollection( editor );
  84. let spy = sinon.spy( plugins, '_add' );
  85. return plugins.load( [ PluginD ] )
  86. .then( ( loadedPlugins ) => {
  87. expect( getPlugins( plugins ).length ).to.equal( 4 );
  88. // The order must have dependencies first.
  89. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  90. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  91. expect( getPluginNames( loadedPlugins ) )
  92. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  93. } );
  94. } );
  95. it( 'should handle cross dependency plugins', () => {
  96. let plugins = new PluginCollection( editor );
  97. let spy = sinon.spy( plugins, '_add' );
  98. return plugins.load( [ PluginA, PluginE ] )
  99. .then( ( loadedPlugins ) => {
  100. expect( getPlugins( plugins ).length ).to.equal( 3 );
  101. // The order must have dependencies first.
  102. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  103. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  104. expect( getPluginNames( loadedPlugins ) )
  105. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  106. } );
  107. } );
  108. it( 'should load grand child classes', () => {
  109. let plugins = new PluginCollection( editor );
  110. return plugins.load( [ PluginG ] )
  111. .then( () => {
  112. expect( getPlugins( plugins ).length ).to.equal( 1 );
  113. } );
  114. } );
  115. it( 'should set the `editor` property on loaded plugins', () => {
  116. let plugins = new PluginCollection( editor );
  117. return plugins.load( [ PluginA, PluginB ] )
  118. .then( () => {
  119. expect( plugins.get( PluginA ).editor ).to.equal( editor );
  120. expect( plugins.get( PluginB ).editor ).to.equal( editor );
  121. } );
  122. } );
  123. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  124. let logSpy = testUtils.sinon.stub( log, 'error' );
  125. let plugins = new PluginCollection( editor );
  126. return plugins.load( [ PluginA, PluginX, PluginB ] )
  127. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  128. .then( () => {
  129. throw new Error( 'Test error: this promise should not be resolved successfully' );
  130. } )
  131. .catch( ( err ) => {
  132. expect( err ).to.be.an.instanceof( TestError );
  133. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  134. sinon.assert.calledOnce( logSpy );
  135. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  136. } );
  137. } );
  138. it( 'should reject when loading a module which is not a plugin', () => {
  139. let logSpy = testUtils.sinon.stub( log, 'error' );
  140. let plugins = new PluginCollection( editor );
  141. class Y {}
  142. return plugins.load( [ Y ] )
  143. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  144. .then( () => {
  145. throw new Error( 'Test error: this promise should not be resolved successfully' );
  146. } )
  147. .catch( ( err ) => {
  148. expect( err ).to.be.an.instanceof( CKEditorError );
  149. expect( err.message ).to.match( /^plugincollection-instance/ );
  150. sinon.assert.calledOnce( logSpy );
  151. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  152. } );
  153. } );
  154. } );
  155. describe( 'get()', () => {
  156. it( 'retrieves plugin by its constructor', () => {
  157. let plugins = new PluginCollection( editor );
  158. class SomePlugin extends Plugin {}
  159. return plugins.load( [ SomePlugin ] )
  160. .then( () => {
  161. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  162. } );
  163. } );
  164. it( 'retrieves plugin by its name and constructor', () => {
  165. let plugins = new PluginCollection( editor );
  166. class SomePlugin extends Plugin {}
  167. SomePlugin.pluginName = 'foo/bar';
  168. return plugins.load( [ SomePlugin ] )
  169. .then( () => {
  170. expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
  171. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  172. } );
  173. } );
  174. } );
  175. describe( 'iterator', () => {
  176. it( 'exists', () => {
  177. let plugins = new PluginCollection( editor );
  178. expect( plugins ).to.have.property( Symbol.iterator );
  179. } );
  180. it( 'returns only plugins by constructors', () => {
  181. let plugins = new PluginCollection( editor );
  182. class SomePlugin1 extends Plugin {}
  183. class SomePlugin2 extends Plugin {}
  184. SomePlugin2.pluginName = 'foo/bar';
  185. return plugins.load( [ SomePlugin1, SomePlugin2 ] )
  186. .then( () => {
  187. const pluginConstructors = Array.from( plugins )
  188. .map( entry => entry[ 0 ] );
  189. expect( pluginConstructors ).to.have.members( [ SomePlugin1, SomePlugin2 ] );
  190. } );
  191. } );
  192. } );
  193. } );
  194. function createPlugin( name ) {
  195. const P = class extends Plugin {
  196. constructor( editor ) {
  197. super( editor );
  198. this.pluginName = name;
  199. }
  200. };
  201. P.pluginName = name;
  202. return P;
  203. }
  204. function getPlugins( pluginCollection ) {
  205. return Array.from( pluginCollection )
  206. .map( entry => entry[ 1 ] ); // Get instances.
  207. }
  208. function getPluginsFromSpy( addSpy ) {
  209. return addSpy.args
  210. .map( ( arg ) => arg[ 0 ] )
  211. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  212. // Return only pluginClass => plugin entries as these will always represent all plugins.
  213. .filter( ( plugin ) => typeof plugin == 'function' );
  214. }
  215. function getPluginNames( plugins ) {
  216. return plugins.map( ( plugin ) => plugin.pluginName );
  217. }