imageballoonpanelview.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, editingView;
  14. beforeEach( () => {
  15. const editorElement = global.document.createElement( 'div' );
  16. global.document.body.appendChild( editorElement );
  17. return ClassicEditor.create( editorElement, {
  18. plugins: [ ImageEngine ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. panel = new ImageBalloonPanel( editor );
  23. document = editor.document;
  24. editingView = editor.editing.view;
  25. return editor.ui.view.body.add( panel );
  26. } );
  27. } );
  28. afterEach( () => {
  29. return editor.destroy();
  30. } );
  31. it( 'init method should return a promise', () => {
  32. const panel = new ImageBalloonPanel( editor );
  33. const promise = panel.init();
  34. expect( promise ).to.be.instanceof( Promise );
  35. return promise;
  36. } );
  37. it( 'should add element to editor\'s focus tracker after init', () => {
  38. const spy = sinon.spy( editor.ui.focusTracker, 'add' );
  39. const panel = new ImageBalloonPanel( editor );
  40. sinon.assert.notCalled( spy );
  41. return panel.init().then( () => {
  42. sinon.assert.calledOnce( spy );
  43. sinon.assert.calledWithExactly( spy, panel.element );
  44. } );
  45. } );
  46. it( 'should detach panel when focus is removed', () => {
  47. const spy = sinon.spy( panel, 'detach' );
  48. editor.ui.focusTracker.isFocused = true;
  49. editor.ui.focusTracker.isFocused = false;
  50. sinon.assert.calledOnce( spy );
  51. } );
  52. it( 'should detach when image element is no longer selected', () => {
  53. const spy = sinon.spy( panel, 'detach' );
  54. setData( document, '[<image src=""></image>]' );
  55. panel.attach();
  56. document.enqueueChanges( () => {
  57. document.selection.removeAllRanges();
  58. } );
  59. sinon.assert.calledOnce( spy );
  60. } );
  61. it( 'should attach panel correctly', () => {
  62. const spy = sinon.spy( panel, 'attachTo' );
  63. setData( document, '[<image src=""></image>]' );
  64. panel.attach();
  65. testPanelAttach( spy );
  66. } );
  67. it( 'should calculate panel position on scroll event', () => {
  68. setData( document, '[<image src=""></image>]' );
  69. panel.attach();
  70. const spy = sinon.spy( panel, 'attachTo' );
  71. global.window.dispatchEvent( new Event( 'scroll' ) );
  72. testPanelAttach( spy );
  73. } );
  74. it( 'should calculate panel position on resize event event', () => {
  75. setData( document, '[<image src=""></image>]' );
  76. panel.attach();
  77. const spy = sinon.spy( panel, 'attachTo' );
  78. global.window.dispatchEvent( new Event( 'resize' ) );
  79. testPanelAttach( spy );
  80. } );
  81. it( 'should hide panel on detach', () => {
  82. setData( document, '[<image src=""></image>]' );
  83. panel.attach();
  84. const spy = sinon.spy( panel, 'hide' );
  85. panel.detach();
  86. sinon.assert.calledOnce( spy );
  87. } );
  88. it( 'should not reposition panel after detaching', () => {
  89. setData( document, '[<image src=""></image>]' );
  90. panel.attach();
  91. const spy = sinon.spy( panel, 'attachTo' );
  92. panel.detach();
  93. global.window.dispatchEvent( new Event( 'resize' ) );
  94. global.window.dispatchEvent( new Event( 'scroll' ) );
  95. sinon.assert.notCalled( spy );
  96. } );
  97. // Tests if panel.attachTo() was called correctly.
  98. function testPanelAttach( spy ) {
  99. sinon.assert.calledOnce( spy );
  100. const options = spy.firstCall.args[ 0 ];
  101. // Check if proper target element was used.
  102. expect( options.target.tagName.toLowerCase() ).to.equal( 'figure' );
  103. // Check if correct positions are used.
  104. const [ north, south ] = options.positions;
  105. expect( north ).to.equal( BalloonPanelView.defaultPositions.northArrowSouth );
  106. expect( south ).to.equal( BalloonPanelView.defaultPositions.southArrowNorth );
  107. }
  108. } );