integration.js 3.4 KB

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