editor.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: editor, browser-only */
  6. 'use strict';
  7. import moduleUtils from '/tests/ckeditor5/_utils/module.js';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import Editor from '/ckeditor5/editor.js';
  10. import Plugin from '/ckeditor5/plugin.js';
  11. import Config from '/ckeditor5/utils/config.js';
  12. import PluginCollection from '/ckeditor5/plugincollection.js';
  13. import EditableCollection from '/ckeditor5/editablecollection.js';
  14. const pluginClasses = {};
  15. before( () => {
  16. // Define fake plugins to be used in tests.
  17. testUtils.defineEditorCreatorMock( 'test', {
  18. init: sinon.spy().named( 'creator-test' )
  19. } );
  20. pluginDefinition( 'A/A' );
  21. pluginDefinition( 'B/B' );
  22. pluginDefinition( 'C/C', [ 'B/B' ] );
  23. pluginDefinition( 'D/D', [ 'C/C' ] );
  24. } );
  25. ///////////////////
  26. describe( 'Editor', () => {
  27. describe( 'constructor', () => {
  28. it( 'should create a new editor instance', () => {
  29. const editor = new Editor();
  30. expect( editor ).to.have.property( 'elements', null );
  31. expect( editor.config ).to.be.an.instanceof( Config );
  32. expect( editor.editables ).to.be.an.instanceof( EditableCollection );
  33. expect( editor.commands ).to.be.an.instanceof( Map );
  34. expect( editor.plugins ).to.be.an.instanceof( PluginCollection );
  35. expect( getPlugins( editor ) ).to.be.empty;
  36. } );
  37. } );
  38. describe( 'plugins', () => {
  39. it( 'should be empty on new editor', () => {
  40. const editor = new Editor();
  41. expect( getPlugins( editor ) ).to.be.empty;
  42. } );
  43. } );
  44. describe( 'firstElement', () => {
  45. it( 'should be set to first element', () => {
  46. const editor = new Editor( { foo: 'a', bar: 'b' } );
  47. expect( editor.firstElement ).to.equal( 'a' );
  48. } );
  49. it( 'should be set to null if there are no elements', () => {
  50. const editor = new Editor();
  51. expect( editor.firstElement ).to.be.null;
  52. } );
  53. } );
  54. describe( 'firstElementName', () => {
  55. it( 'should be set to first element name', () => {
  56. const editor = new Editor( { foo: 'a', bar: 'b' } );
  57. expect( editor.firstElementName ).to.equal( 'foo' );
  58. } );
  59. it( 'should be set to null if there are no elements', () => {
  60. const editor = new Editor();
  61. expect( editor.firstElementName ).to.be.null;
  62. } );
  63. } );
  64. describe( 'init', () => {
  65. it( 'should return a promise that resolves properly', () => {
  66. const editor = new Editor( null, {
  67. creator: 'creator-test'
  68. } );
  69. let promise = editor.init();
  70. expect( promise ).to.be.an.instanceof( Promise );
  71. return promise;
  72. } );
  73. it( 'should load features and creator', () => {
  74. const editor = new Editor( null, {
  75. features: [ 'A', 'B' ],
  76. creator: 'creator-test'
  77. } );
  78. expect( getPlugins( editor ) ).to.be.empty;
  79. return editor.init().then( () => {
  80. expect( getPlugins( editor ).length ).to.equal( 3 );
  81. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  82. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  83. expect( editor.plugins.get( 'creator-test' ) ).to.be.an.instanceof( Plugin );
  84. } );
  85. } );
  86. it( 'should load features passed as a string', () => {
  87. const editor = new Editor( null, {
  88. features: 'A,B',
  89. creator: 'creator-test'
  90. } );
  91. expect( getPlugins( editor ) ).to.be.empty;
  92. return editor.init().then( () => {
  93. expect( getPlugins( editor ).length ).to.equal( 3 );
  94. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  95. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  96. } );
  97. } );
  98. it( 'should initialize plugins in the right order', () => {
  99. const editor = new Editor( null, {
  100. features: [ 'A', 'D' ],
  101. creator: 'creator-test'
  102. } );
  103. return editor.init().then( () => {
  104. sinon.assert.callOrder(
  105. editor.plugins.get( 'creator-test' ).init,
  106. editor.plugins.get( pluginClasses[ 'A/A' ] ).init,
  107. editor.plugins.get( pluginClasses[ 'B/B' ] ).init,
  108. editor.plugins.get( pluginClasses[ 'C/C' ] ).init,
  109. editor.plugins.get( pluginClasses[ 'D/D' ] ).init
  110. );
  111. } );
  112. } );
  113. it( 'should initialize plugins in the right order, waiting for asynchronous ones', () => {
  114. class PluginAsync extends Plugin {}
  115. const asyncSpy = sinon.spy().named( 'async-call-spy' );
  116. // Synchronous plugin that depends on an asynchronous one.
  117. pluginDefinition( 'sync/sync', [ 'async/async' ] );
  118. moduleUtils.define( 'async/async', () => {
  119. PluginAsync.prototype.init = sinon.spy( () => {
  120. return new Promise( ( resolve ) => {
  121. setTimeout( () => {
  122. asyncSpy();
  123. resolve();
  124. }, 0 );
  125. } );
  126. } );
  127. return PluginAsync;
  128. } );
  129. const editor = new Editor( null, {
  130. features: [ 'A', 'sync' ],
  131. creator: 'creator-test'
  132. } );
  133. return editor.init().then( () => {
  134. sinon.assert.callOrder(
  135. editor.plugins.get( 'creator-test' ).init,
  136. editor.plugins.get( pluginClasses[ 'A/A' ] ).init,
  137. editor.plugins.get( PluginAsync ).init,
  138. // This one is called with delay by the async init.
  139. asyncSpy,
  140. editor.plugins.get( pluginClasses[ 'sync/sync' ] ).init
  141. );
  142. } );
  143. } );
  144. } );
  145. describe( 'plugins', () => {
  146. it( 'should be empty on new editor', () => {
  147. const editor = new Editor();
  148. expect( getPlugins( editor ) ).to.be.empty;
  149. } );
  150. } );
  151. } );
  152. /**
  153. * @param {String} name Name of the plugin.
  154. * @param {String[]} deps Dependencies of the plugin (only other plugins).
  155. */
  156. function pluginDefinition( name, deps ) {
  157. moduleUtils.define( name, deps || [], function() {
  158. class NewPlugin extends Plugin {}
  159. NewPlugin.prototype.init = sinon.spy().named( name );
  160. NewPlugin.requires = Array.from( arguments );
  161. pluginClasses[ name ] = NewPlugin;
  162. return NewPlugin;
  163. } );
  164. }
  165. /**
  166. * Returns an array of loaded plugins.
  167. */
  168. function getPlugins( editor ) {
  169. const plugins = [];
  170. for ( let entry of editor.plugins ) {
  171. // Keep only plugins kept under their classes.
  172. if ( typeof entry[ 0 ] == 'function' ) {
  173. plugins.push( entry[ 1 ] );
  174. }
  175. }
  176. return plugins;
  177. }