8
0

plugincollection.js 8.9 KB

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