Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #135 from ckeditor/t/134
Browse files Browse the repository at this point in the history
Fix: Editor#destroy waits for the initialization. Closes #134.
  • Loading branch information
Piotr Jasiun authored Jun 29, 2018
2 parents 734166a + b86c0fd commit ad1da26
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
19 changes: 14 additions & 5 deletions src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,26 @@ export default class Editor {
/**
* Destroys the editor instance, releasing all resources used by it.
*
* **Note** The editor can not be destroyed during the initialization phase so
* this methods waits for the editor initialization before destroying.
*
* @fires destroy
* @returns {Promise} A promise that resolves once the editor instance is fully destroyed.
*/
destroy() {
this.fire( 'destroy' );

this.stopListening();
let readyPromise = Promise.resolve();

this.commands.destroy();
if ( this.state == 'initializing' ) {
readyPromise = new Promise( resolve => this.once( 'ready', resolve ) );
}

return this.plugins.destroy()
return readyPromise
.then( () => {
this.fire( 'destroy' );
this.stopListening();
this.commands.destroy();
} )
.then( () => this.plugins.destroy() )
.then( () => {
this.model.destroy();
this.data.destroy();
Expand Down
10 changes: 6 additions & 4 deletions tests/_utils-tests/modeltesteditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ describe( 'ModelTestEditor', () => {

it( 'should disable editing pipeline', () => {
const spy = sinon.spy( EditingController.prototype, 'destroy' );
const editor = new ModelTestEditor( { foo: 1 } );

sinon.assert.calledOnce( spy );
return ModelTestEditor.create( { foo: 1 } ).then( editor => {
sinon.assert.calledOnce( spy );

spy.restore();

spy.restore();
return editor.destroy();
return editor.destroy();
} );
} );

it( 'creates main root element', () => {
Expand Down
60 changes: 38 additions & 22 deletions tests/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ describe( 'Editor', () => {
} );

it( 'is `destroyed` after editor destroy', () => {
const editor = new Editor();

return editor.destroy().then( () => {
expect( editor.state ).to.equal( 'destroyed' );
return Editor.create().then( editor => {
return editor.destroy().then( () => {
expect( editor.state ).to.equal( 'destroyed' );
} );
} );
} );

Expand Down Expand Up @@ -283,33 +283,49 @@ describe( 'Editor', () => {

describe( 'destroy()', () => {
it( 'should fire "destroy"', () => {
const editor = new Editor();
const spy = sinon.spy();
return Editor.create().then( editor => {
const spy = sinon.spy();

editor.on( 'destroy', spy );
editor.on( 'destroy', spy );

return editor.destroy().then( () => {
expect( spy.calledOnce ).to.be.true;
return editor.destroy().then( () => {
expect( spy.calledOnce ).to.be.true;
} );
} );
} );

it( 'should destroy all components it initialized', () => {
return Editor.create().then( editor => {
const dataDestroySpy = sinon.spy( editor.data, 'destroy' );
const modelDestroySpy = sinon.spy( editor.model, 'destroy' );
const editingDestroySpy = sinon.spy( editor.editing, 'destroy' );
const pluginsDestroySpy = sinon.spy( editor.plugins, 'destroy' );
const keystrokesDestroySpy = sinon.spy( editor.keystrokes, 'destroy' );

return editor.destroy()
.then( () => {
sinon.assert.calledOnce( dataDestroySpy );
sinon.assert.calledOnce( modelDestroySpy );
sinon.assert.calledOnce( editingDestroySpy );
sinon.assert.calledOnce( pluginsDestroySpy );
sinon.assert.calledOnce( keystrokesDestroySpy );
} );
} );
} );

it( 'should wait for the full init before destroying', done => {
const spy = sinon.spy();
const editor = new Editor();

const dataDestroySpy = sinon.spy( editor.data, 'destroy' );
const modelDestroySpy = sinon.spy( editor.model, 'destroy' );
const editingDestroySpy = sinon.spy( editor.editing, 'destroy' );
const pluginsDestroySpy = sinon.spy( editor.plugins, 'destroy' );
const keystrokesDestroySpy = sinon.spy( editor.keystrokes, 'destroy' );
editor.on( 'destroy', () => {
done();
} );

return editor.destroy()
.then( () => {
sinon.assert.calledOnce( dataDestroySpy );
sinon.assert.calledOnce( modelDestroySpy );
sinon.assert.calledOnce( editingDestroySpy );
sinon.assert.calledOnce( pluginsDestroySpy );
sinon.assert.calledOnce( keystrokesDestroySpy );
} );
editor.destroy();

sinon.assert.notCalled( spy );

editor.fire( 'ready' );
} );
} );

Expand Down
1 change: 1 addition & 0 deletions tests/editor/utils/attachtoform.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe( 'attachToForm()', () => {
editor.data.processor = new HtmlDataProcessor();
editor.model.document.createRoot();
editor.model.schema.extend( '$text', { allowIn: '$root' } );
editor.fire( 'ready' );

editor.data.set( 'foo bar' );
} );
Expand Down

0 comments on commit ad1da26

Please sign in to comment.