contextualballoon.js 8.7 KB

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