integration.js 3.2 KB

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