contextualballoon.js 7.3 KB

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