8
0

plugincollection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 function() {
  70. return ( spies.jSpy = sinon.spy() );
  71. };
  72. } );
  73. /////////////
  74. describe( 'load', function() {
  75. it( 'should not fail when trying to load 0 plugins (empty string)', function() {
  76. var PluginCollection = modules.plugincollection;
  77. var plugins = new PluginCollection( editor );
  78. return plugins.load( '' )
  79. .then( function() {
  80. expect( plugins.length ).to.equal( 0 );
  81. } );
  82. } );
  83. it( 'should not fail when trying to load 0 plugins (undefined)', function() {
  84. var PluginCollection = modules.plugincollection;
  85. var plugins = new PluginCollection( editor );
  86. return plugins.load()
  87. .then( function() {
  88. expect( plugins.length ).to.equal( 0 );
  89. } );
  90. } );
  91. it( 'should add collection items for loaded plugins', function() {
  92. var PluginCollection = modules.plugincollection;
  93. var plugins = new PluginCollection( editor );
  94. return plugins.load( 'A,B' )
  95. .then( function() {
  96. expect( plugins.length ).to.equal( 2 );
  97. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  98. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  99. } );
  100. } );
  101. it( 'should load dependency plugins', function() {
  102. var PluginCollection = modules.plugincollection;
  103. var plugins = new PluginCollection( editor );
  104. return plugins.load( 'A,C' )
  105. .then( function() {
  106. expect( plugins.length ).to.equal( 3 );
  107. // The order must have dependencies first.
  108. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  109. expect( plugins.get( 1 ).name ).to.equal( 'B' );
  110. expect( plugins.get( 2 ).name ).to.equal( 'C' );
  111. } );
  112. } );
  113. it( 'should be ok when dependencies are loaded first', function() {
  114. var PluginCollection = modules.plugincollection;
  115. var plugins = new PluginCollection( editor );
  116. return plugins.load( 'A,B,C' )
  117. .then( function() {
  118. expect( plugins.length ).to.equal( 3 );
  119. // The order must have dependencies first.
  120. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  121. expect( plugins.get( 1 ).name ).to.equal( 'B' );
  122. expect( plugins.get( 2 ).name ).to.equal( 'C' );
  123. } );
  124. } );
  125. it( 'should load deep dependency plugins', function() {
  126. var PluginCollection = modules.plugincollection;
  127. var plugins = new PluginCollection( editor );
  128. return plugins.load( 'D' )
  129. .then( function() {
  130. expect( plugins.length ).to.equal( 4 );
  131. // The order must have dependencies first.
  132. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  133. expect( plugins.get( 1 ).name ).to.equal( 'B' );
  134. expect( plugins.get( 2 ).name ).to.equal( 'C' );
  135. expect( plugins.get( 3 ).name ).to.equal( 'D' );
  136. } );
  137. } );
  138. it( 'should handle cross dependency plugins', function() {
  139. var PluginCollection = modules.plugincollection;
  140. var plugins = new PluginCollection( editor );
  141. return plugins.load( 'A,E' )
  142. .then( function() {
  143. expect( plugins.length ).to.equal( 3 );
  144. // The order must have dependencies first.
  145. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  146. expect( plugins.get( 1 ).name ).to.equal( 'F' );
  147. expect( plugins.get( 2 ).name ).to.equal( 'E' );
  148. } );
  149. } );
  150. it( 'should set the `editor` property on loaded plugins', function() {
  151. var PluginCollection = modules.plugincollection;
  152. var plugins = new PluginCollection( editor );
  153. return plugins.load( 'A,B' )
  154. .then( function() {
  155. expect( plugins.get( 0 ).editor ).to.equal( editor );
  156. expect( plugins.get( 1 ).editor ).to.equal( editor );
  157. } );
  158. } );
  159. it( 'should set the `path` property on loaded plugins', function() {
  160. var PluginCollection = modules.plugincollection;
  161. var plugins = new PluginCollection( editor );
  162. return plugins.load( 'A,B' )
  163. .then( function() {
  164. expect( plugins.get( 'A' ).path ).to.equal( CKEDITOR.getPluginPath( 'A' ) );
  165. expect( plugins.get( 'B' ).path ).to.equal( CKEDITOR.getPluginPath( 'B' ) );
  166. } );
  167. } );
  168. it( 'should set the `deps` property on loaded plugins', function() {
  169. var PluginCollection = modules.plugincollection;
  170. var plugins = new PluginCollection( editor );
  171. return plugins.load( 'A,D' )
  172. .then( function() {
  173. expect( plugins.get( 'A' ).deps ).to.deep.equal( [] );
  174. expect( plugins.get( 'B' ).deps ).to.deep.equal( [] );
  175. expect( plugins.get( 'C' ).deps ).to.deep.equal( [ 'B' ] );
  176. expect( plugins.get( 'D' ).deps ).to.deep.equal( [ 'A', 'C' ] );
  177. } );
  178. } );
  179. it( 'should reject on invalid plugin names (forward require.js loading error)', function() {
  180. var PluginCollection = modules.plugincollection;
  181. var log = modules.log;
  182. var logSpy = sandbox.stub( log, 'error' );
  183. var plugins = new PluginCollection( editor );
  184. return plugins.load( 'A,BAD,B' )
  185. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  186. .then( function() {
  187. throw new Error( 'Test error: this promise should not be resolved successfully' );
  188. } )
  189. .catch( function( err ) {
  190. expect( err ).to.be.an.instanceof( Error );
  191. // Make sure it's the Require.JS error, not the one thrown above.
  192. expect( err.message ).to.match( /^Script error for:/ );
  193. sinon.assert.calledOnce( logSpy );
  194. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  195. } );
  196. } );
  197. it( 'should reject on broken plugins (forward the error thrown in a plugin)', function() {
  198. var PluginCollection = modules.plugincollection;
  199. var log = modules.log;
  200. var logSpy = sandbox.stub( log, 'error' );
  201. var plugins = new PluginCollection( editor );
  202. return plugins.load( 'A,G,B' )
  203. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  204. .then( function() {
  205. throw new Error( 'Test error: this promise should not be resolved successfully' );
  206. } )
  207. .catch( function( err ) {
  208. expect( err ).to.be.an.instanceof( TestError );
  209. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  210. sinon.assert.calledOnce( logSpy );
  211. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  212. } );
  213. } );
  214. it( 'should load `deps` which are not plugins', function() {
  215. var PluginCollection = modules.plugincollection;
  216. var plugins = new PluginCollection( editor );
  217. expect( spies ).to.be.empty;
  218. return plugins.load( 'H' )
  219. .then( function() {
  220. expect( plugins.get( 'H' ).deps ).to.deep.equal( [ 'H/a' ] );
  221. // Non–plugin dependencies should be loaded (spy exists)...
  222. expect( spies ).to.have.keys( [
  223. 'plugin!H/a', 'plugin!H/a/b', 'c'
  224. ] );
  225. // ...but not be executed (called == false)...
  226. expect( spies[ 'plugin!H/a' ].called ).to.be.false;
  227. expect( spies[ 'plugin!H/a/b' ].called ).to.be.false;
  228. expect( spies.c.called ).to.be.false;
  229. expect( plugins.length ).to.be.equal( 1 );
  230. } );
  231. } );
  232. it( 'should load instances of Plugin only', function() {
  233. var PluginCollection = modules.plugincollection;
  234. var plugins = new PluginCollection( editor );
  235. return plugins.load( 'I' )
  236. .then( () => {
  237. throw new Error( 'Test error: this promise should not be resolved successfully' );
  238. } ).catch( err => {
  239. expect( err.name ).to.be.equal( 'CKEditorError' );
  240. expect( err.message ).to.match( /^plugincollection-instance:/ );
  241. } );
  242. } );
  243. it( 'should cancel loading module which looks like a plugin but is a normal module', function() {
  244. var PluginCollection = modules.plugincollection;
  245. var plugins = new PluginCollection( editor );
  246. return plugins.load( 'J' )
  247. .then( () => {
  248. throw new Error( 'Test error: this promise should not be resolved successfully' );
  249. } ).catch( () => {
  250. // Path would be set if code execution wasn't stopped when we rejected the promise
  251. // (based on a real mistake we've made).
  252. expect( spies.jSpy.path ).to.be.undefined;
  253. } );
  254. } );
  255. } );
  256. describe( 'add', function() {
  257. it( 'should add plugins to the collection', function() {
  258. var PluginCollection = modules.plugincollection;
  259. var plugins = new PluginCollection( editor );
  260. var pluginA = new PluginA();
  261. var pluginB = new PluginB();
  262. // `add()` requires the `name` property to the defined.
  263. pluginA.name = 'A';
  264. pluginB.name = 'B';
  265. plugins.add( pluginA );
  266. plugins.add( pluginB );
  267. expect( plugins.length ).to.equal( 2 );
  268. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  269. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  270. } );
  271. it( 'should do nothing if the plugin is already loaded', function() {
  272. var PluginCollection = modules.plugincollection;
  273. var plugins = new PluginCollection( editor );
  274. return plugins.load( 'A,B' )
  275. .then( function() {
  276. // Check length before `add()`.
  277. expect( plugins.length ).to.equal( 2 );
  278. var pluginA = new PluginA();
  279. pluginA.name = 'A';
  280. plugins.add( pluginA );
  281. // Length should not change after `add()`.
  282. expect( plugins.length ).to.equal( 2 );
  283. } );
  284. } );
  285. } );
  286. describe( 'get', function() {
  287. it( 'should get a plugin by name', function() {
  288. var PluginCollection = modules.plugincollection;
  289. var plugins = new PluginCollection( editor );
  290. return plugins.load( 'A,B' )
  291. .then( function() {
  292. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  293. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  294. } );
  295. } );
  296. it( 'should return undefined for non existing plugin', function() {
  297. var PluginCollection = modules.plugincollection;
  298. var plugins = new PluginCollection( editor );
  299. return plugins.load( 'A,B' )
  300. .then( function() {
  301. expect( plugins.get( 'C' ) ).to.be.an( 'undefined' );
  302. } );
  303. } );
  304. } );