plugincollection.js 11 KB

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