8
0

plugincollection.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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', [ 'plugin' ], function() {
  19. return PluginA;
  20. } );
  21. CKEDITOR.define( 'plugin!B', [ 'plugin' ], 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 throw error for invalid plugins', function() {
  107. var PluginCollection = modules.plugincollection;
  108. var plugins = new PluginCollection( editor );
  109. return plugins.load( 'A,BAD,B' )
  110. .then( function() {
  111. throw new Error( 'Test error: this promise should not be resolved successfully' );
  112. } )
  113. .catch( function( err ) {
  114. expect( err ).to.be.an.instanceof( Error );
  115. expect( err.name ).to.equal( 'CKEditor Error' );
  116. } );
  117. } );
  118. } );
  119. describe( 'add', function() {
  120. it( 'should add plugins to the collection', function() {
  121. var PluginCollection = modules.plugincollection;
  122. var plugins = new PluginCollection( editor );
  123. var pluginA = new PluginA();
  124. var pluginB = new PluginB();
  125. // `add()` requires the `name` property to the defined.
  126. pluginA.name = 'A';
  127. pluginB.name = 'B';
  128. plugins.add( pluginA );
  129. plugins.add( pluginB );
  130. expect( plugins.length ).to.equal( 2 );
  131. expect( plugins.get( 0 ) ).to.be.an.instanceof( PluginA );
  132. expect( plugins.get( 1 ) ).to.be.an.instanceof( PluginB );
  133. } );
  134. it( 'should do nothing if the plugin is already loaded', function() {
  135. var PluginCollection = modules.plugincollection;
  136. var plugins = new PluginCollection( editor );
  137. return plugins.load( 'A,B' )
  138. .then( function() {
  139. // Check length before `add()`.
  140. expect( plugins.length ).to.equal( 2 );
  141. var pluginA = new PluginA();
  142. pluginA.name = 'A';
  143. plugins.add( pluginA );
  144. // Length should not change after `add()`.
  145. expect( plugins.length ).to.equal( 2 );
  146. } );
  147. } );
  148. } );
  149. describe( 'get', function() {
  150. it( 'should get plugin by name', function() {
  151. var PluginCollection = modules.plugincollection;
  152. var plugins = new PluginCollection( editor );
  153. return plugins.load( 'A,B' )
  154. .then( function() {
  155. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  156. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  157. } );
  158. } );
  159. it( 'should return undefined for non existing plugin', function() {
  160. var PluginCollection = modules.plugincollection;
  161. var plugins = new PluginCollection( editor );
  162. return plugins.load( 'A,B' )
  163. .then( function() {
  164. expect( plugins.get( 'C' ) ).to.be.an( 'undefined' );
  165. } );
  166. } );
  167. } );