contextualballoon.js 8.2 KB

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