| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- 'use strict';
- /* bender-tags: editor, creator */
- import moduleUtils from '/tests/_utils/module.js';
- import testUtils from '/tests/_utils/utils.js';
- import coreTestUtils from '/tests/core/_utils/utils.js';
- import Editor from '/ckeditor5/core/editor.js';
- import Creator from '/ckeditor5/core/creator.js';
- import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
- let editor, element;
- function initEditor( config ) {
- element = document.createElement( 'div' );
- document.body.appendChild( element );
- editor = new Editor( element, config );
- return editor.init();
- }
- testUtils.createSinonSandbox();
- before( () => {
- coreTestUtils.defineEditorCreatorMock( 'test1', {
- create: sinon.spy(),
- destroy: sinon.spy()
- } );
- coreTestUtils.defineEditorCreatorMock( 'test-throw-on-many1' );
- coreTestUtils.defineEditorCreatorMock( 'test-throw-on-many2' );
- coreTestUtils.defineEditorCreatorMock( 'test-config1', {
- create: sinon.spy()
- } );
- coreTestUtils.defineEditorCreatorMock( 'test-config2', {
- create: sinon.spy()
- } );
- moduleUtils.define( 'test3', [ 'core/plugin' ], ( Plugin ) => {
- return class extends Plugin {};
- } );
- moduleUtils.define( 'creator-async-create', [ 'core/creator' ], ( Creator ) => {
- return class extends Creator {
- create() {
- return new Promise( ( resolve, reject ) => {
- reject( new Error( 'Catch me - create.' ) );
- } );
- }
- destroy() {}
- };
- } );
- moduleUtils.define( 'creator-async-destroy', [ 'core/creator' ], ( Creator ) => {
- return class extends Creator {
- create() {}
- destroy() {
- return new Promise( ( resolve, reject ) => {
- reject( new Error( 'Catch me - destroy.' ) );
- } );
- }
- };
- } );
- moduleUtils.define( 'creator-destroy-order', [ 'core/creator' ], ( Creator ) => {
- return class extends Creator {
- create() {}
- destroy() {
- editor._elementInsideCreatorDestroy = this.editor.element;
- editor._destroyOrder.push( 'creator' );
- }
- };
- } );
- } );
- afterEach( () => {
- editor = null; // To make sure we're using the freshly inited editor.
- } );
- ///////////////////
- describe( 'init', () => {
- it( 'should instantiate the creator and call create()', () => {
- return initEditor( {
- creator: 'creator-test1'
- } )
- .then( () => {
- let creator = editor.plugins.get( 'creator-test1' );
- expect( creator ).to.be.instanceof( Creator );
- // The create method has been called.
- sinon.assert.calledOnce( creator.create );
- } );
- } );
- it( 'should throw if creator is not defined', () => {
- return initEditor( {} )
- .then( () => {
- throw new Error( 'This should not be executed.' );
- } )
- .catch( ( err ) => {
- expect( err ).to.be.instanceof( CKEditorError );
- expect( err.message ).to.match( /^editor-undefined-creator:/ );
- } );
- } );
- it( 'should use the creator specified in config.creator', () => {
- return initEditor( {
- creator: 'creator-test-config2',
- features: [ 'creator-test-config1', 'creator-test-config2' ],
- } )
- .then( () => {
- let creator1 = editor.plugins.get( 'creator-test-config1' );
- let creator2 = editor.plugins.get( 'creator-test-config2' );
- sinon.assert.calledOnce( creator2.create );
- sinon.assert.notCalled( creator1.create );
- } );
- } );
- it( 'should throw an error if the creator doesn\'t exist', () => {
- return initEditor( {
- creator: 'bad'
- } )
- .then( () => {
- throw new Error( 'This should not be executed.' );
- } )
- .catch( ( err ) => {
- // It's the Require.JS error.
- expect( err ).to.be.an.instanceof( Error );
- expect( err.message ).to.match( /^Script error for/ );
- } );
- } );
- it( 'should chain the promise from the creator (enables async creators)', () => {
- return initEditor( {
- creator: 'creator-async-create'
- } )
- .then( () => {
- throw new Error( 'This should not be executed.' );
- } )
- .catch( ( err ) => {
- // Unfortunately fake timers don't work with promises, so throwing in the creator's create()
- // seems to be the only way to test that the promise chain isn't broken.
- expect( err ).to.have.property( 'message', 'Catch me - create.' );
- } );
- } );
- } );
- describe( 'destroy', () => {
- it( 'should call "destroy" on the creator', () => {
- let creator1;
- return initEditor( {
- creator: 'creator-test1'
- } )
- .then( () => {
- creator1 = editor.plugins.get( 'creator-test1' );
- return editor.destroy();
- } )
- .then( () => {
- sinon.assert.calledOnce( creator1.destroy );
- } );
- } );
- it( 'should chain the promise from the creator (enables async creators)', () => {
- return initEditor( {
- creator: 'creator-async-destroy'
- } )
- .then( () => {
- return editor.destroy();
- } )
- .then( () => {
- throw new Error( 'This should not be executed.' );
- } )
- .catch( ( err ) => {
- // Unfortunately fake timers don't work with promises, so throwing in the creator's destroy()
- // seems to be the only way to test that the promise chain isn't broken.
- expect( err ).to.have.property( 'message', 'Catch me - destroy.' );
- } );
- } );
- it( 'should do things in the correct order', () => {
- return initEditor( {
- creator: 'creator-destroy-order'
- } )
- .then( () => {
- editor._destroyOrder = [];
- editor.on( 'destroy', () => {
- editor._destroyOrder.push( 'event' );
- } );
- return editor.destroy();
- } )
- .then( () => {
- expect( editor._elementInsideCreatorDestroy ).to.not.be.undefined;
- expect( editor._destroyOrder ).to.deep.equal( [ 'event', 'creator' ] );
- } );
- } );
- } );
|