plugincollection.js 8.3 KB

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