editor.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/ckeditor5/_utils/module.js';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import Editor from '/ckeditor5/editor.js';
  10. import EditorConfig from '/ckeditor5/editorconfig.js';
  11. import PluginCollection from '/ckeditor5/plugincollection.js';
  12. import EditableCollection from '/ckeditor5/editablecollection.js';
  13. import Plugin from '/ckeditor5/plugin.js';
  14. import Command from '/ckeditor5/command/command.js';
  15. import Locale from '/ckeditor5/utils/locale.js';
  16. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  17. import Document from '/ckeditor5/engine/treemodel/document.js';
  18. const pluginClasses = {};
  19. before( () => {
  20. // Define fake plugins to be used in tests.
  21. testUtils.defineEditorCreatorMock( 'test', {
  22. init: sinon.spy().named( 'creator-test' )
  23. } );
  24. pluginDefinition( 'A/A' );
  25. pluginDefinition( 'B/B' );
  26. pluginDefinition( 'C/C', [ 'B/B' ] );
  27. pluginDefinition( 'D/D', [ 'C/C' ] );
  28. } );
  29. ///////////////////
  30. describe( 'Editor', () => {
  31. describe( 'constructor', () => {
  32. it( 'should create a new editor instance', () => {
  33. const editor = new Editor();
  34. expect( editor ).to.have.property( 'elements', null );
  35. expect( editor.config ).to.be.an.instanceof( EditorConfig );
  36. expect( editor.editables ).to.be.an.instanceof( EditableCollection );
  37. expect( editor.commands ).to.be.an.instanceof( Map );
  38. expect( editor.plugins ).to.be.an.instanceof( PluginCollection );
  39. expect( getPlugins( editor ) ).to.be.empty;
  40. } );
  41. } );
  42. describe( 'config', () => {
  43. it( 'should be an instance of EditorConfig', () => {
  44. const editor = new Editor();
  45. expect( editor.config ).to.be.an.instanceof( EditorConfig );
  46. } );
  47. } );
  48. describe( 'locale', () => {
  49. it( 'is instantiated and t() is exposed', () => {
  50. const editor = new Editor();
  51. expect( editor.locale ).to.be.instanceof( Locale );
  52. expect( editor.t ).to.equal( editor.locale.t );
  53. } );
  54. it( 'is configured with the config.lang', () => {
  55. const editor = new Editor( null, { lang: 'pl' } );
  56. expect( editor.locale.lang ).to.equal( 'pl' );
  57. } );
  58. } );
  59. describe( 'plugins', () => {
  60. it( 'should be empty on new editor', () => {
  61. const editor = new Editor();
  62. expect( getPlugins( editor ) ).to.be.empty;
  63. } );
  64. } );
  65. describe( 'init', () => {
  66. it( 'should return a promise that resolves properly', () => {
  67. const editor = new Editor( null, {
  68. creator: 'creator-test'
  69. } );
  70. let promise = editor.init();
  71. expect( promise ).to.be.an.instanceof( Promise );
  72. return promise;
  73. } );
  74. it( 'should load features and creator', () => {
  75. const editor = new Editor( null, {
  76. features: [ 'A', 'B' ],
  77. creator: 'creator-test'
  78. } );
  79. expect( getPlugins( editor ) ).to.be.empty;
  80. return editor.init().then( () => {
  81. expect( getPlugins( editor ).length ).to.equal( 3 );
  82. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  83. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  84. expect( editor.plugins.get( 'creator-test' ) ).to.be.an.instanceof( Plugin );
  85. } );
  86. } );
  87. it( 'should load features passed as a string', () => {
  88. const editor = new Editor( null, {
  89. features: 'A,B',
  90. creator: 'creator-test'
  91. } );
  92. expect( getPlugins( editor ) ).to.be.empty;
  93. return editor.init().then( () => {
  94. expect( getPlugins( editor ).length ).to.equal( 3 );
  95. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  96. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  97. } );
  98. } );
  99. it( 'should initialize plugins in the right order', () => {
  100. const editor = new Editor( null, {
  101. features: [ 'A', 'D' ],
  102. creator: 'creator-test'
  103. } );
  104. return editor.init().then( () => {
  105. sinon.assert.callOrder(
  106. editor.plugins.get( 'creator-test' ).init,
  107. editor.plugins.get( pluginClasses[ 'A/A' ] ).init,
  108. editor.plugins.get( pluginClasses[ 'B/B' ] ).init,
  109. editor.plugins.get( pluginClasses[ 'C/C' ] ).init,
  110. editor.plugins.get( pluginClasses[ 'D/D' ] ).init
  111. );
  112. } );
  113. } );
  114. it( 'should initialize plugins in the right order, waiting for asynchronous ones', () => {
  115. class PluginAsync extends Plugin {}
  116. const asyncSpy = sinon.spy().named( 'async-call-spy' );
  117. // Synchronous plugin that depends on an asynchronous one.
  118. pluginDefinition( 'sync/sync', [ 'async/async' ] );
  119. moduleUtils.define( 'async/async', () => {
  120. PluginAsync.prototype.init = sinon.spy( () => {
  121. return new Promise( ( resolve ) => {
  122. setTimeout( () => {
  123. asyncSpy();
  124. resolve();
  125. }, 0 );
  126. } );
  127. } );
  128. return PluginAsync;
  129. } );
  130. const editor = new Editor( null, {
  131. features: [ 'A', 'sync' ],
  132. creator: 'creator-test'
  133. } );
  134. return editor.init().then( () => {
  135. sinon.assert.callOrder(
  136. editor.plugins.get( 'creator-test' ).init,
  137. editor.plugins.get( pluginClasses[ 'A/A' ] ).init,
  138. editor.plugins.get( PluginAsync ).init,
  139. // This one is called with delay by the async init.
  140. asyncSpy,
  141. editor.plugins.get( pluginClasses[ 'sync/sync' ] ).init
  142. );
  143. } );
  144. } );
  145. } );
  146. describe( 'destroy', () => {
  147. it( 'should fire "destroy"', () => {
  148. const editor = new Editor();
  149. let spy = sinon.spy();
  150. editor.on( 'destroy', spy );
  151. return editor.destroy().then( () => {
  152. expect( spy.calledOnce ).to.be.true;
  153. } );
  154. } );
  155. // Note: Tests for destroying creators are in creator/creator.js.
  156. // When destroying creator will be generalized to destroying plugins,
  157. // move that code here.
  158. } );
  159. describe( 'execute', () => {
  160. it( 'should execute specified command', () => {
  161. const editor = new Editor();
  162. let command = new Command( editor );
  163. sinon.spy( command, '_execute' );
  164. editor.commands.set( 'commandName', command );
  165. editor.execute( 'commandName' );
  166. expect( command._execute.calledOnce ).to.be.true;
  167. } );
  168. it( 'should throw an error if specified command has not been added', () => {
  169. const editor = new Editor();
  170. expect( () => {
  171. editor.execute( 'command' );
  172. } ).to.throw( CKEditorError, /^editor-command-not-found:/ );
  173. } );
  174. } );
  175. describe( 'setData', () => {
  176. let editor;
  177. beforeEach( () => {
  178. editor = new Editor();
  179. editor.document = new Document();
  180. editor.data = {
  181. set: sinon.spy()
  182. };
  183. } );
  184. it( 'should set data of the first root', () => {
  185. editor.document.createRoot( 'firstRoot', 'div' );
  186. editor.setData( 'foo' );
  187. expect( editor.data.set.calledOnce ).to.be.true;
  188. expect( editor.data.set.calledWithExactly( 'firstRoot', 'foo' ) ).to.be.true;
  189. } );
  190. it( 'should set data of the specified root', () => {
  191. editor.setData( 'foo', 'someRoot' );
  192. expect( editor.data.set.calledOnce ).to.be.true;
  193. expect( editor.data.set.calledWithExactly( 'someRoot', 'foo' ) ).to.be.true;
  194. } );
  195. it( 'should throw when no roots', () => {
  196. expect( () => {
  197. editor.setData( 'foo' );
  198. } ).to.throw( CKEditorError, /^editor-no-root-editables:/ );
  199. } );
  200. it( 'should throw when more than one root and no root name given', () => {
  201. editor.document.createRoot( 'firstRoot', 'div' );
  202. editor.document.createRoot( 'secondRoot', 'div' );
  203. expect( () => {
  204. editor.setData( 'foo' );
  205. } ).to.throw( CKEditorError, /^editor-root-editable-name-missing:/ );
  206. } );
  207. } );
  208. describe( 'getData', () => {
  209. let editor;
  210. beforeEach( () => {
  211. editor = new Editor();
  212. editor.document = new Document();
  213. editor.data = {
  214. get( rootName ) {
  215. return `data for ${ rootName }`;
  216. }
  217. };
  218. } );
  219. it( 'should get data from the first root', () => {
  220. editor.document.createRoot( 'firstRoot', 'div' );
  221. expect( editor.getData() ).to.equal( 'data for firstRoot' );
  222. } );
  223. it( 'should get data from the specified root', () => {
  224. expect( editor.getData( 'someRoot' ) ).to.equal( 'data for someRoot' );
  225. } );
  226. it( 'should throw when no roots', () => {
  227. expect( () => {
  228. editor.getData();
  229. } ).to.throw( CKEditorError, /^editor-no-root-editables:/ );
  230. } );
  231. it( 'should throw when more than one root and no root name given', () => {
  232. editor.document.createRoot( 'firstRoot', 'div' );
  233. editor.document.createRoot( 'secondRoot', 'div' );
  234. expect( () => {
  235. editor.getData();
  236. } ).to.throw( CKEditorError, /^editor-root-editable-name-missing:/ );
  237. } );
  238. } );
  239. } );
  240. /**
  241. * @param {String} name Name of the plugin.
  242. * @param {String[]} deps Dependencies of the plugin (only other plugins).
  243. */
  244. function pluginDefinition( name, deps ) {
  245. moduleUtils.define( name, deps || [], function() {
  246. class NewPlugin extends Plugin {}
  247. NewPlugin.prototype.init = sinon.spy().named( name );
  248. NewPlugin.requires = Array.from( arguments );
  249. pluginClasses[ name ] = NewPlugin;
  250. return NewPlugin;
  251. } );
  252. }
  253. /**
  254. * Returns an array of loaded plugins.
  255. */
  256. function getPlugins( editor ) {
  257. const plugins = [];
  258. for ( let entry of editor.plugins ) {
  259. // Keep only plugins kept under their classes.
  260. if ( typeof entry[ 0 ] == 'function' ) {
  261. plugins.push( entry[ 1 ] );
  262. }
  263. }
  264. return plugins;
  265. }