contextualballoon.js 9.3 KB

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