contextualballoon.js 9.1 KB

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