balloontoolbar.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 BalloonToolbarEditorUI from '../src/balloontoolbareditorui';
  7. import BalloonToolbarEditorUIView from '../src/balloontoolbareditoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import BalloonToolbarEditor from '../src/balloontoolbar';
  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 ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. import count from '@ckeditor/ckeditor5-utils/src/count';
  16. testUtils.createSinonSandbox();
  17. describe( 'BalloonToolbarEditor', () => {
  18. let editor, editorElement;
  19. beforeEach( () => {
  20. editorElement = document.createElement( 'div' );
  21. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  22. document.body.appendChild( editorElement );
  23. } );
  24. afterEach( () => {
  25. editorElement.remove();
  26. } );
  27. describe( 'constructor()', () => {
  28. beforeEach( () => {
  29. editor = new BalloonToolbarEditor( editorElement, {
  30. plugins: [ Bold ],
  31. toolbar: [ 'Bold' ]
  32. } );
  33. } );
  34. it( 'pushes ContextualToolbar to the list of plugins', () => {
  35. expect( editor.config.get( 'plugins' ) ).to.include( ContextualToolbar );
  36. } );
  37. it( 'pipes config#toolbar to config#contextualToolbar', () => {
  38. expect( editor.config.get( 'contextualToolbar' ) ).to.have.members( [ 'Bold' ] );
  39. } );
  40. it( 'creates a single div editable root in the view', () => {
  41. expect( editor.editing.view.getRoot() ).to.have.property( 'name', 'div' );
  42. } );
  43. it( 'creates a single document root', () => {
  44. expect( count( editor.document.getRootNames() ) ).to.equal( 1 );
  45. expect( editor.document.getRoot() ).to.have.property( 'name', '$root' );
  46. } );
  47. it( 'uses HTMLDataProcessor', () => {
  48. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  49. } );
  50. it( 'creates the UI using BalloonToolbarEditorUI classes', () => {
  51. expect( editor.ui ).to.be.instanceof( BalloonToolbarEditorUI );
  52. expect( editor.ui.view ).to.be.instanceof( BalloonToolbarEditorUIView );
  53. } );
  54. } );
  55. describe( 'create()', () => {
  56. beforeEach( function() {
  57. return BalloonToolbarEditor.create( editorElement, {
  58. plugins: [ Paragraph, Bold ]
  59. } )
  60. .then( newEditor => {
  61. editor = newEditor;
  62. } );
  63. } );
  64. afterEach( () => {
  65. return editor.destroy();
  66. } );
  67. it( 'creates an instance which inherits from the BalloonToolbarEditor', () => {
  68. expect( editor ).to.be.instanceof( BalloonToolbarEditor );
  69. } );
  70. it( 'creates element–less UI view', () => {
  71. expect( editor.ui.view.element ).to.be.null;
  72. } );
  73. it( 'attaches editable UI as view\'s DOM root', () => {
  74. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  75. } );
  76. it( 'loads data from the editor element', () => {
  77. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  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 BalloonToolbarEditor.create( editorElement, {
  98. plugins: [ EventWatcher ]
  99. } )
  100. .then( newEditor => {
  101. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  102. editor = newEditor;
  103. } );
  104. } );
  105. it( 'fires dataReady once data is loaded', () => {
  106. let data;
  107. class EventWatcher extends Plugin {
  108. init() {
  109. this.editor.on( 'dataReady', () => {
  110. data = this.editor.getData();
  111. } );
  112. }
  113. }
  114. return BalloonToolbarEditor.create( editorElement, {
  115. plugins: [ EventWatcher, Paragraph, Bold ]
  116. } )
  117. .then( newEditor => {
  118. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  119. editor = newEditor;
  120. } );
  121. } );
  122. it( 'fires uiReady once UI is ready', () => {
  123. let isReady;
  124. class EventWatcher extends Plugin {
  125. init() {
  126. this.editor.on( 'uiReady', () => {
  127. isReady = this.editor.ui.view.ready;
  128. } );
  129. }
  130. }
  131. return BalloonToolbarEditor.create( editorElement, {
  132. plugins: [ EventWatcher ]
  133. } )
  134. .then( newEditor => {
  135. expect( isReady ).to.be.true;
  136. editor = newEditor;
  137. } );
  138. } );
  139. } );
  140. describe( 'destroy', () => {
  141. beforeEach( function() {
  142. return BalloonToolbarEditor.create( editorElement, { plugins: [ Paragraph ] } )
  143. .then( newEditor => {
  144. editor = newEditor;
  145. } );
  146. } );
  147. it( 'sets the data back to the editor element', () => {
  148. editor.setData( '<p>foo</p>' );
  149. return editor.destroy()
  150. .then( () => {
  151. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  152. } );
  153. } );
  154. } );
  155. } );