plugincollection.js 10 KB

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