plugincollection.js 9.6 KB

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