plugincollection.js 11 KB

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