| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- 'use strict';
- import amdUtils from '/tests/_utils/amd.js';
- import coreTestUtils from '/tests/core/_utils/utils.js';
- import Editor from '/ckeditor5/core/editor.js';
- import EditorConfig from '/ckeditor5/core/editorconfig.js';
- import Plugin from '/ckeditor5/core/plugin.js';
- const pluginClasses = {};
- let element;
- before( () => {
- // Define fake plugins to be used in tests.
- coreTestUtils.defineEditorCreatorMock( 'test', {
- init: sinon.spy().named( 'creator-test' )
- } );
- pluginDefinition( 'A' );
- pluginDefinition( 'B' );
- pluginDefinition( 'C', [ 'B' ] );
- pluginDefinition( 'D', [ 'C' ] );
- } );
- beforeEach( () => {
- element = document.createElement( 'div' );
- document.body.appendChild( element );
- } );
- ///////////////////
- describe( 'constructor', () => {
- it( 'should create a new editor instance', () => {
- const editor = new Editor( element );
- expect( editor ).to.have.property( 'element' ).to.equal( element );
- } );
- } );
- describe( 'config', () => {
- it( 'should be an instance of EditorConfig', () => {
- const editor = new Editor( element );
- expect( editor.config ).to.be.an.instanceof( EditorConfig );
- } );
- } );
- describe( 'init', () => {
- it( 'should return a promise that resolves properly', () => {
- const editor = new Editor( element, {
- creator: 'creator-test'
- } );
- let promise = editor.init();
- expect( promise ).to.be.an.instanceof( Promise );
- return promise;
- } );
- it( 'should load features and creator', () => {
- const editor = new Editor( element, {
- features: [ 'A', 'B' ],
- creator: 'creator-test'
- } );
- expect( getPlugins( editor ) ).to.be.empty;
- return editor.init().then( () => {
- expect( getPlugins( editor ).length ).to.equal( 3 );
- expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
- expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
- expect( editor.plugins.get( 'creator-test' ) ).to.be.an.instanceof( Plugin );
- } );
- } );
- it( 'should load features passed as a string', () => {
- const editor = new Editor( element, {
- features: 'A,B',
- creator: 'creator-test'
- } );
- expect( getPlugins( editor ) ).to.be.empty;
- return editor.init().then( () => {
- expect( getPlugins( editor ).length ).to.equal( 3 );
- expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
- expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
- } );
- } );
- it( 'should initialize plugins in the right order', () => {
- const editor = new Editor( element, {
- features: [ 'A', 'D' ],
- creator: 'creator-test'
- } );
- return editor.init().then( () => {
- sinon.assert.callOrder(
- editor.plugins.get( 'creator-test' ).init,
- editor.plugins.get( pluginClasses.A ).init,
- editor.plugins.get( pluginClasses.B ).init,
- editor.plugins.get( pluginClasses.C ).init,
- editor.plugins.get( pluginClasses.D ).init
- );
- } );
- } );
- it( 'should initialize plugins in the right order, waiting for asynchronous ones', () => {
- class PluginAsync extends Plugin {}
- const asyncSpy = sinon.spy().named( 'async-call-spy' );
- // Synchronous plugin that depends on an asynchronous one.
- pluginDefinition( 'sync', [ 'async' ] );
- amdUtils.define( 'async', () => {
- PluginAsync.prototype.init = sinon.spy( () => {
- return new Promise( ( resolve ) => {
- setTimeout( () => {
- asyncSpy();
- resolve();
- }, 0 );
- } );
- } );
- return PluginAsync;
- } );
- const editor = new Editor( element, {
- features: [ 'A', 'sync' ],
- creator: 'creator-test'
- } );
- return editor.init().then( () => {
- sinon.assert.callOrder(
- editor.plugins.get( 'creator-test' ).init,
- editor.plugins.get( pluginClasses.A ).init,
- editor.plugins.get( PluginAsync ).init,
- // This one is called with delay by the async init.
- asyncSpy,
- editor.plugins.get( pluginClasses.sync ).init
- );
- } );
- } );
- } );
- describe( 'plugins', () => {
- it( 'should be empty on new editor', () => {
- const editor = new Editor( element );
- expect( getPlugins( editor ) ).to.be.empty;
- } );
- } );
- describe( 'destroy', () => {
- it( 'should fire "destroy"', () => {
- const editor = new Editor( element );
- let spy = sinon.spy();
- editor.on( 'destroy', spy );
- return editor.destroy().then( () => {
- sinon.assert.called( spy );
- } );
- } );
- it( 'should delete the "element" property', () => {
- const editor = new Editor( element );
- return editor.destroy().then( () => {
- expect( editor ).to.not.have.property( 'element' );
- } );
- } );
- } );
- /**
- * @param {String} name Name of the plugin.
- * @param {String[]} deps Dependencies of the plugin (only other plugins).
- */
- function pluginDefinition( name, deps ) {
- amdUtils.define( name, deps || [], function() {
- class NewPlugin extends Plugin {}
- NewPlugin.prototype.init = sinon.spy().named( name );
- NewPlugin.requires = Array.from( arguments );
- pluginClasses[ name ] = NewPlugin;
- return NewPlugin;
- } );
- }
- /**
- * Returns an array of loaded plugins.
- */
- function getPlugins( editor ) {
- const plugins = [];
- for ( let entry of editor.plugins ) {
- // Keep only plugins kept under their classes.
- if ( typeof entry[ 0 ] == 'function' ) {
- plugins.push( entry[ 1 ] );
- }
- }
- return plugins;
- }
|