plugincollection.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals describe, it, expect, before, document */
  6. 'use strict';
  7. var modules = bender.amd.require( 'plugincollection', 'plugin', 'editor' );
  8. var editor;
  9. var PluginA, PluginB;
  10. before( function() {
  11. var Editor = modules.editor;
  12. var Plugin = modules.plugin;
  13. PluginA = Plugin.extend();
  14. PluginB = Plugin.extend();
  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() {
  25. return modules.plugin;
  26. } );
  27. CKEDITOR.define( 'plugin!D', [ 'plugin', 'plugin!A', 'plugin!C' ], function() {
  28. return modules.plugin;
  29. } );
  30. CKEDITOR.define( 'plugin!E', [ 'plugin', 'plugin!F' ], function() {
  31. return modules.plugin;
  32. } );
  33. CKEDITOR.define( 'plugin!F', [ 'plugin', 'plugin!E' ], function() {
  34. return modules.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 error for invalid plugins', function() {
  127. var PluginCollection = modules.plugincollection;
  128. var plugins = new PluginCollection( editor );
  129. return plugins.load( 'A,BAD,B' )
  130. .then( function() {
  131. throw new Error( 'Test error: this promise should not be resolved successfully' );
  132. } )
  133. .catch( function( err ) {
  134. expect( err ).to.be.an.instanceof( Error );
  135. expect( err.name ).to.equal( 'CKEditor Error' );
  136. } );
  137. } );
  138. } );
  139. describe( 'add', function() {
  140. it( 'should add plugins to the collection', function() {
  141. var PluginCollection = modules.plugincollection;
  142. var plugins = new PluginCollection( editor );
  143. var pluginA = new PluginA();
  144. var pluginB = new PluginB();
  145. // `add()` requires the `name` property to the defined.
  146. pluginA.name = 'A';
  147. pluginB.name = 'B';
  148. plugins.add( pluginA );
  149. plugins.add( pluginB );
  150. expect( plugins.length ).to.equal( 2 );
  151. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  152. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  153. } );
  154. it( 'should do nothing if the plugin is already loaded', function() {
  155. var PluginCollection = modules.plugincollection;
  156. var plugins = new PluginCollection( editor );
  157. return plugins.load( 'A,B' )
  158. .then( function() {
  159. // Check length before `add()`.
  160. expect( plugins.length ).to.equal( 2 );
  161. var pluginA = new PluginA();
  162. pluginA.name = 'A';
  163. plugins.add( pluginA );
  164. // Length should not change after `add()`.
  165. expect( plugins.length ).to.equal( 2 );
  166. } );
  167. } );
  168. } );
  169. describe( 'get', function() {
  170. it( 'should get plugin by name', function() {
  171. var PluginCollection = modules.plugincollection;
  172. var plugins = new PluginCollection( editor );
  173. return plugins.load( 'A,B' )
  174. .then( function() {
  175. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  176. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  177. } );
  178. } );
  179. it( 'should return undefined for non existing plugin', function() {
  180. var PluginCollection = modules.plugincollection;
  181. var plugins = new PluginCollection( editor );
  182. return plugins.load( 'A,B' )
  183. .then( function() {
  184. expect( plugins.get( 'C' ) ).to.be.an( 'undefined' );
  185. } );
  186. } );
  187. } );