8
0

contextualballoon.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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( 'add()', () => {
  24. it( 'should add panel to the stack and display in balloon', () => {
  25. balloon.add( {
  26. view: viewA,
  27. position: { target: 'fake' }
  28. } );
  29. expect( balloon.view.content.length ).to.equal( 1 );
  30. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
  31. expect( balloon.view.attachTo.calledOnce ).to.true;
  32. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  33. } );
  34. it( 'should throw an error when try to add the same panel more than once', () => {
  35. balloon.add( {
  36. view: viewA,
  37. position: { target: 'fake' }
  38. } );
  39. expect( () => {
  40. balloon.add( {
  41. view: viewA,
  42. position: { target: 'fake' }
  43. } );
  44. } ).to.throw( CKEditorError, /^contextualballoon-add-panel-exist/ );
  45. } );
  46. it( 'should add multiple panels to he stack and display last one', () => {
  47. balloon.add( {
  48. view: viewA,
  49. position: { target: 'fake' }
  50. } );
  51. balloon.add( {
  52. view: viewB,
  53. position: { target: 'fake' }
  54. } );
  55. expect( balloon.view.content.length ).to.equal( 1 );
  56. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  57. } );
  58. it( 'should add multiple panels to the stack and keep balloon in the same position', () => {
  59. balloon.add( {
  60. view: viewA,
  61. position: { target: 'fake', foo: 'bar' }
  62. } );
  63. balloon.add( {
  64. view: viewB,
  65. position: { target: 'fake', bar: 'biz' }
  66. } );
  67. expect( balloon.view.attachTo.calledTwice ).to.true;
  68. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  69. target: 'fake',
  70. foo: 'bar'
  71. } );
  72. expect( balloon.view.attachTo.secondCall.args[ 0 ] ).to.deep.equal( {
  73. target: 'fake',
  74. foo: 'bar'
  75. } );
  76. } );
  77. } );
  78. describe( 'visible', () => {
  79. it( 'should return data of currently visible panel', () => {
  80. balloon.add( {
  81. view: viewA,
  82. position: { target: 'fake' }
  83. } );
  84. expect( balloon.visible ).to.deep.equal( {
  85. view: viewA,
  86. position: { target: 'fake' }
  87. } );
  88. } );
  89. it( 'should return data of currently visible panel when there is more than one in the stack', () => {
  90. balloon.add( {
  91. view: viewA,
  92. position: { target: 'fake' }
  93. } );
  94. balloon.add( {
  95. view: viewB,
  96. position: { target: 'fake' }
  97. } );
  98. expect( balloon.visible ).to.deep.equal( {
  99. view: viewB,
  100. position: { target: 'fake' }
  101. } );
  102. } );
  103. it( 'should return `null` when the stack is empty', () => {
  104. expect( balloon.visible ).to.null;
  105. } );
  106. } );
  107. describe( 'remove()', () => {
  108. it( 'should remove panel of given view and hide balloon when there is no other panel to display', () => {
  109. balloon.add( {
  110. view: viewA,
  111. position: { target: 'fake' }
  112. } );
  113. balloon.remove( viewA );
  114. expect( balloon.visible ).to.null;
  115. } );
  116. it( 'should remove panel of given view and set previous in the stack as visible when removed panel was visible', () => {
  117. balloon.add( {
  118. view: viewA,
  119. position: { target: 'fake' }
  120. } );
  121. balloon.add( {
  122. view: viewB,
  123. position: { target: 'fake' }
  124. } );
  125. balloon.remove( viewB );
  126. expect( balloon.visible ).to.deep.equal( {
  127. view: viewA,
  128. position: { target: 'fake' }
  129. } );
  130. } );
  131. it( 'should remove given panel from the stack when panel is not visible', () => {
  132. balloon.add( {
  133. view: viewA,
  134. position: { target: 'fake' }
  135. } );
  136. balloon.add( {
  137. view: viewB,
  138. position: { target: 'fake' }
  139. } );
  140. balloon.remove( viewA );
  141. expect( balloon.visible ).to.deep.equal( {
  142. view: viewB,
  143. position: { target: 'fake' }
  144. } );
  145. } );
  146. it( 'should throw an error when there is no panel of given view in the stack', () => {
  147. expect( () => {
  148. balloon.remove( viewA );
  149. } ).to.throw( CKEditorError, /^contextualballoon-remove-panel-not-exist/ );
  150. } );
  151. } );
  152. describe( 'updatePosition()', () => {
  153. it( 'should attach balloon to the target using the same position options as currently set', () => {
  154. balloon.add( {
  155. view: viewA,
  156. position: { target: 'fake' }
  157. } );
  158. balloon.view.attachTo.reset();
  159. balloon.updatePosition();
  160. expect( balloon.view.attachTo.calledOnce );
  161. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  162. } );
  163. it( 'should attach balloon to the target using the same position options as currently set when there is more than one panel', () => {
  164. balloon.add( {
  165. view: viewA,
  166. position: {
  167. target: 'fake',
  168. foo: 'bar'
  169. }
  170. } );
  171. balloon.add( {
  172. view: viewB,
  173. position: {
  174. target: 'fake',
  175. bar: 'biz'
  176. }
  177. } );
  178. balloon.view.attachTo.reset();
  179. balloon.updatePosition();
  180. expect( balloon.view.attachTo.calledOnce );
  181. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  182. target: 'fake',
  183. foo: 'bar'
  184. } );
  185. } );
  186. it( 'should remove given panel from the stack when panel is not visible', () => {
  187. balloon.add( {
  188. view: viewA,
  189. position: { target: 'fake' }
  190. } );
  191. balloon.add( {
  192. view: viewB,
  193. position: { target: 'fake' }
  194. } );
  195. balloon.remove( viewA );
  196. expect( balloon.visible ).to.deep.equal( {
  197. view: viewB,
  198. position: { target: 'fake' }
  199. } );
  200. } );
  201. it( 'should throw an error when there is no panel of given view in the stack', () => {
  202. expect( () => {
  203. balloon.remove( viewA );
  204. } ).to.throw( CKEditorError, /^contextualballoon-remove-panel-not-exist/ );
  205. } );
  206. } );
  207. } );
  208. class ViewA extends View {
  209. constructor( locale ) {
  210. super( locale );
  211. this.template = new Template( {
  212. tag: 'div'
  213. } );
  214. }
  215. }
  216. class ViewB extends View {
  217. constructor( locale ) {
  218. super( locale );
  219. this.template = new Template( {
  220. tag: 'div'
  221. } );
  222. }
  223. }