decouplededitor.js 6.7 KB

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