8
0

plugincollection.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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', 'ckeditorerror' );
  8. var editor;
  9. var PluginA, PluginB;
  10. before( function() {
  11. var Editor = modules.editor;
  12. var Plugin = modules.plugin;
  13. PluginA = class extends Plugin {};
  14. PluginB = class extends Plugin {};
  15. editor = new Editor( document.body.appendChild( document.createElement( 'div' ) ) );
  16. } );
  17. // Create fake plugins that will be used on tests.
  18. CKEDITOR.define( 'plugin!A', function() {
  19. return PluginA;
  20. } );
  21. CKEDITOR.define( 'plugin!B', function() {
  22. return PluginB;
  23. } );
  24. CKEDITOR.define( 'plugin!C', [ 'plugin', 'plugin!B' ], function( Plugin ) {
  25. return class extends Plugin {};
  26. } );
  27. CKEDITOR.define( 'plugin!D', [ 'plugin', 'plugin!A', 'plugin!C' ], function( Plugin ) {
  28. return class extends Plugin {};
  29. } );
  30. CKEDITOR.define( 'plugin!E', [ 'plugin', 'plugin!F' ], function( Plugin ) {
  31. return class extends Plugin {};
  32. } );
  33. CKEDITOR.define( 'plugin!F', [ 'plugin', 'plugin!E' ], function( Plugin ) {
  34. return class extends Plugin {};
  35. } );
  36. /////////////
  37. describe( 'load', function() {
  38. it( 'should add collection items for loaded plugins', function() {
  39. var PluginCollection = modules.plugincollection;
  40. var plugins = new PluginCollection( editor );
  41. return plugins.load( 'A,B' )
  42. .then( function() {
  43. expect( plugins.length ).to.equal( 2 );
  44. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  45. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  46. } );
  47. } );
  48. it( 'should load dependency plugins', function() {
  49. var PluginCollection = modules.plugincollection;
  50. var plugins = new PluginCollection( editor );
  51. return plugins.load( 'A,C' )
  52. .then( function() {
  53. expect( plugins.length ).to.equal( 3 );
  54. // The order must have dependencies first.
  55. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  56. expect( plugins.get( 1 ).name ).to.equal( 'B' );
  57. expect( plugins.get( 2 ).name ).to.equal( 'C' );
  58. } );
  59. } );
  60. it( 'should be ok when dependencies are loaded first', function() {
  61. var PluginCollection = modules.plugincollection;
  62. var plugins = new PluginCollection( editor );
  63. return plugins.load( 'A,B,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 load deep dependency plugins', function() {
  73. var PluginCollection = modules.plugincollection;
  74. var plugins = new PluginCollection( editor );
  75. return plugins.load( 'D' )
  76. .then( function() {
  77. expect( plugins.length ).to.equal( 4 );
  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. expect( plugins.get( 3 ).name ).to.equal( 'D' );
  83. } );
  84. } );
  85. it( 'should handle cross dependency plugins', function() {
  86. var PluginCollection = modules.plugincollection;
  87. var plugins = new PluginCollection( editor );
  88. return plugins.load( 'A,E' )
  89. .then( function() {
  90. expect( plugins.length ).to.equal( 3 );
  91. // The order must have dependencies first.
  92. expect( plugins.get( 0 ).name ).to.equal( 'A' );
  93. expect( plugins.get( 1 ).name ).to.equal( 'F' );
  94. expect( plugins.get( 2 ).name ).to.equal( 'E' );
  95. } );
  96. } );
  97. it( 'should set the `editor` property on loaded plugins', function() {
  98. var PluginCollection = modules.plugincollection;
  99. var plugins = new PluginCollection( editor );
  100. return plugins.load( 'A,B' )
  101. .then( function() {
  102. expect( plugins.get( 0 ).editor ).to.equal( editor );
  103. expect( plugins.get( 1 ).editor ).to.equal( editor );
  104. } );
  105. } );
  106. it( 'should set the `path` property on loaded plugins', function() {
  107. var PluginCollection = modules.plugincollection;
  108. var plugins = new PluginCollection( editor );
  109. return plugins.load( 'A,B' )
  110. .then( function() {
  111. expect( plugins.get( 'A' ).path ).to.equal( CKEDITOR.getPluginPath( 'A' ) );
  112. expect( plugins.get( 'B' ).path ).to.equal( CKEDITOR.getPluginPath( 'B' ) );
  113. } );
  114. } );
  115. it( 'should set the `deps` property on loaded plugins', function() {
  116. var PluginCollection = modules.plugincollection;
  117. var plugins = new PluginCollection( editor );
  118. return plugins.load( 'A,D' )
  119. .then( function() {
  120. expect( plugins.get( 'A' ).deps ).to.deep.equal( [] );
  121. expect( plugins.get( 'B' ).deps ).to.deep.equal( [] );
  122. expect( plugins.get( 'C' ).deps ).to.deep.equal( [ 'B' ] );
  123. expect( plugins.get( 'D' ).deps ).to.deep.equal( [ 'A', 'C' ] );
  124. } );
  125. } );
  126. it( 'should throw an error for invalid plugins', function() {
  127. var PluginCollection = modules.plugincollection;
  128. var CKEditorError = modules.ckeditorerror;
  129. var plugins = new PluginCollection( editor );
  130. return plugins.load( 'A,BAD,B' )
  131. .then( function() {
  132. throw new Error( 'Test error: this promise should not be resolved successfully' );
  133. } )
  134. .catch( function( err ) {
  135. expect( err ).to.be.an.instanceof( CKEditorError );
  136. expect( err.data ).to.have.property( 'plugin', 'BAD' );
  137. } );
  138. } );
  139. } );
  140. describe( 'add', function() {
  141. it( 'should add plugins to the collection', function() {
  142. var PluginCollection = modules.plugincollection;
  143. var plugins = new PluginCollection( editor );
  144. var pluginA = new PluginA();
  145. var pluginB = new PluginB();
  146. // `add()` requires the `name` property to the defined.
  147. pluginA.name = 'A';
  148. pluginB.name = 'B';
  149. plugins.add( pluginA );
  150. plugins.add( pluginB );
  151. expect( plugins.length ).to.equal( 2 );
  152. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  153. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  154. } );
  155. it( 'should do nothing if the plugin is already loaded', function() {
  156. var PluginCollection = modules.plugincollection;
  157. var plugins = new PluginCollection( editor );
  158. return plugins.load( 'A,B' )
  159. .then( function() {
  160. // Check length before `add()`.
  161. expect( plugins.length ).to.equal( 2 );
  162. var pluginA = new PluginA();
  163. pluginA.name = 'A';
  164. plugins.add( pluginA );
  165. // Length should not change after `add()`.
  166. expect( plugins.length ).to.equal( 2 );
  167. } );
  168. } );
  169. } );
  170. describe( 'get', function() {
  171. it( 'should get a plugin by name', function() {
  172. var PluginCollection = modules.plugincollection;
  173. var plugins = new PluginCollection( editor );
  174. return plugins.load( 'A,B' )
  175. .then( function() {
  176. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  177. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  178. } );
  179. } );
  180. it( 'should return undefined for non existing plugin', function() {
  181. var PluginCollection = modules.plugincollection;
  182. var plugins = new PluginCollection( editor );
  183. return plugins.load( 'A,B' )
  184. .then( function() {
  185. expect( plugins.get( 'C' ) ).to.be.an( 'undefined' );
  186. } );
  187. } );
  188. } );