integration.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  10. import Image from '../src/image';
  11. import ImageToolbar from '../src/imagetoolbar';
  12. import View from '@ckeditor/ckeditor5-ui/src/view';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. describe( 'ImageToolbar integration', () => {
  15. describe( 'with the BalloonToolbar', () => {
  16. let balloon, balloonToolbar, newEditor, editorElement;
  17. testUtils.createSinonSandbox();
  18. beforeEach( () => {
  19. editorElement = global.document.createElement( 'div' );
  20. global.document.body.appendChild( editorElement );
  21. return ClassicTestEditor
  22. .create( editorElement, {
  23. plugins: [ Image, ImageToolbar, BalloonToolbar, Paragraph ]
  24. } )
  25. .then( editor => {
  26. newEditor = editor;
  27. balloon = newEditor.plugins.get( 'ContextualBalloon' );
  28. balloonToolbar = newEditor.plugins.get( 'BalloonToolbar' );
  29. const button = new View();
  30. button.element = global.document.createElement( 'div' );
  31. // There must be at least one toolbar items which is not disabled to show it.
  32. // https://github.com/ckeditor/ckeditor5-ui/issues/269
  33. balloonToolbar.toolbarView.items.add( button );
  34. newEditor.editing.view.isFocused = true;
  35. newEditor.editing.view.getDomRoot().focus();
  36. } );
  37. } );
  38. afterEach( () => {
  39. editorElement.remove();
  40. return newEditor.destroy();
  41. } );
  42. it( 'should prevent the BalloonToolbar from being displayed when an image is selected', () => {
  43. // When image is selected along with text.
  44. setModelData( newEditor.model, '<paragraph>fo[o</paragraph><image alt="alt text" src="/assets/sample.png"></image>]' );
  45. balloonToolbar.show();
  46. // BalloonToolbar should be visible.
  47. expect( balloon.visibleView ).to.equal( balloonToolbar.toolbarView );
  48. // When only image is selected.
  49. setModelData( newEditor.model, '<paragraph>foo</paragraph>[<image alt="alt text" src="/assets/sample.png"></image>]' );
  50. balloonToolbar.show();
  51. // BalloonToolbar should not be visible.
  52. expect( balloon.visibleView ).to.be.null;
  53. } );
  54. it( 'should listen to BalloonToolbar#show event with the high priority', () => {
  55. const highestPrioritySpy = sinon.spy();
  56. const highPrioritySpy = sinon.spy();
  57. const normalPrioritySpy = sinon.spy();
  58. // Select an image
  59. setModelData( newEditor.model, '<paragraph>foo</paragraph>[<image alt="alt text" src="/assets/sample.png"></image>]' );
  60. newEditor.listenTo( balloonToolbar, 'show', highestPrioritySpy, { priority: 'highest' } );
  61. newEditor.listenTo( balloonToolbar, 'show', highPrioritySpy, { priority: 'high' } );
  62. newEditor.listenTo( balloonToolbar, 'show', normalPrioritySpy, { priority: 'normal' } );
  63. balloonToolbar.show();
  64. sinon.assert.calledOnce( highestPrioritySpy );
  65. sinon.assert.notCalled( highPrioritySpy );
  66. sinon.assert.notCalled( normalPrioritySpy );
  67. } );
  68. } );
  69. } );