8
0

contextualballoon.js 7.8 KB

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