editor.js 5.8 KB

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