decouplededitor.js 4.9 KB

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