plugincollection.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const modules = bender.amd.require( 'plugincollection', 'plugin', 'editor', 'log' );
  7. let editor;
  8. let PluginA, PluginB;
  9. class TestError extends Error {}
  10. bender.tools.createSinonSandbox();
  11. before( () => {
  12. const Editor = modules.editor;
  13. const Plugin = modules.plugin;
  14. PluginA = class extends Plugin {};
  15. PluginB = class extends Plugin {};
  16. editor = new Editor( document.body.appendChild( document.createElement( 'div' ) ) );
  17. } );
  18. // Create fake plugins that will be used on tests.
  19. CKEDITOR.define( 'plugin!A', () => {
  20. return PluginA;
  21. } );
  22. CKEDITOR.define( 'plugin!B', () => {
  23. return PluginB;
  24. } );
  25. CKEDITOR.define( 'plugin!C', [ 'plugin', 'plugin!B' ], ( Plugin ) => {
  26. return class extends Plugin {};
  27. } );
  28. CKEDITOR.define( 'plugin!D', [ 'plugin', 'plugin!A', 'plugin!C' ], ( Plugin ) => {
  29. return class extends Plugin {};
  30. } );
  31. CKEDITOR.define( 'plugin!E', [ 'plugin', 'plugin!F' ], ( Plugin ) => {
  32. return class extends Plugin {};
  33. } );
  34. CKEDITOR.define( 'plugin!F', [ 'plugin', 'plugin!E' ], ( Plugin ) => {
  35. return class extends Plugin {};
  36. } );
  37. CKEDITOR.define( 'plugin!G', () => {
  38. throw new TestError( 'Some error inside a plugin' );
  39. } );
  40. CKEDITOR.define( 'plugin!H', [ 'plugin', 'plugin!H/a' ], ( Plugin ) => {
  41. return class extends Plugin {};
  42. } );
  43. let spies = {};
  44. // Note: This is NOT a plugin.
  45. CKEDITOR.define( 'plugin!H/a', [ 'plugin!H/a/b' ], () => {
  46. return ( spies[ 'plugin!H/a' ] = sinon.spy() );
  47. } );
  48. // Note: This is NOT a plugin.
  49. CKEDITOR.define( 'plugin!H/a/b', [ 'c' ], () => {
  50. return ( spies[ 'plugin!H/a/b' ] = sinon.spy() );
  51. } );
  52. // Note: This is NOT a plugin.
  53. CKEDITOR.define( 'c', () => {
  54. return ( spies.c = sinon.spy() );
  55. } );
  56. CKEDITOR.define( 'plugin!I', [ 'plugin', 'plugin!J' ], ( Plugin ) => {
  57. return class extends Plugin {};
  58. } );
  59. // Note: This is NOT a plugin.
  60. CKEDITOR.define( 'plugin!J', () => {
  61. return function() {
  62. return ( spies.jSpy = sinon.spy() );
  63. };
  64. } );
  65. /////////////
  66. describe( 'load', () => {
  67. it( 'should not fail when trying to load 0 plugins (empty string)', () => {
  68. const PluginCollection = modules.plugincollection;
  69. let plugins = new PluginCollection( editor );
  70. return plugins.load( '' )
  71. .then( () => {
  72. expect( plugins.length ).to.equal( 0 );
  73. } );
  74. } );
  75. it( 'should not fail when trying to load 0 plugins (undefined)', () => {
  76. const PluginCollection = modules.plugincollection;
  77. let plugins = new PluginCollection( editor );
  78. return plugins.load()
  79. .then( () => {
  80. expect( plugins.length ).to.equal( 0 );
  81. } );
  82. } );
  83. it( 'should add collection items for loaded plugins', () => {
  84. const PluginCollection = modules.plugincollection;
  85. let plugins = new PluginCollection( editor );
  86. return plugins.load( 'A,B' )
  87. .then( () => {
  88. expect( plugins.length ).to.equal( 2 );
  89. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  90. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  91. } );
  92. } );
  93. it( 'should load dependency plugins', () => {
  94. const PluginCollection = modules.plugincollection;
  95. let plugins = new PluginCollection( editor );
  96. let spy = sinon.spy( plugins, 'add' );
  97. return plugins.load( 'A,C' )
  98. .then( ( loadedPlugins ) => {
  99. expect( plugins.length ).to.equal( 3 );
  100. expect( getPluginNamesFromSpy( spy ) ).to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins.add()' );
  101. expect( getPluginNames( loadedPlugins ) ).to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  102. } );
  103. } );
  104. it( 'should be ok when dependencies are loaded first', () => {
  105. const PluginCollection = modules.plugincollection;
  106. let plugins = new PluginCollection( editor );
  107. let spy = sinon.spy( plugins, 'add' );
  108. return plugins.load( 'A,B,C' )
  109. .then( ( loadedPlugins ) => {
  110. expect( plugins.length ).to.equal( 3 );
  111. expect( getPluginNamesFromSpy( spy ) ).to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins.add()' );
  112. expect( getPluginNames( loadedPlugins ) ).to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  113. } );
  114. } );
  115. it( 'should load deep dependency plugins', () => {
  116. const PluginCollection = modules.plugincollection;
  117. let plugins = new PluginCollection( editor );
  118. let spy = sinon.spy( plugins, 'add' );
  119. return plugins.load( 'D' )
  120. .then( ( loadedPlugins ) => {
  121. expect( plugins.length ).to.equal( 4 );
  122. // The order must have dependencies first.
  123. expect( getPluginNamesFromSpy( spy ) ).to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins.add()' );
  124. expect( getPluginNames( loadedPlugins ) ).to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  125. } );
  126. } );
  127. it( 'should handle cross dependency plugins', () => {
  128. const PluginCollection = modules.plugincollection;
  129. let plugins = new PluginCollection( editor );
  130. let spy = sinon.spy( plugins, 'add' );
  131. return plugins.load( 'A,E' )
  132. .then( ( loadedPlugins ) => {
  133. expect( plugins.length ).to.equal( 3 );
  134. // The order must have dependencies first.
  135. expect( getPluginNamesFromSpy( spy ) ).to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins.add()' );
  136. expect( getPluginNames( loadedPlugins ) ).to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  137. } );
  138. } );
  139. it( 'should set the `editor` property on loaded plugins', () => {
  140. const PluginCollection = modules.plugincollection;
  141. let plugins = new PluginCollection( editor );
  142. return plugins.load( 'A,B' )
  143. .then( () => {
  144. expect( plugins.get( 'A' ).editor ).to.equal( editor );
  145. expect( plugins.get( 'B' ).editor ).to.equal( editor );
  146. } );
  147. } );
  148. it( 'should set the `path` property on loaded plugins', () => {
  149. const PluginCollection = modules.plugincollection;
  150. let plugins = new PluginCollection( editor );
  151. return plugins.load( 'A,B' )
  152. .then( () => {
  153. expect( plugins.get( 'A' ).path ).to.equal( CKEDITOR.getPluginPath( 'A' ) );
  154. expect( plugins.get( 'B' ).path ).to.equal( CKEDITOR.getPluginPath( 'B' ) );
  155. } );
  156. } );
  157. it( 'should set the `deps` property on loaded plugins', () => {
  158. const PluginCollection = modules.plugincollection;
  159. let plugins = new PluginCollection( editor );
  160. return plugins.load( 'A,D' )
  161. .then( () => {
  162. expect( plugins.get( 'A' ).deps ).to.deep.equal( [] );
  163. expect( plugins.get( 'B' ).deps ).to.deep.equal( [] );
  164. expect( plugins.get( 'C' ).deps ).to.deep.equal( [ 'B' ] );
  165. expect( plugins.get( 'D' ).deps ).to.deep.equal( [ 'A', 'C' ] );
  166. } );
  167. } );
  168. it( 'should reject on invalid plugin names (forward require.js loading error)', () => {
  169. const PluginCollection = modules.plugincollection;
  170. let log = modules.log;
  171. let logSpy = bender.sinon.stub( log, 'error' );
  172. let plugins = new PluginCollection( editor );
  173. return plugins.load( 'A,BAD,B' )
  174. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  175. .then( () => {
  176. throw new Error( 'Test error: this promise should not be resolved successfully' );
  177. } )
  178. .catch( ( err ) => {
  179. expect( err ).to.be.an.instanceof( Error );
  180. // Make sure it's the Require.JS error, not the one thrown above.
  181. expect( err.message ).to.match( /^Script error for/ );
  182. sinon.assert.calledOnce( logSpy );
  183. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  184. } );
  185. } );
  186. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  187. const PluginCollection = modules.plugincollection;
  188. let log = modules.log;
  189. let logSpy = bender.sinon.stub( log, 'error' );
  190. let plugins = new PluginCollection( editor );
  191. return plugins.load( 'A,G,B' )
  192. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  193. .then( () => {
  194. throw new Error( 'Test error: this promise should not be resolved successfully' );
  195. } )
  196. .catch( ( err ) => {
  197. expect( err ).to.be.an.instanceof( TestError );
  198. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  199. sinon.assert.calledOnce( logSpy );
  200. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  201. } );
  202. } );
  203. it( 'should load `deps` which are not plugins', () => {
  204. const PluginCollection = modules.plugincollection;
  205. let plugins = new PluginCollection( editor );
  206. expect( spies ).to.be.empty;
  207. return plugins.load( 'H' )
  208. .then( () => {
  209. expect( plugins.get( 'H' ).deps ).to.deep.equal( [ 'H/a' ] );
  210. // Non–plugin dependencies should be loaded (spy exists)...
  211. expect( spies ).to.have.keys( [
  212. 'plugin!H/a', 'plugin!H/a/b', 'c'
  213. ] );
  214. // ...but not be executed (called == false)...
  215. expect( spies[ 'plugin!H/a' ].called ).to.be.false;
  216. expect( spies[ 'plugin!H/a/b' ].called ).to.be.false;
  217. expect( spies.c.called ).to.be.false;
  218. expect( plugins.length ).to.be.equal( 1 );
  219. } );
  220. } );
  221. it( 'should load instances of Plugin only', () => {
  222. const PluginCollection = modules.plugincollection;
  223. let plugins = new PluginCollection( editor );
  224. return plugins.load( 'I' )
  225. .then( () => {
  226. throw new Error( 'Test error: this promise should not be resolved successfully' );
  227. } ).catch( err => {
  228. expect( err.name ).to.be.equal( 'CKEditorError' );
  229. expect( err.message ).to.match( /^plugincollection-instance:/ );
  230. } );
  231. } );
  232. it( 'should cancel loading module which looks like a plugin but is a normal module', () => {
  233. const PluginCollection = modules.plugincollection;
  234. let plugins = new PluginCollection( editor );
  235. return plugins.load( 'J' )
  236. .then( () => {
  237. throw new Error( 'Test error: this promise should not be resolved successfully' );
  238. } ).catch( () => {
  239. // Path would be set if code execution wasn't stopped when we rejected the promise
  240. // (based on a real mistake we've made).
  241. expect( spies.jSpy.path ).to.be.undefined;
  242. } );
  243. } );
  244. } );
  245. function getPluginNamesFromSpy( addSpy ) {
  246. return addSpy.args.map( ( arg ) => {
  247. return arg[ 0 ].name;
  248. } );
  249. }
  250. function getPluginNames( plugins ) {
  251. return plugins.map( ( arg ) => {
  252. return arg.name;
  253. } );
  254. }