imageballoonpanelview.js 4.0 KB

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