contextualballoon.js 8.0 KB

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