editor.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 PluginCollection from '/ckeditor5/plugincollection.js';
  12. import EditableCollection from '/ckeditor5/editablecollection.js';
  13. import EditorConfig from '/ckeditor5/editorconfig.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( EditorConfig );
  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( 'init', () => {
  39. it( 'should return a promise that resolves properly', () => {
  40. const editor = new Editor( null, {
  41. creator: 'creator-test'
  42. } );
  43. let promise = editor.init();
  44. expect( promise ).to.be.an.instanceof( Promise );
  45. return promise;
  46. } );
  47. it( 'should load features and creator', () => {
  48. const editor = new Editor( null, {
  49. features: [ 'A', 'B' ],
  50. creator: 'creator-test'
  51. } );
  52. expect( getPlugins( editor ) ).to.be.empty;
  53. return editor.init().then( () => {
  54. expect( getPlugins( editor ).length ).to.equal( 3 );
  55. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  56. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  57. expect( editor.plugins.get( 'creator-test' ) ).to.be.an.instanceof( Plugin );
  58. } );
  59. } );
  60. it( 'should load features passed as a string', () => {
  61. const editor = new Editor( null, {
  62. features: 'A,B',
  63. creator: 'creator-test'
  64. } );
  65. expect( getPlugins( editor ) ).to.be.empty;
  66. return editor.init().then( () => {
  67. expect( getPlugins( editor ).length ).to.equal( 3 );
  68. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  69. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  70. } );
  71. } );
  72. it( 'should initialize plugins in the right order', () => {
  73. const editor = new Editor( null, {
  74. features: [ 'A', 'D' ],
  75. creator: 'creator-test'
  76. } );
  77. return editor.init().then( () => {
  78. sinon.assert.callOrder(
  79. editor.plugins.get( 'creator-test' ).init,
  80. editor.plugins.get( pluginClasses[ 'A/A' ] ).init,
  81. editor.plugins.get( pluginClasses[ 'B/B' ] ).init,
  82. editor.plugins.get( pluginClasses[ 'C/C' ] ).init,
  83. editor.plugins.get( pluginClasses[ 'D/D' ] ).init
  84. );
  85. } );
  86. } );
  87. it( 'should initialize plugins in the right order, waiting for asynchronous ones', () => {
  88. class PluginAsync extends Plugin {}
  89. const asyncSpy = sinon.spy().named( 'async-call-spy' );
  90. // Synchronous plugin that depends on an asynchronous one.
  91. pluginDefinition( 'sync/sync', [ 'async/async' ] );
  92. moduleUtils.define( 'async/async', () => {
  93. PluginAsync.prototype.init = sinon.spy( () => {
  94. return new Promise( ( resolve ) => {
  95. setTimeout( () => {
  96. asyncSpy();
  97. resolve();
  98. }, 0 );
  99. } );
  100. } );
  101. return PluginAsync;
  102. } );
  103. const editor = new Editor( null, {
  104. features: [ 'A', 'sync' ],
  105. creator: 'creator-test'
  106. } );
  107. return editor.init().then( () => {
  108. sinon.assert.callOrder(
  109. editor.plugins.get( 'creator-test' ).init,
  110. editor.plugins.get( pluginClasses[ 'A/A' ] ).init,
  111. editor.plugins.get( PluginAsync ).init,
  112. // This one is called with delay by the async init.
  113. asyncSpy,
  114. editor.plugins.get( pluginClasses[ 'sync/sync' ] ).init
  115. );
  116. } );
  117. } );
  118. } );
  119. describe( 'plugins', () => {
  120. it( 'should be empty on new editor', () => {
  121. const editor = new Editor();
  122. expect( getPlugins( editor ) ).to.be.empty;
  123. } );
  124. } );
  125. } );
  126. /**
  127. * @param {String} name Name of the plugin.
  128. * @param {String[]} deps Dependencies of the plugin (only other plugins).
  129. */
  130. function pluginDefinition( name, deps ) {
  131. moduleUtils.define( name, deps || [], function() {
  132. class NewPlugin extends Plugin {}
  133. NewPlugin.prototype.init = sinon.spy().named( name );
  134. NewPlugin.requires = Array.from( arguments );
  135. pluginClasses[ name ] = NewPlugin;
  136. return NewPlugin;
  137. } );
  138. }
  139. /**
  140. * Returns an array of loaded plugins.
  141. */
  142. function getPlugins( editor ) {
  143. const plugins = [];
  144. for ( let entry of editor.plugins ) {
  145. // Keep only plugins kept under their classes.
  146. if ( typeof entry[ 0 ] == 'function' ) {
  147. plugins.push( entry[ 1 ] );
  148. }
  149. }
  150. return plugins;
  151. }