integration.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 env from '@ckeditor/ckeditor5-utils/src/env';
  14. describe( 'ImageToolbar integration', () => {
  15. describe( 'with the BalloonToolbar', () => {
  16. let balloon, balloonToolbar, newEditor, editorElement;
  17. beforeEach( () => {
  18. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  19. sinon.stub( env, 'isEdge' ).get( () => false );
  20. editorElement = global.document.createElement( 'div' );
  21. global.document.body.appendChild( editorElement );
  22. return ClassicTestEditor
  23. .create( editorElement, {
  24. plugins: [ Image, ImageToolbar, BalloonToolbar, Paragraph ]
  25. } )
  26. .then( editor => {
  27. newEditor = editor;
  28. balloon = newEditor.plugins.get( 'ContextualBalloon' );
  29. balloonToolbar = newEditor.plugins.get( 'BalloonToolbar' );
  30. const button = new View();
  31. button.element = global.document.createElement( 'div' );
  32. // There must be at least one toolbar items which is not disabled to show it.
  33. // https://github.com/ckeditor/ckeditor5-ui/issues/269
  34. balloonToolbar.toolbarView.items.add( button );
  35. newEditor.editing.view.isFocused = true;
  36. newEditor.editing.view.getDomRoot().focus();
  37. } );
  38. } );
  39. afterEach( () => {
  40. editorElement.remove();
  41. return newEditor.destroy();
  42. } );
  43. it( 'should prevent the BalloonToolbar from being displayed when an image is selected', () => {
  44. // When image is selected along with text.
  45. setModelData( newEditor.model, '<paragraph>fo[o</paragraph><image alt="alt text" src="foo.png"></image>]' );
  46. balloonToolbar.show();
  47. // BalloonToolbar should be visible.
  48. expect( balloon.visibleView ).to.equal( balloonToolbar.toolbarView );
  49. // When only image is selected.
  50. setModelData( newEditor.model, '<paragraph>foo</paragraph>[<image alt="alt text" src="foo.png"></image>]' );
  51. balloonToolbar.show();
  52. // BalloonToolbar should not be visible.
  53. expect( balloon.visibleView ).to.be.null;
  54. } );
  55. it( 'should listen to BalloonToolbar#show event with the high priority', () => {
  56. const highestPrioritySpy = sinon.spy();
  57. const highPrioritySpy = sinon.spy();
  58. const normalPrioritySpy = sinon.spy();
  59. // Select an image
  60. setModelData( newEditor.model, '<paragraph>foo</paragraph>[<image alt="alt text" src="foo.png"></image>]' );
  61. newEditor.listenTo( balloonToolbar, 'show', highestPrioritySpy, { priority: 'highest' } );
  62. newEditor.listenTo( balloonToolbar, 'show', highPrioritySpy, { priority: 'high' } );
  63. newEditor.listenTo( balloonToolbar, 'show', normalPrioritySpy, { priority: 'normal' } );
  64. balloonToolbar.show();
  65. sinon.assert.calledOnce( highestPrioritySpy );
  66. sinon.assert.notCalled( highPrioritySpy );
  67. sinon.assert.notCalled( normalPrioritySpy );
  68. } );
  69. } );
  70. } );