plugincollection.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. /////////////
  49. describe( 'load', function() {
  50. it( 'should add collection items for loaded plugins', function() {
  51. var PluginCollection = modules.plugincollection;
  52. var plugins = new PluginCollection( editor );
  53. return plugins.load( 'A,B' )
  54. .then( function() {
  55. expect( plugins.length ).to.equal( 2 );
  56. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  57. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  58. } );
  59. } );
  60. it( 'should load dependency plugins', function() {
  61. var PluginCollection = modules.plugincollection;
  62. var plugins = new PluginCollection( editor );
  63. return plugins.load( 'A,C' )
  64. .then( function() {
  65. expect( plugins.length ).to.equal( 3 );
  66. // The order must have dependencies first.
  67. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  68. expect( plugins.get( 1 ).name ).to.equal( 'B' );
  69. expect( plugins.get( 2 ).name ).to.equal( 'C' );
  70. } );
  71. } );
  72. it( 'should be ok when dependencies are loaded first', function() {
  73. var PluginCollection = modules.plugincollection;
  74. var plugins = new PluginCollection( editor );
  75. return plugins.load( 'A,B,C' )
  76. .then( function() {
  77. expect( plugins.length ).to.equal( 3 );
  78. // The order must have dependencies first.
  79. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  80. expect( plugins.get( 1 ).name ).to.equal( 'B' );
  81. expect( plugins.get( 2 ).name ).to.equal( 'C' );
  82. } );
  83. } );
  84. it( 'should load deep dependency plugins', function() {
  85. var PluginCollection = modules.plugincollection;
  86. var plugins = new PluginCollection( editor );
  87. return plugins.load( 'D' )
  88. .then( function() {
  89. expect( plugins.length ).to.equal( 4 );
  90. // The order must have dependencies first.
  91. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  92. expect( plugins.get( 1 ).name ).to.equal( 'B' );
  93. expect( plugins.get( 2 ).name ).to.equal( 'C' );
  94. expect( plugins.get( 3 ).name ).to.equal( 'D' );
  95. } );
  96. } );
  97. it( 'should handle cross dependency plugins', function() {
  98. var PluginCollection = modules.plugincollection;
  99. var plugins = new PluginCollection( editor );
  100. return plugins.load( 'A,E' )
  101. .then( function() {
  102. expect( plugins.length ).to.equal( 3 );
  103. // The order must have dependencies first.
  104. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  105. expect( plugins.get( 1 ).name ).to.equal( 'F' );
  106. expect( plugins.get( 2 ).name ).to.equal( 'E' );
  107. } );
  108. } );
  109. it( 'should set the `editor` property on loaded plugins', function() {
  110. var PluginCollection = modules.plugincollection;
  111. var plugins = new PluginCollection( editor );
  112. return plugins.load( 'A,B' )
  113. .then( function() {
  114. expect( plugins.get( 0 ).editor ).to.equal( editor );
  115. expect( plugins.get( 1 ).editor ).to.equal( editor );
  116. } );
  117. } );
  118. it( 'should set the `path` property on loaded plugins', function() {
  119. var PluginCollection = modules.plugincollection;
  120. var plugins = new PluginCollection( editor );
  121. return plugins.load( 'A,B' )
  122. .then( function() {
  123. expect( plugins.get( 'A' ).path ).to.equal( CKEDITOR.getPluginPath( 'A' ) );
  124. expect( plugins.get( 'B' ).path ).to.equal( CKEDITOR.getPluginPath( 'B' ) );
  125. } );
  126. } );
  127. it( 'should set the `deps` property on loaded plugins', function() {
  128. var PluginCollection = modules.plugincollection;
  129. var plugins = new PluginCollection( editor );
  130. return plugins.load( 'A,D' )
  131. .then( function() {
  132. expect( plugins.get( 'A' ).deps ).to.deep.equal( [] );
  133. expect( plugins.get( 'B' ).deps ).to.deep.equal( [] );
  134. expect( plugins.get( 'C' ).deps ).to.deep.equal( [ 'B' ] );
  135. expect( plugins.get( 'D' ).deps ).to.deep.equal( [ 'A', 'C' ] );
  136. } );
  137. } );
  138. it( 'should reject on invalid plugin names (forward require.js loading error)', function() {
  139. var PluginCollection = modules.plugincollection;
  140. var log = modules.log;
  141. var logSpy = sandbox.stub( log, 'error' );
  142. var plugins = new PluginCollection( editor );
  143. return plugins.load( 'A,BAD,B' )
  144. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  145. .then( function() {
  146. throw new Error( 'Test error: this promise should not be resolved successfully' );
  147. } )
  148. .catch( function( err ) {
  149. expect( err ).to.be.an.instanceof( Error );
  150. // Make sure it's the Require.JS error, not the one thrown above.
  151. expect( err.message ).to.match( /^Script error for:/ );
  152. sinon.assert.calledOnce( logSpy );
  153. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  154. } );
  155. } );
  156. it( 'should reject on broken plugins (forward the error thrown in a plugin)', function() {
  157. var PluginCollection = modules.plugincollection;
  158. var log = modules.log;
  159. var logSpy = sandbox.stub( log, 'error' );
  160. var plugins = new PluginCollection( editor );
  161. return plugins.load( 'A,G,B' )
  162. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  163. .then( function() {
  164. throw new Error( 'Test error: this promise should not be resolved successfully' );
  165. } )
  166. .catch( function( err ) {
  167. expect( err ).to.be.an.instanceof( TestError );
  168. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  169. sinon.assert.calledOnce( logSpy );
  170. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  171. } );
  172. } );
  173. } );
  174. describe( 'add', function() {
  175. it( 'should add plugins to the collection', function() {
  176. var PluginCollection = modules.plugincollection;
  177. var plugins = new PluginCollection( editor );
  178. var pluginA = new PluginA();
  179. var pluginB = new PluginB();
  180. // `add()` requires the `name` property to the defined.
  181. pluginA.name = 'A';
  182. pluginB.name = 'B';
  183. plugins.add( pluginA );
  184. plugins.add( pluginB );
  185. expect( plugins.length ).to.equal( 2 );
  186. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  187. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  188. } );
  189. it( 'should do nothing if the plugin is already loaded', function() {
  190. var PluginCollection = modules.plugincollection;
  191. var plugins = new PluginCollection( editor );
  192. return plugins.load( 'A,B' )
  193. .then( function() {
  194. // Check length before `add()`.
  195. expect( plugins.length ).to.equal( 2 );
  196. var pluginA = new PluginA();
  197. pluginA.name = 'A';
  198. plugins.add( pluginA );
  199. // Length should not change after `add()`.
  200. expect( plugins.length ).to.equal( 2 );
  201. } );
  202. } );
  203. } );
  204. describe( 'get', function() {
  205. it( 'should get a plugin by name', function() {
  206. var PluginCollection = modules.plugincollection;
  207. var plugins = new PluginCollection( editor );
  208. return plugins.load( 'A,B' )
  209. .then( function() {
  210. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  211. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  212. } );
  213. } );
  214. it( 'should return undefined for non existing plugin', function() {
  215. var PluginCollection = modules.plugincollection;
  216. var plugins = new PluginCollection( editor );
  217. return plugins.load( 'A,B' )
  218. .then( function() {
  219. expect( plugins.get( 'C' ) ).to.be.an( 'undefined' );
  220. } );
  221. } );
  222. } );