8
0

classic.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 'ckeditor5-editor-classic/src/classiceditorui';
  7. import ClassicEditorUIView from 'ckeditor5-editor-classic/src/classiceditoruiview';
  8. import HtmlDataProcessor from 'ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import ClassicEditor from 'ckeditor5-editor-classic/src/classic';
  10. import Plugin from 'ckeditor5-core/src/plugin';
  11. import Paragraph from 'ckeditor5-paragraph/src/paragraph';
  12. import Bold from 'ckeditor5-basic-styles/src/bold';
  13. import testUtils from 'ckeditor5-core/tests/_utils/utils';
  14. import count from '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. } );
  70. describe( 'create - events', () => {
  71. afterEach( () => {
  72. return editor.destroy();
  73. } );
  74. it( 'fires all events in the right order', () => {
  75. const fired = [];
  76. function spy( evt ) {
  77. fired.push( evt.name );
  78. }
  79. class EventWatcher extends Plugin {
  80. init() {
  81. this.editor.on( 'pluginsReady', spy );
  82. this.editor.on( 'uiReady', spy );
  83. this.editor.on( 'dataReady', spy );
  84. this.editor.on( 'ready', spy );
  85. }
  86. }
  87. return ClassicEditor.create( editorElement, {
  88. plugins: [ EventWatcher ]
  89. } )
  90. .then( ( newEditor ) => {
  91. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  92. editor = newEditor;
  93. } );
  94. } );
  95. it( 'fires dataReady once data is loaded', () => {
  96. let data;
  97. class EventWatcher extends Plugin {
  98. init() {
  99. this.editor.on( 'dataReady', () => {
  100. data = this.editor.getData();
  101. } );
  102. }
  103. }
  104. return ClassicEditor.create( editorElement, {
  105. plugins: [ EventWatcher, Paragraph, Bold ]
  106. } )
  107. .then( ( newEditor ) => {
  108. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  109. editor = newEditor;
  110. } );
  111. } );
  112. it( 'fires uiReady once UI is ready', () => {
  113. let isReady;
  114. class EventWatcher extends Plugin {
  115. init() {
  116. this.editor.on( 'uiReady', () => {
  117. isReady = this.editor.ui.view.ready;
  118. } );
  119. }
  120. }
  121. return ClassicEditor.create( editorElement, {
  122. plugins: [ EventWatcher ]
  123. } )
  124. .then( ( newEditor ) => {
  125. expect( isReady ).to.be.true;
  126. editor = newEditor;
  127. } );
  128. } );
  129. } );
  130. describe( 'destroy', () => {
  131. beforeEach( function() {
  132. return ClassicEditor.create( editorElement, { plugins: [ Paragraph ] } )
  133. .then( newEditor => {
  134. editor = newEditor;
  135. } );
  136. } );
  137. it( 'sets the data back to the editor element', () => {
  138. editor.setData( '<p>foo</p>' );
  139. return editor.destroy()
  140. .then( () => {
  141. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  142. } );
  143. } );
  144. it( 'restores the editor element', () => {
  145. expect( editor.element.style.display ).to.equal( 'none' );
  146. return editor.destroy()
  147. .then( () => {
  148. expect( editor.element.style.display ).to.equal( '' );
  149. } );
  150. } );
  151. } );
  152. } );