contextualballoon.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @license Copyright (c) 2003-2016, 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 ContextualBalloon from '../../../src/panel/balloon/contextualballoon';
  7. import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
  8. import View from '../../../src/view';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. /* global document, Event, setTimeout */
  12. describe( 'ContextualBalloon', () => {
  13. let editor, editorElement, balloon, viewA, viewB;
  14. beforeEach( () => {
  15. editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. return ClassicTestEditor.create( editorElement, {
  18. plugins: [ ContextualBalloon ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. balloon = editor.plugins.get( ContextualBalloon );
  23. // We don't need to test BalloonPanelView attachTo and pin methods it's enough to check if was called with proper data.
  24. sinon.stub( balloon.view, 'attachTo', () => {} );
  25. sinon.stub( balloon.view, 'pin', () => {} );
  26. viewA = new View();
  27. viewB = new View();
  28. // Add viewA to the pane and init viewB.
  29. return Promise.all( [
  30. balloon.add( {
  31. view: viewA,
  32. position: { target: 'fake' }
  33. } ),
  34. viewB.init(),
  35. ] );
  36. } );
  37. } );
  38. afterEach( () => {
  39. editor.destroy();
  40. } );
  41. it( 'should be a plugin instance', () => {
  42. expect( balloon ).to.instanceof( Plugin );
  43. } );
  44. describe( 'pluginName', () => {
  45. it( 'should return plugin by name', () => {
  46. expect( editor.plugins.get( 'contextualballoon' ) ).to.instanceof( ContextualBalloon );
  47. } );
  48. } );
  49. describe( 'init()', () => {
  50. it( 'should create a plugin instance with properties', () => {
  51. expect( balloon.view ).to.instanceof( BalloonPanelView );
  52. } );
  53. it( 'should add balloon panel view to editor `body` collection', () => {
  54. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.above( -1 );
  55. } );
  56. it( 'should register balloon panel element in editor.ui#focusTracker', () => {
  57. editor.ui.focusTracker.isfocused = false;
  58. balloon.add( {
  59. view: viewB,
  60. position: { target: 'fake' }
  61. } );
  62. balloon.view.element.dispatchEvent( new Event( 'focus' ) );
  63. expect( editor.ui.focusTracker.isFocused ).to.true;
  64. } );
  65. } );
  66. describe( 'hasView()', () => {
  67. it( 'should return true when given view is in stack', () => {
  68. expect( balloon.hasView( viewA ) ).to.true;
  69. } );
  70. it( 'should return true when given view is in stack but is not visible', () => {
  71. balloon.add( {
  72. view: viewB,
  73. position: { target: 'fake' }
  74. } );
  75. expect( balloon.visibleView ).to.equal( viewB );
  76. expect( balloon.hasView( viewA ) ).to.true;
  77. } );
  78. it( 'should return false when given view is not in stack', () => {
  79. expect( balloon.hasView( viewB ) ).to.false;
  80. } );
  81. } );
  82. describe( 'add()', () => {
  83. it( 'should return promise resolved when view is ready', ( done ) => {
  84. const clock = sinon.useFakeTimers();
  85. const view = {
  86. init: () => {
  87. return new Promise( ( resolve ) => {
  88. setTimeout( () => {
  89. resolve();
  90. }, 10 );
  91. } );
  92. },
  93. destroy: () => {}
  94. };
  95. const result = balloon.add( {
  96. view: view,
  97. position: { target: 'fake' }
  98. } );
  99. expect( result ).to.instanceof( Promise );
  100. result.then( done );
  101. clock.tick( 11 );
  102. clock.restore();
  103. } );
  104. it( 'should add view to the stack and display in balloon attached using given position options', () => {
  105. expect( balloon.view.content.length ).to.equal( 1 );
  106. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
  107. expect( balloon.view.pin.calledOnce ).to.true;
  108. expect( balloon.view.pin.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  109. } );
  110. it( 'should pin balloon to the target element', () => {
  111. sinon.assert.calledOnce( balloon.view.pin );
  112. } );
  113. it( 'should throw an error when try to add the same view more than once', () => {
  114. expect( () => {
  115. balloon.add( {
  116. view: viewA,
  117. position: { target: 'fake' }
  118. } );
  119. } ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
  120. } );
  121. it( 'should add multiple views to he stack and display last one', () => {
  122. balloon.add( {
  123. view: viewB,
  124. position: { target: 'fake' }
  125. } );
  126. expect( balloon.view.content.length ).to.equal( 1 );
  127. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  128. } );
  129. it( 'should keep balloon at the same position after adding next view', () => {
  130. return balloon.add( {
  131. view: viewB,
  132. position: { target: 'other' }
  133. } )
  134. .then( () => {
  135. expect( balloon.view.pin.calledTwice ).to.true;
  136. expect( balloon.view.pin.firstCall.args[ 0 ] ).to.deep.equal( {
  137. target: 'fake'
  138. } );
  139. expect( balloon.view.pin.secondCall.args[ 0 ] ).to.deep.equal( {
  140. target: 'fake'
  141. } );
  142. } );
  143. } );
  144. } );
  145. describe( 'visibleView', () => {
  146. it( 'should return data of currently visible view', () => {
  147. expect( balloon.visibleView ).to.equal( viewA );
  148. } );
  149. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  150. balloon.add( {
  151. view: viewB,
  152. position: { target: 'fake' }
  153. } );
  154. expect( balloon.visibleView ).to.equal( viewB );
  155. } );
  156. it( 'should return `null` when the stack is empty', () => {
  157. balloon.remove( viewA );
  158. expect( balloon.visibleView ).to.null;
  159. } );
  160. } );
  161. describe( 'remove()', () => {
  162. it( 'should return promise', () => {
  163. expect( balloon.remove( viewA ) ).to.instanceof( Promise );
  164. } );
  165. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  166. balloon.view.isVisible = true;
  167. balloon.remove( viewA );
  168. expect( balloon.visibleView ).to.null;
  169. expect( balloon.view.isVisible ).to.false;
  170. } );
  171. it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
  172. balloon.add( {
  173. view: viewB,
  174. position: { target: 'fake' }
  175. } );
  176. balloon.remove( viewB );
  177. expect( balloon.visibleView ).to.equal( viewA );
  178. } );
  179. it( 'should wait for init of preceding view when was is not ready', ( done ) => {
  180. const clock = sinon.useFakeTimers();
  181. const view = {
  182. init: () => {
  183. return new Promise( ( resolve ) => {
  184. setTimeout( () => {
  185. resolve();
  186. }, 10 );
  187. } );
  188. },
  189. destroy: () => {}
  190. };
  191. balloon.add( {
  192. view: view,
  193. position: { target: 'fake' }
  194. } );
  195. balloon.add( {
  196. view: viewB,
  197. position: { target: 'fake' }
  198. } );
  199. balloon.remove( viewB ).then( done );
  200. clock.tick( 11 );
  201. clock.restore();
  202. } );
  203. it( 'should remove given view from the stack when view is not visible', () => {
  204. balloon.add( {
  205. view: viewB,
  206. position: { target: 'fake' }
  207. } );
  208. balloon.remove( viewA );
  209. expect( balloon.visibleView ).to.equal( viewB );
  210. } );
  211. it( 'should throw an error when there is no given view in the stack', () => {
  212. expect( () => {
  213. balloon.remove( viewB );
  214. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  215. } );
  216. } );
  217. describe( 'updatePosition()', () => {
  218. it( 'should attach balloon to the target using the same position options as first view in the stack', () => {
  219. balloon.add( {
  220. view: viewB,
  221. position: {
  222. target: 'other'
  223. }
  224. } );
  225. balloon.view.attachTo.reset();
  226. balloon.updatePosition();
  227. expect( balloon.view.attachTo.calledOnce );
  228. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  229. target: 'fake'
  230. } );
  231. } );
  232. it( 'should throw an error when there is no given view in the stack', () => {
  233. expect( () => {
  234. balloon.remove( viewB );
  235. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  236. } );
  237. } );
  238. describe( 'destroy()', () => {
  239. it( 'should remove balloon panel view from editor body collection and clear stack', () => {
  240. balloon.destroy();
  241. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.equal( -1 );
  242. expect( balloon.visibleView ).to.null;
  243. } );
  244. } );
  245. } );