diff --git a/src/decouplededitor.js b/src/decouplededitor.js index a2061fd..8ce0076 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -196,7 +196,7 @@ export default class DecoupledEditor extends Editor { editor.fire( 'uiReady' ); } ) .then( () => { - editor.data.init( editor.element ? getDataFromElement( editor.element ) : elementOrData ); + return editor.data.init( editor.element ? getDataFromElement( editor.element ) : elementOrData ); } ) .then( () => { editor.fire( 'dataReady' ); diff --git a/tests/decouplededitor.js b/tests/decouplededitor.js index 7f2450b..9752773 100644 --- a/tests/decouplededitor.js +++ b/tests/decouplededitor.js @@ -3,7 +3,7 @@ * For licensing, see LICENSE.md. */ -/* globals document */ +/* globals document, setTimeout */ import DecoupledEditorUI from '../src/decouplededitorui'; import DecoupledEditorUIView from '../src/decouplededitoruiview'; @@ -57,6 +57,39 @@ describe( 'DecoupledEditor', () => { } ); describe( 'create()', () => { + it( 'should properly handled async data initialization', done => { + const spy = sinon.spy(); + let resolver; + + class AsyncDataInit extends Plugin { + init() { + this.editor.on( 'dataReady', () => spy( 'dataReady' ) ); + + this.editor.data.on( 'init', evt => { + evt.stop(); + evt.return = new Promise( resolve => { + resolver = () => { + spy( 'asyncInit' ); + resolve(); + }; + } ); + }, { priority: 'high' } ); + } + } + + DecoupledEditor.create( '

foo bar

', { + plugins: [ Paragraph, Bold, AsyncDataInit ] + } ).then( editor => { + sinon.assert.calledWith( spy.firstCall, 'asyncInit' ); + sinon.assert.calledWith( spy.secondCall, 'dataReady' ); + + editor.destroy().then( done ); + } ); + + // Resolve init promise in next cycle to hold data initialization. + setTimeout( () => resolver() ); + } ); + describe( 'editor with data', () => { test( () => editorData ); } );