8
0

contextualballoon.js 8.6 KB

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