|
3 | 3 | * For licensing, see LICENSE.md. |
4 | 4 | */ |
5 | 5 |
|
6 | | -/* globals document */ |
| 6 | +/* globals document, setTimeout */ |
7 | 7 |
|
8 | 8 | import DecoupledEditorUI from '../src/decouplededitorui'; |
9 | 9 | import DecoupledEditorUIView from '../src/decouplededitoruiview'; |
@@ -57,6 +57,39 @@ describe( 'DecoupledEditor', () => { |
57 | 57 | } ); |
58 | 58 |
|
59 | 59 | describe( 'create()', () => { |
| 60 | + it( 'should properly handled async data initialization', done => { |
| 61 | + const spy = sinon.spy(); |
| 62 | + let resolver; |
| 63 | + |
| 64 | + class AsyncDataInit extends Plugin { |
| 65 | + init() { |
| 66 | + this.editor.on( 'dataReady', () => spy( 'dataReady' ) ); |
| 67 | + |
| 68 | + this.editor.data.on( 'init', evt => { |
| 69 | + evt.stop(); |
| 70 | + evt.return = new Promise( resolve => { |
| 71 | + resolver = () => { |
| 72 | + spy( 'asyncInit' ); |
| 73 | + resolve(); |
| 74 | + }; |
| 75 | + } ); |
| 76 | + }, { priority: 'high' } ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + DecoupledEditor.create( '<p>foo bar</p>', { |
| 81 | + plugins: [ Paragraph, Bold, AsyncDataInit ] |
| 82 | + } ).then( editor => { |
| 83 | + sinon.assert.calledWith( spy.firstCall, 'asyncInit' ); |
| 84 | + sinon.assert.calledWith( spy.secondCall, 'dataReady' ); |
| 85 | + |
| 86 | + editor.destroy().then( done ); |
| 87 | + } ); |
| 88 | + |
| 89 | + // Resolve init promise in next cycle to hold data initialization. |
| 90 | + setTimeout( () => resolver() ); |
| 91 | + } ); |
| 92 | + |
60 | 93 | describe( 'editor with data', () => { |
61 | 94 | test( () => editorData ); |
62 | 95 | } ); |
|
0 commit comments