8
0

imageballoonpanelview.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. panel.attach();
  64. testPanelAttach( spy );
  65. } );
  66. it( 'should calculate panel position on scroll event', () => {
  67. panel.attach();
  68. const spy = sinon.spy( panel, 'attachTo' );
  69. global.window.dispatchEvent( new Event( 'scroll' ) );
  70. testPanelAttach( spy );
  71. } );
  72. it( 'should calculate panel position on resize event event', () => {
  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. panel.attach();
  80. const spy = sinon.spy( panel, 'hide' );
  81. panel.detach();
  82. sinon.assert.calledOnce( spy );
  83. } );
  84. it( 'should not reposition panel after detaching', () => {
  85. panel.attach();
  86. const spy = sinon.spy( panel, 'attachTo' );
  87. panel.detach();
  88. global.window.dispatchEvent( new Event( 'resize' ) );
  89. global.window.dispatchEvent( new Event( 'scroll' ) );
  90. sinon.assert.notCalled( spy );
  91. } );
  92. // Tests if panel.attachTo() was called correctly.
  93. function testPanelAttach( spy ) {
  94. const domRange = editor.editing.view.domConverter.viewRangeToDom( editingView.selection.getFirstRange() );
  95. sinon.assert.calledOnce( spy );
  96. const options = spy.firstCall.args[ 0 ];
  97. // Check if proper range was used.
  98. expect( options.target.startContainer ).to.equal( domRange.startContainer );
  99. expect( options.target.startOffset ).to.equal( domRange.startOffset );
  100. expect( options.target.endContainer ).to.equal( domRange.endContainer );
  101. expect( options.target.endOffset ).to.equal( domRange.endOffset );
  102. // Check if correct positions are used.
  103. const [ north, south ] = options.positions;
  104. expect( north ).to.equal( BalloonPanelView.defaultPositions.northArrowSouth );
  105. expect( south ).to.equal( BalloonPanelView.defaultPositions.southArrowNorth );
  106. }
  107. } );