8
0

contextualballoon.js 8.4 KB

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