8
0

contextualballoon.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 BalloonPanelView attachTo and pin methods it's enough to check if was called with proper data.
  27. sinon.stub( balloon.view, 'attachTo', () => {} );
  28. sinon.stub( balloon.view, 'pin', () => {} );
  29. } );
  30. } );
  31. afterEach( () => {
  32. editor.destroy();
  33. } );
  34. it( 'should be a plugin instance', () => {
  35. expect( balloon ).to.instanceof( Plugin );
  36. } );
  37. describe( 'pluginName', () => {
  38. it( 'should return plugin by name', () => {
  39. expect( editor.plugins.get( 'contextualballoon' ) ).to.instanceof( ContextualBalloon );
  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.pin.calledOnce ).to.true;
  83. expect( balloon.view.pin.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  84. } );
  85. it( 'should pin balloon to the target element', () => {
  86. balloon.add( {
  87. view: viewA,
  88. position: { target: document.createElement( 'div' ) }
  89. } );
  90. sinon.assert.calledOnce( balloon.view.pin );
  91. } );
  92. it( 'should throw an error when try to add the same view more than once', () => {
  93. balloon.add( {
  94. view: viewA,
  95. position: { target: 'fake' }
  96. } );
  97. expect( () => {
  98. balloon.add( {
  99. view: viewA,
  100. position: { target: 'fake' }
  101. } );
  102. } ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
  103. } );
  104. it( 'should add multiple views to he stack and display last one', () => {
  105. balloon.add( {
  106. view: viewA,
  107. position: { target: 'fake' }
  108. } );
  109. balloon.add( {
  110. view: viewB,
  111. position: { target: 'fake' }
  112. } );
  113. expect( balloon.view.content.length ).to.equal( 1 );
  114. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  115. } );
  116. it( 'should add multiple views to the stack and keep balloon in the same position', () => {
  117. balloon.add( {
  118. view: viewA,
  119. position: { target: 'fake', foo: 'bar' }
  120. } );
  121. balloon.add( {
  122. view: viewB,
  123. position: { target: 'fake', bar: 'biz' }
  124. } );
  125. expect( balloon.view.pin.calledTwice ).to.true;
  126. expect( balloon.view.pin.firstCall.args[ 0 ] ).to.deep.equal( {
  127. target: 'fake',
  128. foo: 'bar'
  129. } );
  130. expect( balloon.view.pin.secondCall.args[ 0 ] ).to.deep.equal( {
  131. target: 'fake',
  132. foo: 'bar'
  133. } );
  134. } );
  135. } );
  136. describe( 'visibleView', () => {
  137. it( 'should return data of currently visible view', () => {
  138. balloon.add( {
  139. view: viewA,
  140. position: { target: 'fake' }
  141. } );
  142. expect( balloon.visibleView ).to.equal( viewA );
  143. } );
  144. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  145. balloon.add( {
  146. view: viewA,
  147. position: { target: 'fake' }
  148. } );
  149. balloon.add( {
  150. view: viewB,
  151. position: { target: 'fake' }
  152. } );
  153. expect( balloon.visibleView ).to.equal( viewB );
  154. } );
  155. it( 'should return `null` when the stack is empty', () => {
  156. expect( balloon.visibleView ).to.null;
  157. } );
  158. } );
  159. describe( 'remove()', () => {
  160. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  161. balloon.add( {
  162. view: viewA,
  163. position: { target: 'fake' }
  164. } );
  165. balloon.remove( viewA );
  166. expect( balloon.visibleView ).to.null;
  167. } );
  168. it( 'should remove given view and set previous in the stack as visible when removed view was visible', () => {
  169. balloon.add( {
  170. view: viewA,
  171. position: { target: 'fake' }
  172. } );
  173. balloon.add( {
  174. view: viewB,
  175. position: { target: 'fake' }
  176. } );
  177. balloon.remove( viewB );
  178. expect( balloon.visibleView ).to.equal( viewA );
  179. } );
  180. it( 'should remove given view from the stack when view is not visible', () => {
  181. balloon.add( {
  182. view: viewA,
  183. position: { target: 'fake' }
  184. } );
  185. balloon.add( {
  186. view: viewB,
  187. position: { target: 'fake' }
  188. } );
  189. balloon.remove( viewA );
  190. expect( balloon.visibleView ).to.equal( viewB );
  191. } );
  192. it( 'should throw an error when there is no given view in the stack', () => {
  193. expect( () => {
  194. balloon.remove( viewA );
  195. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  196. } );
  197. } );
  198. describe( 'updatePosition()', () => {
  199. it( 'should attach balloon to the target using the same position options as first view in the stack', () => {
  200. balloon.add( {
  201. view: viewA,
  202. position: {
  203. target: 'fake',
  204. foo: 'bar'
  205. }
  206. } );
  207. balloon.add( {
  208. view: viewB,
  209. position: {
  210. target: 'fake',
  211. bar: 'biz'
  212. }
  213. } );
  214. balloon.view.attachTo.reset();
  215. balloon.updatePosition();
  216. expect( balloon.view.attachTo.calledOnce );
  217. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  218. target: 'fake',
  219. foo: 'bar'
  220. } );
  221. } );
  222. it( 'should remove given view from the stack when view is not visible', () => {
  223. balloon.add( {
  224. view: viewA,
  225. position: { target: 'fake' }
  226. } );
  227. balloon.add( {
  228. view: viewB,
  229. position: { target: 'fake' }
  230. } );
  231. balloon.remove( viewA );
  232. expect( balloon.visibleView ).to.equal( viewB );
  233. } );
  234. it( 'should throw an error when there is no given view in the stack', () => {
  235. expect( () => {
  236. balloon.remove( viewA );
  237. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  238. } );
  239. } );
  240. describe( 'destroy()', () => {
  241. it( 'should balloon panel remove view from editor body collection', () => {
  242. balloon.destroy();
  243. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.equal( -1 );
  244. } );
  245. } );
  246. } );
  247. class ViewA extends View {
  248. constructor( locale ) {
  249. super( locale );
  250. this.template = new Template( {
  251. tag: 'div'
  252. } );
  253. }
  254. }
  255. class ViewB extends View {
  256. constructor( locale ) {
  257. super( locale );
  258. this.template = new Template( {
  259. tag: 'div'
  260. } );
  261. }
  262. }