contextualballoon.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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( 'isViewInStack()', () => {
  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.isViewInStack( 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.visible.view === viewB ).to.true;
  67. expect( balloon.isViewInStack( viewA ) ).to.true;
  68. } );
  69. it( 'should return false when given view is not in stack', () => {
  70. expect( balloon.isViewInStack( 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( 'visible', () => {
  129. it( 'should return data of currently visible view', () => {
  130. balloon.add( {
  131. view: viewA,
  132. position: { target: 'fake' }
  133. } );
  134. expect( balloon.visible ).to.deep.equal( {
  135. view: viewA,
  136. position: { target: 'fake' }
  137. } );
  138. } );
  139. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  140. balloon.add( {
  141. view: viewA,
  142. position: { target: 'fake' }
  143. } );
  144. balloon.add( {
  145. view: viewB,
  146. position: { target: 'fake' }
  147. } );
  148. expect( balloon.visible ).to.deep.equal( {
  149. view: viewB,
  150. position: { target: 'fake' }
  151. } );
  152. } );
  153. it( 'should return `null` when the stack is empty', () => {
  154. expect( balloon.visible ).to.null;
  155. } );
  156. } );
  157. describe( 'remove()', () => {
  158. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  159. balloon.add( {
  160. view: viewA,
  161. position: { target: 'fake' }
  162. } );
  163. balloon.remove( viewA );
  164. expect( balloon.visible ).to.null;
  165. } );
  166. it( 'should remove given view and set previous in the stack as visible when removed view was visible', () => {
  167. balloon.add( {
  168. view: viewA,
  169. position: { target: 'fake' }
  170. } );
  171. balloon.add( {
  172. view: viewB,
  173. position: { target: 'fake' }
  174. } );
  175. balloon.remove( viewB );
  176. expect( balloon.visible ).to.deep.equal( {
  177. view: viewA,
  178. position: { target: 'fake' }
  179. } );
  180. } );
  181. it( 'should remove given view from the stack when view is not visible', () => {
  182. balloon.add( {
  183. view: viewA,
  184. position: { target: 'fake' }
  185. } );
  186. balloon.add( {
  187. view: viewB,
  188. position: { target: 'fake' }
  189. } );
  190. balloon.remove( viewA );
  191. expect( balloon.visible ).to.deep.equal( {
  192. view: viewB,
  193. position: { target: 'fake' }
  194. } );
  195. } );
  196. it( 'should throw an error when there is no given view in the stack', () => {
  197. expect( () => {
  198. balloon.remove( viewA );
  199. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  200. } );
  201. } );
  202. describe( 'updatePosition()', () => {
  203. it( 'should attach balloon to the target using the same position options as currently set', () => {
  204. balloon.add( {
  205. view: viewA,
  206. position: { target: 'fake' }
  207. } );
  208. balloon.view.attachTo.reset();
  209. balloon.updatePosition();
  210. expect( balloon.view.attachTo.calledOnce );
  211. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
  212. } );
  213. it( 'should attach balloon to the target using the same position options as currently set when there is more than one view', () => {
  214. balloon.add( {
  215. view: viewA,
  216. position: {
  217. target: 'fake',
  218. foo: 'bar'
  219. }
  220. } );
  221. balloon.add( {
  222. view: viewB,
  223. position: {
  224. target: 'fake',
  225. bar: 'biz'
  226. }
  227. } );
  228. balloon.view.attachTo.reset();
  229. balloon.updatePosition();
  230. expect( balloon.view.attachTo.calledOnce );
  231. expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {
  232. target: 'fake',
  233. foo: 'bar'
  234. } );
  235. } );
  236. it( 'should remove given view from the stack when view is not visible', () => {
  237. balloon.add( {
  238. view: viewA,
  239. position: { target: 'fake' }
  240. } );
  241. balloon.add( {
  242. view: viewB,
  243. position: { target: 'fake' }
  244. } );
  245. balloon.remove( viewA );
  246. expect( balloon.visible ).to.deep.equal( {
  247. view: viewB,
  248. position: { target: 'fake' }
  249. } );
  250. } );
  251. it( 'should throw an error when there is no given view in the stack', () => {
  252. expect( () => {
  253. balloon.remove( viewA );
  254. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  255. } );
  256. } );
  257. describe( 'destroy()', () => {
  258. it( 'should balloon panel remove view from editor body collection', () => {
  259. balloon.destroy();
  260. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.equal( -1 );
  261. } );
  262. } );
  263. } );
  264. class ViewA extends View {
  265. constructor( locale ) {
  266. super( locale );
  267. this.template = new Template( {
  268. tag: 'div'
  269. } );
  270. }
  271. }
  272. class ViewB extends View {
  273. constructor( locale ) {
  274. super( locale );
  275. this.template = new Template( {
  276. tag: 'div'
  277. } );
  278. }
  279. }