contextualballoon.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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, 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. } );
  57. describe( 'hasView()', () => {
  58. it( 'should return true when given view is in stack', () => {
  59. expect( balloon.hasView( viewA ) ).to.true;
  60. } );
  61. it( 'should return true when given view is in stack but is not visible', () => {
  62. balloon.add( {
  63. view: viewB,
  64. position: { target: 'fake' }
  65. } );
  66. expect( balloon.visibleView ).to.equal( viewB );
  67. expect( balloon.hasView( viewA ) ).to.true;
  68. } );
  69. it( 'should return false when given view is not in stack', () => {
  70. expect( balloon.hasView( viewB ) ).to.false;
  71. } );
  72. } );
  73. describe( 'add()', () => {
  74. it( 'should return promise resolved when view is ready', ( done ) => {
  75. const clock = sinon.useFakeTimers();
  76. const view = {
  77. init: () => {
  78. return new Promise( ( resolve ) => {
  79. setTimeout( () => {
  80. resolve();
  81. }, 10 );
  82. } );
  83. },
  84. destroy: () => {}
  85. };
  86. const result = balloon.add( {
  87. view: view,
  88. position: { target: 'fake' }
  89. } );
  90. expect( result ).to.instanceof( Promise );
  91. result.then( done );
  92. clock.tick( 11 );
  93. clock.restore();
  94. } );
  95. it( 'should add view to the stack and display in balloon attached using given position options', () => {
  96. expect( balloon.view.content.length ).to.equal( 1 );
  97. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
  98. expect( balloon.view.pin.calledOnce ).to.true;
  99. expect( balloon.view.pin.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  100. } );
  101. it( 'should pin balloon to the target element', () => {
  102. sinon.assert.calledOnce( balloon.view.pin );
  103. } );
  104. it( 'should throw an error when try to add the same view more than once', () => {
  105. expect( () => {
  106. balloon.add( {
  107. view: viewA,
  108. position: { target: 'fake' }
  109. } );
  110. } ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
  111. } );
  112. it( 'should add multiple views to he stack and display last one', () => {
  113. balloon.add( {
  114. view: viewB,
  115. position: { target: 'fake' }
  116. } );
  117. expect( balloon.view.content.length ).to.equal( 1 );
  118. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  119. } );
  120. it( 'should keep balloon at the same position after adding next view', () => {
  121. return balloon.add( {
  122. view: viewB,
  123. position: { target: 'other' }
  124. } )
  125. .then( () => {
  126. expect( balloon.view.pin.calledTwice ).to.true;
  127. expect( balloon.view.pin.firstCall.args[ 0 ] ).to.deep.equal( {
  128. target: 'fake'
  129. } );
  130. expect( balloon.view.pin.secondCall.args[ 0 ] ).to.deep.equal( {
  131. target: 'fake'
  132. } );
  133. } );
  134. } );
  135. } );
  136. describe( 'visibleView', () => {
  137. it( 'should return data of currently visible view', () => {
  138. expect( balloon.visibleView ).to.equal( viewA );
  139. } );
  140. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  141. balloon.add( {
  142. view: viewB,
  143. position: { target: 'fake' }
  144. } );
  145. expect( balloon.visibleView ).to.equal( viewB );
  146. } );
  147. it( 'should return `null` when the stack is empty', () => {
  148. balloon.remove( viewA );
  149. expect( balloon.visibleView ).to.null;
  150. } );
  151. } );
  152. describe( 'remove()', () => {
  153. it( 'should return promise', () => {
  154. expect( balloon.remove( viewA ) ).to.instanceof( Promise );
  155. } );
  156. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  157. balloon.view.isVisible = true;
  158. balloon.remove( viewA );
  159. expect( balloon.visibleView ).to.null;
  160. expect( balloon.view.isVisible ).to.false;
  161. } );
  162. it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
  163. balloon.add( {
  164. view: viewB,
  165. position: { target: 'fake' }
  166. } );
  167. balloon.remove( viewB );
  168. expect( balloon.visibleView ).to.equal( viewA );
  169. } );
  170. it( 'should wait for init of preceding view when was is not ready', ( done ) => {
  171. const clock = sinon.useFakeTimers();
  172. const view = {
  173. init: () => {
  174. return new Promise( ( resolve ) => {
  175. setTimeout( () => {
  176. resolve();
  177. }, 10 );
  178. } );
  179. },
  180. destroy: () => {}
  181. };
  182. balloon.add( {
  183. view: view,
  184. position: { target: 'fake' }
  185. } );
  186. balloon.add( {
  187. view: viewB,
  188. position: { target: 'fake' }
  189. } );
  190. balloon.remove( viewB ).then( done );
  191. clock.tick( 11 );
  192. clock.restore();
  193. } );
  194. it( 'should remove given view from the stack when view is not visible', () => {
  195. balloon.add( {
  196. view: viewB,
  197. position: { target: 'fake' }
  198. } );
  199. balloon.remove( viewA );
  200. expect( balloon.visibleView ).to.equal( viewB );
  201. } );
  202. it( 'should throw an error when there is no given view in the stack', () => {
  203. expect( () => {
  204. balloon.remove( viewB );
  205. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  206. } );
  207. } );
  208. describe( 'updatePosition()', () => {
  209. it( 'should attach balloon to the target using the same position options as first view in the stack', () => {
  210. balloon.add( {
  211. view: viewB,
  212. position: {
  213. target: 'other'
  214. }
  215. } );
  216. balloon.view.attachTo.reset();
  217. balloon.updatePosition();
  218. expect( balloon.view.attachTo.calledOnce );
  219. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  220. target: 'fake'
  221. } );
  222. } );
  223. it( 'should throw an error when there is no given view in the stack', () => {
  224. expect( () => {
  225. balloon.remove( viewB );
  226. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  227. } );
  228. } );
  229. describe( 'destroy()', () => {
  230. it( 'should remove balloon panel view from editor body collection and clear stack', () => {
  231. balloon.destroy();
  232. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.equal( -1 );
  233. expect( balloon.visibleView ).to.null;
  234. } );
  235. } );
  236. } );