contextualballoon.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 */
  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 execute BalloonPanel pin and attachTo methods
  24. // it's enough to check if was called with the proper data.
  25. sinon.stub( balloon.view, 'attachTo', () => {} );
  26. sinon.stub( balloon.view, 'pin', () => {} );
  27. viewA = new View();
  28. viewB = new View();
  29. // Add viewA to the pane and init viewB.
  30. balloon.add( {
  31. view: viewA,
  32. position: { target: 'fake' }
  33. } );
  34. viewB.init();
  35. } );
  36. } );
  37. afterEach( () => {
  38. editor.destroy();
  39. } );
  40. it( 'should create a plugin instance', () => {
  41. expect( balloon ).to.instanceof( Plugin );
  42. expect( balloon ).to.instanceof( ContextualBalloon );
  43. } );
  44. describe( 'pluginName', () => {
  45. it( 'should return plugin by name', () => {
  46. expect( editor.plugins.get( 'ContextualBalloon' ) ).to.equal( balloon );
  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 add view to the stack and display in balloon attached using given position options', () => {
  84. expect( balloon.view.content.length ).to.equal( 1 );
  85. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
  86. expect( balloon.view.pin.calledOnce ).to.true;
  87. expect( balloon.view.pin.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  88. } );
  89. it( 'should pin balloon to the target element', () => {
  90. sinon.assert.calledOnce( balloon.view.pin );
  91. } );
  92. it( 'should throw an error when try to add the same view more than once', () => {
  93. expect( () => {
  94. balloon.add( {
  95. view: viewA,
  96. position: { target: 'fake' }
  97. } );
  98. } ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
  99. } );
  100. it( 'should add multiple views to he stack and display last one', () => {
  101. balloon.add( {
  102. view: viewB,
  103. position: { target: 'fake' }
  104. } );
  105. expect( balloon.view.content.length ).to.equal( 1 );
  106. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  107. } );
  108. it( 'should keep balloon at the same position after adding next view', () => {
  109. balloon.add( {
  110. view: viewB,
  111. position: { target: 'other' }
  112. } );
  113. expect( balloon.view.pin.calledTwice ).to.true;
  114. expect( balloon.view.pin.firstCall.args[ 0 ] ).to.deep.equal( {
  115. target: 'fake'
  116. } );
  117. expect( balloon.view.pin.secondCall.args[ 0 ] ).to.deep.equal( {
  118. target: 'fake'
  119. } );
  120. } );
  121. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  122. const view = new View();
  123. balloon.add( {
  124. view,
  125. position: { target: 'fake' },
  126. balloonClassName: 'foo'
  127. } );
  128. expect( balloon.view.className ).to.equal( 'foo' );
  129. balloon.add( {
  130. view: viewB,
  131. position: { target: 'fake' },
  132. balloonClassName: 'bar'
  133. } );
  134. expect( balloon.view.className ).to.equal( 'bar' );
  135. } );
  136. } );
  137. describe( 'visibleView', () => {
  138. it( 'should return data of currently visible view', () => {
  139. expect( balloon.visibleView ).to.equal( viewA );
  140. } );
  141. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  142. balloon.add( {
  143. view: viewB,
  144. position: { target: 'fake' }
  145. } );
  146. expect( balloon.visibleView ).to.equal( viewB );
  147. } );
  148. it( 'should return `null` when the stack is empty', () => {
  149. balloon.remove( viewA );
  150. expect( balloon.visibleView ).to.null;
  151. } );
  152. } );
  153. describe( 'remove()', () => {
  154. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  155. balloon.view.isVisible = true;
  156. balloon.remove( viewA );
  157. expect( balloon.visibleView ).to.null;
  158. expect( balloon.view.isVisible ).to.false;
  159. } );
  160. it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
  161. balloon.add( {
  162. view: viewB,
  163. position: { target: 'fake' }
  164. } );
  165. balloon.remove( viewB );
  166. expect( balloon.visibleView ).to.equal( viewA );
  167. } );
  168. it( 'should remove given view from the stack when view is not visible', () => {
  169. balloon.add( {
  170. view: viewB,
  171. position: { target: 'fake' }
  172. } );
  173. balloon.remove( viewA );
  174. expect( balloon.visibleView ).to.equal( viewB );
  175. } );
  176. it( 'should throw an error when there is no given view in the stack', () => {
  177. expect( () => {
  178. balloon.remove( viewB );
  179. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  180. } );
  181. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  182. const view = new View();
  183. balloon.add( {
  184. view,
  185. position: { target: 'fake' },
  186. balloonClassName: 'foo'
  187. } );
  188. balloon.add( {
  189. view: viewB,
  190. position: { target: 'fake' },
  191. balloonClassName: 'bar'
  192. } );
  193. balloon.remove( viewB );
  194. expect( balloon.view.className ).to.equal( 'foo' );
  195. } );
  196. } );
  197. describe( 'updatePosition()', () => {
  198. it( 'should attach balloon to the target using position option from the first view in the stack', () => {
  199. balloon.add( {
  200. view: viewB,
  201. position: {
  202. target: 'other'
  203. }
  204. } );
  205. balloon.view.attachTo.reset();
  206. balloon.updatePosition();
  207. expect( balloon.view.attachTo.calledOnce );
  208. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  209. } );
  210. it( 'should attach balloon to the target using new position options', () => {
  211. balloon.view.attachTo.reset();
  212. balloon.updatePosition( { target: 'new' } );
  213. expect( balloon.view.attachTo.calledOnce );
  214. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'new' } );
  215. balloon.updatePosition();
  216. expect( balloon.view.attachTo.calledTwice );
  217. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'new' } );
  218. } );
  219. it( 'should throw an error when there is no given view in the stack', () => {
  220. expect( () => {
  221. balloon.remove( viewB );
  222. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  223. } );
  224. } );
  225. describe( 'destroy()', () => {
  226. it( 'can be called multiple times', () => {
  227. expect( () => {
  228. balloon.destroy();
  229. balloon.destroy();
  230. } ).to.not.throw();
  231. } );
  232. it( 'should not touch the DOM', () => {
  233. balloon.destroy();
  234. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.not.equal( -1 );
  235. } );
  236. } );
  237. } );