8
0

plugincollection.js 10 KB

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