ckeditor.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import ClassicEditor from '../src/ckeditor';
  7. import BaseClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  8. import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
  9. describe( 'ClassicEditor build', () => {
  10. let editor, editorElement;
  11. beforeEach( () => {
  12. editorElement = document.createElement( 'div' );
  13. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  14. document.body.appendChild( editorElement );
  15. } );
  16. afterEach( () => {
  17. editorElement.remove();
  18. editor = null;
  19. } );
  20. describe( 'build', () => {
  21. it( 'contains plugins', () => {
  22. expect( ClassicEditor.builtinPlugins ).to.not.be.empty;
  23. } );
  24. it( 'contains config', () => {
  25. expect( ClassicEditor.defaultConfig.toolbar ).to.not.be.empty;
  26. } );
  27. } );
  28. describe( 'create()', () => {
  29. beforeEach( () => {
  30. return ClassicEditor.create( editorElement )
  31. .then( newEditor => {
  32. editor = newEditor;
  33. } );
  34. } );
  35. afterEach( () => {
  36. return editor.destroy();
  37. } );
  38. it( 'creates an instance which inherits from the ClassicEditor', () => {
  39. expect( editor ).to.be.instanceof( ClassicEditor );
  40. expect( editor ).to.be.instanceof( BaseClassicEditor );
  41. } );
  42. it( 'loads data from the editor element', () => {
  43. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  44. } );
  45. } );
  46. describe( 'destroy()', () => {
  47. beforeEach( () => {
  48. return ClassicEditor.create( editorElement )
  49. .then( newEditor => {
  50. editor = newEditor;
  51. } );
  52. } );
  53. it( 'sets the data back to the editor element', () => {
  54. editor.setData( '<p>foo</p>' );
  55. return editor.destroy()
  56. .then( () => {
  57. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  58. } );
  59. } );
  60. it( 'restores the editor element', () => {
  61. expect( editor.sourceElement.style.display ).to.equal( 'none' );
  62. return editor.destroy()
  63. .then( () => {
  64. expect( editor.sourceElement.style.display ).to.equal( '' );
  65. } );
  66. } );
  67. } );
  68. describe( 'plugins', () => {
  69. beforeEach( () => {
  70. return ClassicEditor.create( editorElement )
  71. .then( newEditor => {
  72. editor = newEditor;
  73. } );
  74. } );
  75. afterEach( () => {
  76. return editor.destroy();
  77. } );
  78. it( 'paragraph works', () => {
  79. const data = '<p>Some text inside a paragraph.</p>';
  80. editor.setData( data );
  81. expect( editor.getData() ).to.equal( data );
  82. } );
  83. it( 'basic-styles work', () => {
  84. const data = [
  85. '<p>',
  86. '<strong>Test:strong</strong>',
  87. '<i>Test:i</i>',
  88. '</p>'
  89. ].join( '' );
  90. editor.setData( data );
  91. expect( editor.getData() ).to.equal( data );
  92. } );
  93. it( 'block-quote works', () => {
  94. const data = '<blockquote><p>Quote</p></blockquote>';
  95. editor.setData( data );
  96. expect( editor.getData() ).to.equal( data );
  97. } );
  98. it( 'heading works', () => {
  99. const data = [
  100. '<h2>Heading 1.</h2>',
  101. '<h3>Heading 1.1</h3>',
  102. '<h4>Heading 1.1.1</h4>',
  103. '<h4>Heading 1.1.2</h4>',
  104. '<h3>Heading 1.2</h3>',
  105. '<h4>Heading 1.2.1</h4>',
  106. '<h2>Heading 2</h2>'
  107. ].join( '' );
  108. editor.setData( data );
  109. expect( editor.getData() ).to.equal( data );
  110. } );
  111. it( 'image works', () => {
  112. const data = '<figure class="image"><img src="/assets/sample.png"></figure>';
  113. editor.setData( data );
  114. expect( editor.getData() ).to.equal( data );
  115. } );
  116. it( 'list works', () => {
  117. const data = [
  118. '<ul>',
  119. '<li>Item 1.</li>',
  120. '<li>Item 2.</li>',
  121. '</ul>',
  122. '<ol>',
  123. '<li>Item 1.</li>',
  124. '<li>Item 2.</li>',
  125. '</ol>'
  126. ].join( '' );
  127. editor.setData( data );
  128. expect( editor.getData() ).to.equal( data );
  129. } );
  130. it( 'link works', () => {
  131. const data = '<p><a href="//ckeditor.com">CKEditor.com</a></p>';
  132. editor.setData( data );
  133. expect( editor.getData() ).to.equal( data );
  134. } );
  135. } );
  136. describe( 'config', () => {
  137. afterEach( () => {
  138. return editor.destroy();
  139. } );
  140. // https://github.com/ckeditor/ckeditor5/issues/572
  141. it( 'allows configuring toolbar items through config.toolbar', () => {
  142. return ClassicEditor
  143. .create( editorElement, {
  144. toolbar: [ 'bold' ]
  145. } )
  146. .then( newEditor => {
  147. editor = newEditor;
  148. expect( editor.ui.view.toolbar.items.length ).to.equal( 1 );
  149. } );
  150. } );
  151. // https://github.com/ckeditor/ckeditor5/issues/572
  152. it( 'allows configuring toolbar offset without overriding toolbar items', () => {
  153. return ClassicEditor
  154. .create( editorElement, {
  155. toolbar: {
  156. viewportTopOffset: 42
  157. }
  158. } )
  159. .then( newEditor => {
  160. editor = newEditor;
  161. expect( editor.ui.view.toolbar.items.length ).to.equal( 17 );
  162. expect( editor.ui.view.stickyPanel.viewportTopOffset ).to.equal( 42 );
  163. } );
  164. } );
  165. } );
  166. describeMemoryUsage( () => {
  167. testMemoryUsage(
  168. 'should not grow on multiple create/destroy',
  169. () => ClassicEditor.create( document.querySelector( '#mem-editor' ) ) );
  170. } );
  171. } );