plugincollection.js 11 KB

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