contextualballoon.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ContextualBalloon from '../src/contextualballoon';
  6. import BalloonPanelView from '../src/panel/balloon/balloonpanelview';
  7. import View from '../src/view';
  8. import Template from '../src/template';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. describe( 'ContextualBalloon', () => {
  11. let balloon, viewA, viewB;
  12. beforeEach( () => {
  13. balloon = new ContextualBalloon();
  14. balloon.view = new BalloonPanelView();
  15. viewA = new ViewA();
  16. viewB = new ViewB();
  17. // We don't need to test attachTo method of BalloonPanel it's enough to check if was called with proper data.
  18. sinon.stub( balloon.view, 'attachTo', () => {} );
  19. } );
  20. afterEach( () => {
  21. balloon.view.attachTo.restore();
  22. } );
  23. describe( 'isPanelInStack()', () => {
  24. it( 'should return true when panel of given view is in stack', () => {
  25. balloon.add( {
  26. view: viewA,
  27. position: { target: 'fake' }
  28. } );
  29. expect( balloon.isPanelInStack( viewA ) ).to.true;
  30. } );
  31. it( 'should return true when panel of given view is in stack but is not visible', () => {
  32. balloon.add( {
  33. view: viewA,
  34. position: { target: 'fake' }
  35. } );
  36. balloon.add( {
  37. view: viewB,
  38. position: { target: 'fake' }
  39. } );
  40. expect( balloon.visible.view === viewB ).to.true;
  41. expect( balloon.isPanelInStack( viewA ) ).to.true;
  42. } );
  43. it( 'should return false when panel of given view is not in stack', () => {
  44. expect( balloon.isPanelInStack( viewA ) ).to.false;
  45. } );
  46. } );
  47. describe( 'add()', () => {
  48. it( 'should add panel to the stack and display in balloon', () => {
  49. balloon.add( {
  50. view: viewA,
  51. position: { target: 'fake' }
  52. } );
  53. expect( balloon.view.content.length ).to.equal( 1 );
  54. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
  55. expect( balloon.view.attachTo.calledOnce ).to.true;
  56. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  57. } );
  58. it( 'should throw an error when try to add the same panel more than once', () => {
  59. balloon.add( {
  60. view: viewA,
  61. position: { target: 'fake' }
  62. } );
  63. expect( () => {
  64. balloon.add( {
  65. view: viewA,
  66. position: { target: 'fake' }
  67. } );
  68. } ).to.throw( CKEditorError, /^contextualballoon-add-panel-exist/ );
  69. } );
  70. it( 'should add multiple panels to he stack and display last one', () => {
  71. balloon.add( {
  72. view: viewA,
  73. position: { target: 'fake' }
  74. } );
  75. balloon.add( {
  76. view: viewB,
  77. position: { target: 'fake' }
  78. } );
  79. expect( balloon.view.content.length ).to.equal( 1 );
  80. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  81. } );
  82. it( 'should add multiple panels to the stack and keep balloon in the same position', () => {
  83. balloon.add( {
  84. view: viewA,
  85. position: { target: 'fake', foo: 'bar' }
  86. } );
  87. balloon.add( {
  88. view: viewB,
  89. position: { target: 'fake', bar: 'biz' }
  90. } );
  91. expect( balloon.view.attachTo.calledTwice ).to.true;
  92. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  93. target: 'fake',
  94. foo: 'bar'
  95. } );
  96. expect( balloon.view.attachTo.secondCall.args[ 0 ] ).to.deep.equal( {
  97. target: 'fake',
  98. foo: 'bar'
  99. } );
  100. } );
  101. } );
  102. describe( 'visible', () => {
  103. it( 'should return data of currently visible panel', () => {
  104. balloon.add( {
  105. view: viewA,
  106. position: { target: 'fake' }
  107. } );
  108. expect( balloon.visible ).to.deep.equal( {
  109. view: viewA,
  110. position: { target: 'fake' }
  111. } );
  112. } );
  113. it( 'should return data of currently visible panel when there is more than one in the stack', () => {
  114. balloon.add( {
  115. view: viewA,
  116. position: { target: 'fake' }
  117. } );
  118. balloon.add( {
  119. view: viewB,
  120. position: { target: 'fake' }
  121. } );
  122. expect( balloon.visible ).to.deep.equal( {
  123. view: viewB,
  124. position: { target: 'fake' }
  125. } );
  126. } );
  127. it( 'should return `null` when the stack is empty', () => {
  128. expect( balloon.visible ).to.null;
  129. } );
  130. } );
  131. describe( 'remove()', () => {
  132. it( 'should remove panel of given view and hide balloon when there is no other panel to display', () => {
  133. balloon.add( {
  134. view: viewA,
  135. position: { target: 'fake' }
  136. } );
  137. balloon.remove( viewA );
  138. expect( balloon.visible ).to.null;
  139. } );
  140. it( 'should remove panel of given view and set previous in the stack as visible when removed panel was visible', () => {
  141. balloon.add( {
  142. view: viewA,
  143. position: { target: 'fake' }
  144. } );
  145. balloon.add( {
  146. view: viewB,
  147. position: { target: 'fake' }
  148. } );
  149. balloon.remove( viewB );
  150. expect( balloon.visible ).to.deep.equal( {
  151. view: viewA,
  152. position: { target: 'fake' }
  153. } );
  154. } );
  155. it( 'should remove given panel from the stack when panel is not visible', () => {
  156. balloon.add( {
  157. view: viewA,
  158. position: { target: 'fake' }
  159. } );
  160. balloon.add( {
  161. view: viewB,
  162. position: { target: 'fake' }
  163. } );
  164. balloon.remove( viewA );
  165. expect( balloon.visible ).to.deep.equal( {
  166. view: viewB,
  167. position: { target: 'fake' }
  168. } );
  169. } );
  170. it( 'should throw an error when there is no panel of given view in the stack', () => {
  171. expect( () => {
  172. balloon.remove( viewA );
  173. } ).to.throw( CKEditorError, /^contextualballoon-remove-panel-not-exist/ );
  174. } );
  175. } );
  176. describe( 'updatePosition()', () => {
  177. it( 'should attach balloon to the target using the same position options as currently set', () => {
  178. balloon.add( {
  179. view: viewA,
  180. position: { target: 'fake' }
  181. } );
  182. balloon.view.attachTo.reset();
  183. balloon.updatePosition();
  184. expect( balloon.view.attachTo.calledOnce );
  185. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  186. } );
  187. it( 'should attach balloon to the target using the same position options as currently set when there is more than one panel', () => {
  188. balloon.add( {
  189. view: viewA,
  190. position: {
  191. target: 'fake',
  192. foo: 'bar'
  193. }
  194. } );
  195. balloon.add( {
  196. view: viewB,
  197. position: {
  198. target: 'fake',
  199. bar: 'biz'
  200. }
  201. } );
  202. balloon.view.attachTo.reset();
  203. balloon.updatePosition();
  204. expect( balloon.view.attachTo.calledOnce );
  205. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  206. target: 'fake',
  207. foo: 'bar'
  208. } );
  209. } );
  210. it( 'should remove given panel from the stack when panel is not visible', () => {
  211. balloon.add( {
  212. view: viewA,
  213. position: { target: 'fake' }
  214. } );
  215. balloon.add( {
  216. view: viewB,
  217. position: { target: 'fake' }
  218. } );
  219. balloon.remove( viewA );
  220. expect( balloon.visible ).to.deep.equal( {
  221. view: viewB,
  222. position: { target: 'fake' }
  223. } );
  224. } );
  225. it( 'should throw an error when there is no panel of given view in the stack', () => {
  226. expect( () => {
  227. balloon.remove( viewA );
  228. } ).to.throw( CKEditorError, /^contextualballoon-remove-panel-not-exist/ );
  229. } );
  230. } );
  231. } );
  232. class ViewA extends View {
  233. constructor( locale ) {
  234. super( locale );
  235. this.template = new Template( {
  236. tag: 'div'
  237. } );
  238. }
  239. }
  240. class ViewB extends View {
  241. constructor( locale ) {
  242. super( locale );
  243. this.template = new Template( {
  244. tag: 'div'
  245. } );
  246. }
  247. }