imageballoonpanelview.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global window, document, Event */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ImageEngine from '../../../src/image/imageengine';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import ImageBalloonPanel from '../../../src/image/ui/imageballoonpanelview';
  10. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  11. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'ImageBalloonPanel', () => {
  13. let editor, panel, doc, editorElement;
  14. beforeEach( () => {
  15. editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. return ClassicEditor
  18. .create( editorElement, {
  19. plugins: [ ImageEngine, Paragraph ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. panel = new ImageBalloonPanel( editor );
  24. doc = editor.document;
  25. return editor.ui.view.body.add( panel );
  26. } );
  27. } );
  28. afterEach( () => {
  29. editorElement.remove();
  30. return editor.destroy();
  31. } );
  32. it( 'should add element to editor\'s focus tracker after init', () => {
  33. const spy = sinon.spy( editor.ui.focusTracker, 'add' );
  34. const panel = new ImageBalloonPanel( editor );
  35. sinon.assert.notCalled( spy );
  36. panel.init();
  37. sinon.assert.calledOnce( spy );
  38. sinon.assert.calledWithExactly( spy, panel.element );
  39. } );
  40. it( 'should detach panel when focus is removed', () => {
  41. const spy = sinon.spy( panel, 'detach' );
  42. editor.ui.focusTracker.isFocused = true;
  43. editor.ui.focusTracker.isFocused = false;
  44. sinon.assert.calledOnce( spy );
  45. } );
  46. it( 'should detach when image element is no longer selected', () => {
  47. const spy = sinon.spy( panel, 'detach' );
  48. setData( doc, '<paragraph>foo</paragraph>[<image src=""></image>]' );
  49. panel.attach();
  50. doc.enqueueChanges( () => {
  51. doc.selection.removeAllRanges();
  52. } );
  53. sinon.assert.calledOnce( spy );
  54. } );
  55. it( 'should attach panel correctly', () => {
  56. const spy = sinon.spy( panel, 'attachTo' );
  57. setData( doc, '[<image src=""></image>]' );
  58. panel.attach();
  59. testPanelAttach( spy );
  60. } );
  61. it( 'should calculate panel position on scroll event', () => {
  62. setData( doc, '[<image src=""></image>]' );
  63. panel.attach();
  64. const spy = sinon.spy( panel, 'attachTo' );
  65. window.dispatchEvent( new Event( 'scroll' ) );
  66. testPanelAttach( spy );
  67. } );
  68. it( 'should calculate panel position on resize event event', () => {
  69. setData( doc, '[<image src=""></image>]' );
  70. panel.attach();
  71. const spy = sinon.spy( panel, 'attachTo' );
  72. window.dispatchEvent( new Event( 'resize' ) );
  73. testPanelAttach( spy );
  74. } );
  75. it( 'should hide panel on detach', () => {
  76. setData( doc, '[<image src=""></image>]' );
  77. panel.attach();
  78. const spy = sinon.spy( panel, 'hide' );
  79. panel.detach();
  80. sinon.assert.calledOnce( spy );
  81. } );
  82. it( 'should not reposition panel after detaching', () => {
  83. setData( doc, '[<image src=""></image>]' );
  84. panel.attach();
  85. const spy = sinon.spy( panel, 'attachTo' );
  86. panel.detach();
  87. window.dispatchEvent( new Event( 'resize' ) );
  88. window.dispatchEvent( new Event( 'scroll' ) );
  89. sinon.assert.notCalled( spy );
  90. } );
  91. // Tests if panel.attachTo() was called correctly.
  92. function testPanelAttach( spy ) {
  93. sinon.assert.calledOnce( spy );
  94. const options = spy.firstCall.args[ 0 ];
  95. // Check if proper target element was used.
  96. expect( options.target.tagName.toLowerCase() ).to.equal( 'figure' );
  97. // Check if correct positions are used.
  98. const [ north, south ] = options.positions;
  99. expect( north ).to.equal( BalloonPanelView.defaultPositions.northArrowSouth );
  100. expect( south ).to.equal( BalloonPanelView.defaultPositions.southArrowNorth );
  101. }
  102. } );