classic.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicEditorUI from '../src/classiceditorui';
  7. import ClassicEditorUIView from '../src/classiceditoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import ClassicEditor from '../src/classic';
  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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. testUtils.createSinonSandbox();
  16. describe( 'ClassicEditor', () => {
  17. let editor, editorElement;
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  21. document.body.appendChild( editorElement );
  22. } );
  23. afterEach( () => {
  24. editorElement.remove();
  25. } );
  26. describe( 'constructor()', () => {
  27. beforeEach( () => {
  28. editor = new ClassicEditor( editorElement );
  29. } );
  30. it( 'creates a single div editable root in the view', () => {
  31. expect( editor.editing.view.getRoot() ).to.have.property( 'name', 'div' );
  32. } );
  33. it( 'creates a single document root', () => {
  34. expect( count( editor.document.getRootNames() ) ).to.equal( 1 );
  35. expect( editor.document.getRoot() ).to.have.property( 'name', '$root' );
  36. } );
  37. it( 'creates the UI using BoxedEditorUI classes', () => {
  38. expect( editor.ui ).to.be.instanceof( ClassicEditorUI );
  39. expect( editor.ui.view ).to.be.instanceof( ClassicEditorUIView );
  40. } );
  41. it( 'uses HTMLDataProcessor', () => {
  42. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  43. } );
  44. } );
  45. describe( 'create()', () => {
  46. beforeEach( function() {
  47. return ClassicEditor.create( editorElement, {
  48. plugins: [ Paragraph, Bold ]
  49. } )
  50. .then( newEditor => {
  51. editor = newEditor;
  52. } );
  53. } );
  54. afterEach( () => {
  55. return editor.destroy();
  56. } );
  57. it( 'creates an instance which inherits from the ClassicEditor', () => {
  58. expect( editor ).to.be.instanceof( ClassicEditor );
  59. } );
  60. it( 'inserts editor UI next to editor element', () => {
  61. expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
  62. } );
  63. it( 'attaches editable UI as view\'s DOM root', () => {
  64. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  65. } );
  66. it( 'loads data from the editor element', () => {
  67. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  68. } );
  69. it( 'creates an instance of a ClassicEditor child class', () => {
  70. class CustomClassicEditor extends ClassicEditor {}
  71. return CustomClassicEditor.create( editorElement, {
  72. plugins: [ Paragraph, Bold ]
  73. } )
  74. .then( newEditor => {
  75. editor = newEditor;
  76. expect( newEditor ).to.be.instanceof( CustomClassicEditor );
  77. expect( newEditor ).to.be.instanceof( ClassicEditor );
  78. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  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 ClassicEditor.create( editorElement, {
  100. plugins: [ EventWatcher ]
  101. } )
  102. .then( ( newEditor ) => {
  103. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  104. editor = newEditor;
  105. } );
  106. } );
  107. it( 'fires dataReady once data is loaded', () => {
  108. let data;
  109. class EventWatcher extends Plugin {
  110. init() {
  111. this.editor.on( 'dataReady', () => {
  112. data = this.editor.getData();
  113. } );
  114. }
  115. }
  116. return ClassicEditor.create( editorElement, {
  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 ready', () => {
  125. let isReady;
  126. class EventWatcher extends Plugin {
  127. init() {
  128. this.editor.on( 'uiReady', () => {
  129. isReady = this.editor.ui.view.ready;
  130. } );
  131. }
  132. }
  133. return ClassicEditor.create( editorElement, {
  134. plugins: [ EventWatcher ]
  135. } )
  136. .then( ( newEditor ) => {
  137. expect( isReady ).to.be.true;
  138. editor = newEditor;
  139. } );
  140. } );
  141. } );
  142. describe( 'destroy', () => {
  143. beforeEach( function() {
  144. return ClassicEditor.create( editorElement, { plugins: [ Paragraph ] } )
  145. .then( newEditor => {
  146. editor = newEditor;
  147. } );
  148. } );
  149. it( 'sets the data back to the editor element', () => {
  150. editor.setData( '<p>foo</p>' );
  151. return editor.destroy()
  152. .then( () => {
  153. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  154. } );
  155. } );
  156. it( 'restores the editor element', () => {
  157. expect( editor.element.style.display ).to.equal( 'none' );
  158. return editor.destroy()
  159. .then( () => {
  160. expect( editor.element.style.display ).to.equal( '' );
  161. } );
  162. } );
  163. } );
  164. } );