decouplededitor.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. describe( 'ui', () => {
  75. it( 'attaches editable UI as view\'s DOM root', () => {
  76. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  77. } );
  78. } );
  79. } );
  80. describe( 'create - events', () => {
  81. afterEach( () => {
  82. return editor.destroy();
  83. } );
  84. it( 'fires all events in the right order', () => {
  85. const fired = [];
  86. function spy( evt ) {
  87. fired.push( evt.name );
  88. }
  89. class EventWatcher extends Plugin {
  90. init() {
  91. this.editor.on( 'pluginsReady', spy );
  92. this.editor.on( 'uiReady', spy );
  93. this.editor.on( 'dataReady', spy );
  94. this.editor.on( 'ready', spy );
  95. }
  96. }
  97. return DecoupledEditor
  98. .create( editorData, {
  99. plugins: [ EventWatcher ]
  100. } )
  101. .then( newEditor => {
  102. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  103. editor = newEditor;
  104. } );
  105. } );
  106. it( 'fires dataReady once data is loaded', () => {
  107. let data;
  108. class EventWatcher extends Plugin {
  109. init() {
  110. this.editor.on( 'dataReady', () => {
  111. data = this.editor.getData();
  112. } );
  113. }
  114. }
  115. return DecoupledEditor
  116. .create( editorData, {
  117. plugins: [ EventWatcher, Paragraph, Bold ]
  118. } )
  119. .then( newEditor => {
  120. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  121. editor = newEditor;
  122. } );
  123. } );
  124. it( 'fires uiReady once UI is rendered', () => {
  125. let isReady;
  126. class EventWatcher extends Plugin {
  127. init() {
  128. this.editor.on( 'uiReady', () => {
  129. isReady = this.editor.ui.view.isRendered;
  130. } );
  131. }
  132. }
  133. return DecoupledEditor
  134. .create( editorData, {
  135. plugins: [ EventWatcher ]
  136. } )
  137. .then( newEditor => {
  138. expect( isReady ).to.be.true;
  139. editor = newEditor;
  140. } );
  141. } );
  142. } );
  143. describe( 'destroy', () => {
  144. beforeEach( function() {
  145. return DecoupledEditor
  146. .create( editorData, { plugins: [ Paragraph ] } )
  147. .then( newEditor => {
  148. editor = newEditor;
  149. } );
  150. } );
  151. it( 'destroys the UI', () => {
  152. const spy = sinon.spy( editor.ui, 'destroy' );
  153. return editor.destroy()
  154. .then( () => {
  155. sinon.assert.calledOnce( spy );
  156. } );
  157. } );
  158. } );
  159. } );