contextualballoon.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 Template from '../src/template';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  12. /* global document, setTimeout */
  13. describe( 'ContextualBalloon', () => {
  14. let editor, editorElement, balloon, viewA, viewB;
  15. beforeEach( () => {
  16. editorElement = document.createElement( 'div' );
  17. document.body.appendChild( editorElement );
  18. return ClassicTestEditor.create( editorElement, {
  19. plugins: [ ContextualBalloon ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. balloon = editor.plugins.get( ContextualBalloon );
  24. // We don't need to test attachTo method of BalloonPanel it's enough to check if was called with proper data.
  25. sinon.stub( balloon.view, 'attachTo', () => {} );
  26. viewA = new ViewA();
  27. viewB = new ViewB();
  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 = new LongInitView();
  77. const result = balloon.add( {
  78. view: view,
  79. position: { target: 'fake' }
  80. } );
  81. expect( result ).to.instanceof( Promise );
  82. result.then( done );
  83. clock.tick( 11 );
  84. } );
  85. it( 'should add view to the stack and display in balloon attached using given position options', () => {
  86. expect( balloon.view.content.length ).to.equal( 1 );
  87. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
  88. expect( balloon.view.attachTo.calledOnce ).to.true;
  89. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  90. } );
  91. it( 'should throw an error when try to add the same view more than once', () => {
  92. expect( () => {
  93. balloon.add( {
  94. view: viewA,
  95. position: { target: 'fake' }
  96. } );
  97. } ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
  98. } );
  99. it( 'should add multiple views to he stack and display last one', () => {
  100. balloon.add( {
  101. view: viewB,
  102. position: { target: 'fake' }
  103. } );
  104. expect( balloon.view.content.length ).to.equal( 1 );
  105. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  106. } );
  107. it( 'should keep balloon at the same position after adding next view', () => {
  108. return balloon.add( {
  109. view: viewB,
  110. position: { target: 'other' }
  111. } )
  112. .then( () => {
  113. expect( balloon.view.attachTo.calledTwice ).to.true;
  114. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  115. target: 'fake'
  116. } );
  117. expect( balloon.view.attachTo.secondCall.args[ 0 ] ).to.deep.equal( {
  118. target: 'fake'
  119. } );
  120. } );
  121. } );
  122. } );
  123. describe( 'visibleView', () => {
  124. it( 'should return data of currently visible view', () => {
  125. expect( balloon.visibleView ).to.equal( viewA );
  126. } );
  127. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  128. balloon.add( {
  129. view: viewB,
  130. position: { target: 'fake' }
  131. } );
  132. expect( balloon.visibleView ).to.equal( viewB );
  133. } );
  134. it( 'should return `null` when the stack is empty', () => {
  135. balloon.remove( viewA );
  136. expect( balloon.visibleView ).to.null;
  137. } );
  138. } );
  139. describe( 'remove()', () => {
  140. it( 'should return promise', () => {
  141. expect( balloon.remove( viewA ) ).to.instanceof( Promise );
  142. } );
  143. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  144. balloon.view.isVisible = true;
  145. balloon.remove( viewA );
  146. expect( balloon.visibleView ).to.null;
  147. expect( balloon.view.isVisible ).to.false;
  148. } );
  149. it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
  150. balloon.add( {
  151. view: viewB,
  152. position: { target: 'fake' }
  153. } );
  154. balloon.remove( viewB );
  155. expect( balloon.visibleView ).to.equal( viewA );
  156. } );
  157. it( 'should wait for init of preceding view when was is not ready', ( done ) => {
  158. const clock = sinon.useFakeTimers();
  159. const view = new LongInitView();
  160. balloon.add( {
  161. view: view,
  162. position: { target: 'fake' }
  163. } );
  164. balloon.add( {
  165. view: viewB,
  166. position: { target: 'fake' }
  167. } );
  168. balloon.remove( viewB ).then( done );
  169. clock.tick( 11 );
  170. } );
  171. it( 'should remove given view from the stack when view is not visible', () => {
  172. balloon.add( {
  173. view: viewB,
  174. position: { target: 'fake' }
  175. } );
  176. balloon.remove( viewA );
  177. expect( balloon.visibleView ).to.equal( viewB );
  178. } );
  179. it( 'should throw an error when there is no given view in the stack', () => {
  180. expect( () => {
  181. balloon.remove( viewB );
  182. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  183. } );
  184. } );
  185. describe( 'updatePosition()', () => {
  186. it( 'should attach balloon to the target using the same position options as currently set', () => {
  187. balloon.view.attachTo.reset();
  188. balloon.updatePosition();
  189. expect( balloon.view.attachTo.calledOnce );
  190. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  191. } );
  192. it( 'should attach balloon to the target using the same position options as currently set when there is more than one view', () => {
  193. balloon.add( {
  194. view: viewB,
  195. position: {
  196. target: 'other'
  197. }
  198. } );
  199. balloon.view.attachTo.reset();
  200. balloon.updatePosition();
  201. expect( balloon.view.attachTo.calledOnce );
  202. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  203. target: 'fake'
  204. } );
  205. } );
  206. it( 'should throw an error when there is no given view in the stack', () => {
  207. expect( () => {
  208. balloon.remove( viewB );
  209. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  210. } );
  211. } );
  212. describe( 'destroy()', () => {
  213. it( 'should balloon panel remove view from editor body collection', () => {
  214. balloon.destroy();
  215. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.equal( -1 );
  216. } );
  217. } );
  218. } );
  219. class GenericView extends View {
  220. constructor( locale ) {
  221. super( locale );
  222. this.template = new Template( { tag: 'div' } );
  223. }
  224. }
  225. class ViewA extends GenericView {}
  226. class ViewB extends GenericView {}
  227. class LongInitView extends GenericView {
  228. init() {
  229. setTimeout( () => super.init(), 10 );
  230. }
  231. }