integration.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 env from '@ckeditor/ckeditor5-utils/src/env';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. describe( 'ImageToolbar integration', () => {
  16. describe( 'with the BalloonToolbar', () => {
  17. let balloon, balloonToolbar, newEditor, editorElement;
  18. testUtils.createSinonSandbox();
  19. beforeEach( () => {
  20. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  21. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  22. editorElement = global.document.createElement( 'div' );
  23. global.document.body.appendChild( editorElement );
  24. return ClassicTestEditor
  25. .create( editorElement, {
  26. plugins: [ Image, ImageToolbar, BalloonToolbar, Paragraph ]
  27. } )
  28. .then( editor => {
  29. newEditor = editor;
  30. balloon = newEditor.plugins.get( 'ContextualBalloon' );
  31. balloonToolbar = newEditor.plugins.get( 'BalloonToolbar' );
  32. const button = new View();
  33. button.element = global.document.createElement( 'div' );
  34. // There must be at least one toolbar items which is not disabled to show it.
  35. // https://github.com/ckeditor/ckeditor5-ui/issues/269
  36. balloonToolbar.toolbarView.items.add( button );
  37. newEditor.editing.view.isFocused = true;
  38. newEditor.editing.view.getDomRoot().focus();
  39. } );
  40. } );
  41. afterEach( () => {
  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="/assets/sample.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="/assets/sample.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="/assets/sample.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. } );