8
0

decouplededitor.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import DecoupledEditorUI from '../src/decouplededitorui';
  6. import DecoupledEditorUIView from '../src/decouplededitoruiview';
  7. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  8. import DecoupledEditor from '../src/decouplededitor';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  12. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  13. import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. testUtils.createSinonSandbox();
  16. describe( 'DecoupledEditor', () => {
  17. let editor, editorData;
  18. beforeEach( () => {
  19. editorData = '<p><strong>foo</strong> bar</p>';
  20. } );
  21. describe( 'constructor()', () => {
  22. beforeEach( () => {
  23. editor = new DecoupledEditor();
  24. } );
  25. it( 'uses HTMLDataProcessor', () => {
  26. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  27. } );
  28. it( 'has a Data Interface', () => {
  29. testUtils.isMixed( DecoupledEditor, DataApiMixin );
  30. } );
  31. it( 'creates main root element', () => {
  32. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  33. } );
  34. describe( 'ui', () => {
  35. it( 'is created', () => {
  36. expect( editor.ui ).to.be.instanceof( DecoupledEditorUI );
  37. expect( editor.ui.view ).to.be.instanceof( DecoupledEditorUIView );
  38. } );
  39. } );
  40. } );
  41. describe( 'create()', () => {
  42. beforeEach( () => {
  43. return DecoupledEditor
  44. .create( editorData, {
  45. plugins: [ Paragraph, Bold ]
  46. } )
  47. .then( newEditor => {
  48. editor = newEditor;
  49. } );
  50. } );
  51. afterEach( () => {
  52. return editor.destroy();
  53. } );
  54. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  55. expect( editor ).to.be.instanceof( DecoupledEditor );
  56. } );
  57. it( 'loads the initial data', () => {
  58. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  59. } );
  60. // #53
  61. it( 'creates an instance of a DecoupledEditor child class', () => {
  62. class CustomDecoupledEditor extends DecoupledEditor {}
  63. return CustomDecoupledEditor
  64. .create( editorData, {
  65. plugins: [ Paragraph, Bold ]
  66. } )
  67. .then( newEditor => {
  68. expect( newEditor ).to.be.instanceof( CustomDecoupledEditor );
  69. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  70. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  71. return newEditor.destroy();
  72. } );
  73. } );
  74. // https://github.com/ckeditor/ckeditor5-editor-decoupled/issues/3
  75. it( 'initializes the data controller', () => {
  76. let dataInitSpy;
  77. class DataInitAssertPlugin extends Plugin {
  78. constructor( editor ) {
  79. super();
  80. this.editor = editor;
  81. }
  82. init() {
  83. dataInitSpy = sinon.spy( this.editor.data, 'init' );
  84. }
  85. }
  86. return DecoupledEditor
  87. .create( editorData, {
  88. plugins: [ Paragraph, Bold, DataInitAssertPlugin ]
  89. } )
  90. .then( newEditor => {
  91. sinon.assert.calledOnce( dataInitSpy );
  92. return newEditor.destroy();
  93. } );
  94. } );
  95. describe( 'ui', () => {
  96. it( 'attaches editable UI as view\'s DOM root', () => {
  97. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  98. } );
  99. } );
  100. } );
  101. describe( 'create - events', () => {
  102. afterEach( () => {
  103. return editor.destroy();
  104. } );
  105. it( 'fires all events in the right order', () => {
  106. const fired = [];
  107. function spy( evt ) {
  108. fired.push( evt.name );
  109. }
  110. class EventWatcher extends Plugin {
  111. init() {
  112. this.editor.on( 'pluginsReady', spy );
  113. this.editor.on( 'uiReady', spy );
  114. this.editor.on( 'dataReady', spy );
  115. this.editor.on( 'ready', spy );
  116. }
  117. }
  118. return DecoupledEditor
  119. .create( editorData, {
  120. plugins: [ EventWatcher ]
  121. } )
  122. .then( newEditor => {
  123. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  124. editor = newEditor;
  125. } );
  126. } );
  127. it( 'fires dataReady once data is loaded', () => {
  128. let data;
  129. class EventWatcher extends Plugin {
  130. init() {
  131. this.editor.on( 'dataReady', () => {
  132. data = this.editor.getData();
  133. } );
  134. }
  135. }
  136. return DecoupledEditor
  137. .create( editorData, {
  138. plugins: [ EventWatcher, Paragraph, Bold ]
  139. } )
  140. .then( newEditor => {
  141. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  142. editor = newEditor;
  143. } );
  144. } );
  145. it( 'fires uiReady once UI is rendered', () => {
  146. let isReady;
  147. class EventWatcher extends Plugin {
  148. init() {
  149. this.editor.on( 'uiReady', () => {
  150. isReady = this.editor.ui.view.isRendered;
  151. } );
  152. }
  153. }
  154. return DecoupledEditor
  155. .create( editorData, {
  156. plugins: [ EventWatcher ]
  157. } )
  158. .then( newEditor => {
  159. expect( isReady ).to.be.true;
  160. editor = newEditor;
  161. } );
  162. } );
  163. } );
  164. describe( 'destroy', () => {
  165. beforeEach( function() {
  166. return DecoupledEditor
  167. .create( editorData, { plugins: [ Paragraph ] } )
  168. .then( newEditor => {
  169. editor = newEditor;
  170. } );
  171. } );
  172. it( 'destroys the UI', () => {
  173. const spy = sinon.spy( editor.ui, 'destroy' );
  174. return editor.destroy()
  175. .then( () => {
  176. sinon.assert.calledOnce( spy );
  177. } );
  178. } );
  179. } );
  180. } );