balloonpanelview.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global window, document, Event */
  6. import ViewCollection from '../../../src/viewcollection';
  7. import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
  8. import ButtonView from '../../../src/button/buttonview';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. describe( 'BalloonPanelView', () => {
  11. let view;
  12. testUtils.createSinonSandbox();
  13. beforeEach( () => {
  14. view = new BalloonPanelView();
  15. view.render();
  16. document.body.appendChild( view.element );
  17. } );
  18. afterEach( () => {
  19. if ( view ) {
  20. view.element.remove();
  21. view.destroy();
  22. }
  23. } );
  24. describe( 'constructor()', () => {
  25. it( 'should create element from template', () => {
  26. expect( view.element.tagName ).to.equal( 'DIV' );
  27. expect( view.element.classList.contains( 'ck' ) ).to.true;
  28. expect( view.element.classList.contains( 'ck-balloon-panel' ) ).to.true;
  29. } );
  30. it( 'should set default values', () => {
  31. expect( view.top ).to.equal( 0 );
  32. expect( view.left ).to.equal( 0 );
  33. expect( view.position ).to.equal( 'arrow_nw' );
  34. expect( view.isVisible ).to.equal( false );
  35. expect( view.withArrow ).to.equal( true );
  36. } );
  37. it( 'creates view#content collection', () => {
  38. expect( view.content ).to.be.instanceOf( ViewCollection );
  39. } );
  40. } );
  41. describe( 'DOM bindings', () => {
  42. describe( 'arrow', () => {
  43. it( 'should react on view#position', () => {
  44. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_nw' ) ).to.true;
  45. view.position = 'arrow_ne';
  46. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_ne' ) ).to.true;
  47. } );
  48. it( 'should react on view#withArrow', () => {
  49. expect( view.element.classList.contains( 'ck-balloon-panel_with-arrow' ) ).to.be.true;
  50. view.withArrow = false;
  51. expect( view.element.classList.contains( 'ck-balloon-panel_with-arrow' ) ).to.be.false;
  52. } );
  53. } );
  54. describe( 'isVisible', () => {
  55. it( 'should react on view#isvisible', () => {
  56. expect( view.element.classList.contains( 'ck-balloon-panel_visible' ) ).to.false;
  57. view.isVisible = true;
  58. expect( view.element.classList.contains( 'ck-balloon-panel_visible' ) ).to.true;
  59. } );
  60. } );
  61. describe( 'styles', () => {
  62. it( 'should react on view#top', () => {
  63. expect( view.element.style.top ).to.equal( '0px' );
  64. view.top = 10;
  65. expect( view.element.style.top ).to.equal( '10px' );
  66. } );
  67. it( 'should react on view#left', () => {
  68. expect( view.element.style.left ).to.equal( '0px' );
  69. view.left = 10;
  70. expect( view.element.style.left ).to.equal( '10px' );
  71. } );
  72. } );
  73. describe( 'class', () => {
  74. it( 'should set additional class to the view#element', () => {
  75. view.class = 'foo';
  76. expect( view.element.classList.contains( 'foo' ) ).to.true;
  77. view.class = '';
  78. expect( view.element.classList.contains( 'foo' ) ).to.false;
  79. } );
  80. } );
  81. describe( 'children', () => {
  82. it( 'should react on view#content', () => {
  83. expect( view.element.childNodes.length ).to.equal( 0 );
  84. const button = new ButtonView( { t() {} } );
  85. view.content.add( button );
  86. expect( view.element.childNodes.length ).to.equal( 1 );
  87. } );
  88. } );
  89. } );
  90. describe( 'show()', () => {
  91. it( 'should set view#isVisible as true', () => {
  92. view.isVisible = false;
  93. view.show();
  94. expect( view.isVisible ).to.true;
  95. } );
  96. } );
  97. describe( 'hide()', () => {
  98. it( 'should set view#isVisible as false', () => {
  99. view.isVisible = true;
  100. view.hide();
  101. expect( view.isVisible ).to.false;
  102. } );
  103. } );
  104. describe( 'attachTo()', () => {
  105. let target, limiter;
  106. beforeEach( () => {
  107. limiter = document.createElement( 'div' );
  108. target = document.createElement( 'div' );
  109. document.body.appendChild( limiter );
  110. document.body.appendChild( target );
  111. // Mock balloon panel element dimensions.
  112. mockBoundingBox( view.element, {
  113. top: 0,
  114. left: 0,
  115. width: 100,
  116. height: 100
  117. } );
  118. // Mock window dimensions.
  119. testUtils.sinon.stub( window, 'innerWidth' ).value( 500 );
  120. testUtils.sinon.stub( window, 'innerHeight' ).value( 500 );
  121. testUtils.sinon.stub( window, 'scrollX' ).value( 0 );
  122. testUtils.sinon.stub( window, 'scrollY' ).value( 0 );
  123. } );
  124. afterEach( () => {
  125. limiter.remove();
  126. target.remove();
  127. } );
  128. it( 'should use default options', () => {
  129. const spy = testUtils.sinon.spy( BalloonPanelView, '_getOptimalPosition' );
  130. view.attachTo( { target } );
  131. sinon.assert.calledWithExactly( spy, sinon.match( {
  132. element: view.element,
  133. target,
  134. positions: [
  135. BalloonPanelView.defaultPositions.southArrowNorth,
  136. BalloonPanelView.defaultPositions.southArrowNorthMiddleWest,
  137. BalloonPanelView.defaultPositions.southArrowNorthMiddleEast,
  138. BalloonPanelView.defaultPositions.southArrowNorthWest,
  139. BalloonPanelView.defaultPositions.southArrowNorthEast,
  140. BalloonPanelView.defaultPositions.northArrowSouth,
  141. BalloonPanelView.defaultPositions.northArrowSouthMiddleWest,
  142. BalloonPanelView.defaultPositions.northArrowSouthMiddleEast,
  143. BalloonPanelView.defaultPositions.northArrowSouthWest,
  144. BalloonPanelView.defaultPositions.northArrowSouthEast
  145. ],
  146. limiter: document.body,
  147. fitInViewport: true
  148. } ) );
  149. } );
  150. it( 'should parse optimal position offset to int', () => {
  151. testUtils.sinon.stub( BalloonPanelView, '_getOptimalPosition' ).returns( {
  152. top: 10.345,
  153. left: 10.345,
  154. name: 'position'
  155. } );
  156. view.attachTo( { target, limiter } );
  157. expect( view.top ).to.equal( 10 );
  158. expect( view.left ).to.equal( 10 );
  159. } );
  160. describe( 'limited by limiter element', () => {
  161. beforeEach( () => {
  162. // Mock limiter element dimensions.
  163. mockBoundingBox( limiter, {
  164. left: 0,
  165. top: 0,
  166. width: 500,
  167. height: 500
  168. } );
  169. } );
  170. it( 'should put balloon on the `south east` side of the target element at default', () => {
  171. // Place target element at the center of the limiter.
  172. mockBoundingBox( target, {
  173. top: 225,
  174. left: 225,
  175. width: 50,
  176. height: 50
  177. } );
  178. view.attachTo( { target, limiter } );
  179. expect( view.position ).to.equal( 'arrow_n' );
  180. } );
  181. it( 'should put balloon on the `south east` side of the target element when ' +
  182. 'target is on the top left side of the limiter', () => {
  183. mockBoundingBox( target, {
  184. top: 0,
  185. left: 0,
  186. width: 50,
  187. height: 50
  188. } );
  189. view.attachTo( { target, limiter } );
  190. expect( view.position ).to.equal( 'arrow_nw' );
  191. } );
  192. it( 'should put balloon on the `south west` side of the target element when target is on the right side of the limiter', () => {
  193. mockBoundingBox( target, {
  194. top: 0,
  195. left: 450,
  196. width: 50,
  197. height: 50
  198. } );
  199. view.attachTo( { target, limiter } );
  200. expect( view.position ).to.equal( 'arrow_ne' );
  201. } );
  202. it( 'should put balloon on the `north east` side of the target element when target is on the bottom of the limiter ', () => {
  203. mockBoundingBox( target, {
  204. top: 450,
  205. left: 0,
  206. width: 50,
  207. height: 50
  208. } );
  209. view.attachTo( { target, limiter } );
  210. expect( view.position ).to.equal( 'arrow_sw' );
  211. } );
  212. it( 'should put balloon on the `north west` side of the target element when ' +
  213. 'target is on the bottom right of the limiter', () => {
  214. mockBoundingBox( target, {
  215. top: 450,
  216. left: 450,
  217. width: 50,
  218. height: 50
  219. } );
  220. view.attachTo( { target, limiter } );
  221. expect( view.position ).to.equal( 'arrow_se' );
  222. } );
  223. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  224. it( 'works in a positioned ancestor (position: absolute)', () => {
  225. const positionedAncestor = document.createElement( 'div' );
  226. positionedAncestor.style.position = 'absolute';
  227. positionedAncestor.style.top = '100px';
  228. positionedAncestor.style.left = '100px';
  229. positionedAncestor.appendChild( view.element );
  230. document.body.appendChild( positionedAncestor );
  231. positionedAncestor.appendChild( view.element );
  232. mockBoundingBox( positionedAncestor, {
  233. top: 100,
  234. left: 100,
  235. width: 10,
  236. height: 10
  237. } );
  238. mockBoundingBox( target, {
  239. top: 0,
  240. left: 0,
  241. width: 100,
  242. height: 100
  243. } );
  244. view.attachTo( { target, limiter } );
  245. expect( view.top ).to.equal( BalloonPanelView.arrowVerticalOffset );
  246. expect( view.left ).to.equal( -100 );
  247. positionedAncestor.remove();
  248. } );
  249. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  250. it( 'works in a positioned ancestor (position: static)', () => {
  251. const positionedAncestor = document.createElement( 'div' );
  252. positionedAncestor.style.position = 'static';
  253. positionedAncestor.appendChild( view.element );
  254. document.body.appendChild( positionedAncestor );
  255. positionedAncestor.appendChild( view.element );
  256. mockBoundingBox( target, {
  257. top: 0,
  258. left: 0,
  259. width: 100,
  260. height: 100
  261. } );
  262. view.attachTo( { target, limiter } );
  263. expect( view.top ).to.equal( BalloonPanelView.arrowVerticalOffset + 100 );
  264. expect( view.left ).to.equal( 0 );
  265. positionedAncestor.remove();
  266. } );
  267. } );
  268. describe( 'limited by viewport', () => {
  269. it( 'should put balloon on the `south west` position when `south east` is limited', () => {
  270. mockBoundingBox( limiter, {
  271. left: 0,
  272. top: 0,
  273. width: 500,
  274. height: 500
  275. } );
  276. mockBoundingBox( target, {
  277. top: 0,
  278. left: 225,
  279. width: 50,
  280. height: 50
  281. } );
  282. // Note: No sandboxing here. Otherwise, it would restore to the previously stubbed value.
  283. sinon.stub( window, 'innerWidth' ).value( 275 );
  284. view.attachTo( { target, limiter } );
  285. expect( view.position ).to.equal( 'arrow_ne' );
  286. } );
  287. it( 'should put balloon on the `south east` position when `south west` is limited', () => {
  288. mockBoundingBox( limiter, {
  289. top: 0,
  290. left: -400,
  291. width: 500,
  292. height: 500
  293. } );
  294. mockBoundingBox( target, {
  295. top: 0,
  296. left: 0,
  297. width: 50,
  298. height: 50
  299. } );
  300. view.attachTo( { target, limiter } );
  301. expect( view.position ).to.equal( 'arrow_nw' );
  302. } );
  303. it( 'should put balloon on the `north east` position when `south east` is limited', () => {
  304. mockBoundingBox( limiter, {
  305. left: 0,
  306. top: 0,
  307. width: 500,
  308. height: 500
  309. } );
  310. mockBoundingBox( target, {
  311. top: 225,
  312. left: 0,
  313. width: 50,
  314. height: 50
  315. } );
  316. // Note: No sandboxing here. Otherwise, it would restore to the previously stubbed value.
  317. sinon.stub( window, 'innerHeight' ).value( 275 );
  318. view.attachTo( { target, limiter } );
  319. expect( view.position ).to.equal( 'arrow_sw' );
  320. } );
  321. it( 'should put balloon on the `south east` position when `north east` is limited', () => {
  322. mockBoundingBox( limiter, {
  323. left: 0,
  324. top: -400,
  325. width: 500,
  326. height: 500
  327. } );
  328. mockBoundingBox( target, {
  329. top: 0,
  330. left: 0,
  331. width: 50,
  332. height: 50
  333. } );
  334. view.attachTo( { target, limiter } );
  335. expect( view.position ).to.equal( 'arrow_nw' );
  336. } );
  337. } );
  338. } );
  339. describe( 'pin() and unpin()', () => {
  340. let attachToSpy, target, targetParent, limiter, notRelatedElement;
  341. beforeEach( () => {
  342. attachToSpy = sinon.spy( view, 'attachTo' );
  343. limiter = document.createElement( 'div' );
  344. targetParent = document.createElement( 'div' );
  345. target = document.createElement( 'div' );
  346. notRelatedElement = document.createElement( 'div' );
  347. view.show();
  348. targetParent.appendChild( target );
  349. document.body.appendChild( targetParent );
  350. document.body.appendChild( limiter );
  351. document.body.appendChild( notRelatedElement );
  352. } );
  353. afterEach( () => {
  354. targetParent.remove();
  355. limiter.remove();
  356. notRelatedElement.remove();
  357. } );
  358. describe( 'pin()', () => {
  359. it( 'should show the balloon', () => {
  360. const spy = sinon.spy( view, 'show' );
  361. view.hide();
  362. view.pin( { target, limiter } );
  363. sinon.assert.calledOnce( spy );
  364. } );
  365. it( 'should start pinning when the balloon is visible', () => {
  366. view.pin( { target, limiter } );
  367. sinon.assert.calledOnce( attachToSpy );
  368. view.hide();
  369. targetParent.dispatchEvent( new Event( 'scroll' ) );
  370. view.show();
  371. sinon.assert.calledTwice( attachToSpy );
  372. targetParent.dispatchEvent( new Event( 'scroll' ) );
  373. sinon.assert.calledThrice( attachToSpy );
  374. } );
  375. it( 'should stop pinning when the balloon becomes invisible', () => {
  376. view.show();
  377. view.pin( { target, limiter } );
  378. sinon.assert.calledOnce( attachToSpy );
  379. view.hide();
  380. targetParent.dispatchEvent( new Event( 'scroll' ) );
  381. sinon.assert.calledOnce( attachToSpy );
  382. } );
  383. it( 'should unpin if already pinned', () => {
  384. const unpinSpy = testUtils.sinon.spy( view, 'unpin' );
  385. view.show();
  386. sinon.assert.notCalled( attachToSpy );
  387. view.pin( { target, limiter } );
  388. sinon.assert.calledOnce( attachToSpy );
  389. view.pin( { target, limiter } );
  390. sinon.assert.calledTwice( unpinSpy );
  391. targetParent.dispatchEvent( new Event( 'scroll' ) );
  392. sinon.assert.calledThrice( attachToSpy );
  393. } );
  394. it( 'should keep the balloon pinned to the target when any of the related elements is scrolled', () => {
  395. view.pin( { target, limiter } );
  396. sinon.assert.calledOnce( attachToSpy );
  397. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  398. targetParent.dispatchEvent( new Event( 'scroll' ) );
  399. sinon.assert.calledTwice( attachToSpy );
  400. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  401. limiter.dispatchEvent( new Event( 'scroll' ) );
  402. sinon.assert.calledThrice( attachToSpy );
  403. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  404. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  405. // Nothing's changed.
  406. sinon.assert.calledThrice( attachToSpy );
  407. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  408. } );
  409. it( 'should keep the balloon pinned to the target when the browser window is being resized', () => {
  410. view.pin( { target, limiter } );
  411. sinon.assert.calledOnce( attachToSpy );
  412. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  413. window.dispatchEvent( new Event( 'resize' ) );
  414. sinon.assert.calledTwice( attachToSpy );
  415. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  416. } );
  417. it( 'should stop attaching when the balloon is hidden', () => {
  418. view.pin( { target, limiter } );
  419. sinon.assert.calledOnce( attachToSpy );
  420. view.hide();
  421. window.dispatchEvent( new Event( 'resize' ) );
  422. window.dispatchEvent( new Event( 'scroll' ) );
  423. // Still once.
  424. sinon.assert.calledOnce( attachToSpy );
  425. } );
  426. it( 'should stop attaching once the view is destroyed', () => {
  427. view.pin( { target, limiter } );
  428. sinon.assert.calledOnce( attachToSpy );
  429. view.destroy();
  430. view.element.remove();
  431. view = null;
  432. window.dispatchEvent( new Event( 'resize' ) );
  433. window.dispatchEvent( new Event( 'scroll' ) );
  434. // Still once.
  435. sinon.assert.calledOnce( attachToSpy );
  436. } );
  437. it( 'should set document.body as the default limiter', () => {
  438. view.pin( { target } );
  439. sinon.assert.calledOnce( attachToSpy );
  440. document.body.dispatchEvent( new Event( 'scroll' ) );
  441. sinon.assert.calledTwice( attachToSpy );
  442. } );
  443. it( 'should work for Range as a target', () => {
  444. const element = document.createElement( 'div' );
  445. const range = document.createRange();
  446. element.appendChild( document.createTextNode( 'foo bar' ) );
  447. document.body.appendChild( element );
  448. range.selectNodeContents( element );
  449. view.pin( { target: range } );
  450. sinon.assert.calledOnce( attachToSpy );
  451. element.dispatchEvent( new Event( 'scroll' ) );
  452. sinon.assert.calledTwice( attachToSpy );
  453. element.remove();
  454. } );
  455. it( 'should work for a Rect as a target', () => {
  456. // Just check if this normally works without errors.
  457. const rect = {};
  458. view.pin( { target: rect, limiter } );
  459. sinon.assert.calledOnce( attachToSpy );
  460. limiter.dispatchEvent( new Event( 'scroll' ) );
  461. sinon.assert.calledTwice( attachToSpy );
  462. } );
  463. it( 'should work for a function as a target/limiter', () => {
  464. // Just check if this normally works without errors.
  465. const rect = {};
  466. view.pin( {
  467. target() { return rect; },
  468. limiter() { return limiter; }
  469. } );
  470. sinon.assert.calledOnce( attachToSpy );
  471. limiter.dispatchEvent( new Event( 'scroll' ) );
  472. sinon.assert.calledTwice( attachToSpy );
  473. } );
  474. // https://github.com/ckeditor/ckeditor5-ui/issues/227
  475. it( 'should react to #scroll from anywhere when the target is not an HTMLElement or Range', () => {
  476. const rect = {};
  477. view.pin( { target: rect } );
  478. sinon.assert.calledOnce( attachToSpy );
  479. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  480. sinon.assert.calledTwice( attachToSpy );
  481. } );
  482. // https://github.com/ckeditor/ckeditor5-ui/issues/260
  483. it( 'should react to #scroll from anywhere when the limiter is not an HTMLElement` or Range', () => {
  484. const rect = {};
  485. view.pin( { target, limiter: rect } );
  486. sinon.assert.calledOnce( attachToSpy );
  487. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  488. sinon.assert.calledTwice( attachToSpy );
  489. } );
  490. } );
  491. describe( 'unpin()', () => {
  492. it( 'should hide the balloon if pinned', () => {
  493. const spy = sinon.spy( view, 'hide' );
  494. view.pin( { target, limiter } );
  495. view.unpin();
  496. sinon.assert.calledOnce( spy );
  497. } );
  498. it( 'should stop attaching', () => {
  499. view.pin( { target, limiter } );
  500. sinon.assert.calledOnce( attachToSpy );
  501. view.unpin();
  502. view.hide();
  503. window.dispatchEvent( new Event( 'resize' ) );
  504. document.dispatchEvent( new Event( 'scroll' ) );
  505. view.show();
  506. window.dispatchEvent( new Event( 'resize' ) );
  507. document.dispatchEvent( new Event( 'scroll' ) );
  508. sinon.assert.calledOnce( attachToSpy );
  509. } );
  510. } );
  511. } );
  512. describe( 'defaultPositions', () => {
  513. let positions, balloonRect, targetRect, arrowHOffset, arrowVOffset;
  514. beforeEach( () => {
  515. positions = BalloonPanelView.defaultPositions;
  516. arrowHOffset = BalloonPanelView.arrowHorizontalOffset;
  517. arrowVOffset = BalloonPanelView.arrowVerticalOffset;
  518. targetRect = {
  519. top: 100,
  520. bottom: 200,
  521. left: 100,
  522. right: 200,
  523. width: 100,
  524. height: 100
  525. };
  526. balloonRect = {
  527. top: 0,
  528. bottom: 0,
  529. left: 0,
  530. right: 0,
  531. width: 50,
  532. height: 50
  533. };
  534. } );
  535. it( 'should have a proper length', () => {
  536. expect( Object.keys( positions ) ).to.have.length( 30 );
  537. } );
  538. // ------- North
  539. it( 'should define the "northArrowSouth" position', () => {
  540. expect( positions.northArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  541. top: 50 - arrowVOffset,
  542. left: 125,
  543. name: 'arrow_s'
  544. } );
  545. } );
  546. it( 'should define the "northArrowSouthEast" position', () => {
  547. expect( positions.northArrowSouthEast( targetRect, balloonRect ) ).to.deep.equal( {
  548. top: 50 - arrowVOffset,
  549. left: 100 + arrowHOffset,
  550. name: 'arrow_se'
  551. } );
  552. } );
  553. it( 'should define the "northArrowSouthMiddleEast" position', () => {
  554. expect( positions.northArrowSouthMiddleEast( targetRect, balloonRect ) ).to.deep.equal( {
  555. top: 50 - arrowVOffset,
  556. left: 112.5 + arrowHOffset,
  557. name: 'arrow_sme'
  558. } );
  559. } );
  560. it( 'should define the "northArrowSouthWest" position', () => {
  561. expect( positions.northArrowSouthWest( targetRect, balloonRect ) ).to.deep.equal( {
  562. top: 50 - arrowVOffset,
  563. left: 150 - arrowHOffset,
  564. name: 'arrow_sw'
  565. } );
  566. } );
  567. it( 'should define the "northArrowSouthMiddleWest" position', () => {
  568. expect( positions.northArrowSouthMiddleWest( targetRect, balloonRect ) ).to.deep.equal( {
  569. top: 50 - arrowVOffset,
  570. left: 137.5 - arrowHOffset,
  571. name: 'arrow_smw'
  572. } );
  573. } );
  574. // ------- North west
  575. it( 'should define the "northWestArrowSouth" position', () => {
  576. expect( positions.northWestArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  577. top: 50 - arrowVOffset,
  578. left: 75,
  579. name: 'arrow_s'
  580. } );
  581. } );
  582. it( 'should define the "northWestArrowSouthWest" position', () => {
  583. expect( positions.northWestArrowSouthWest( targetRect, balloonRect ) ).to.deep.equal( {
  584. top: 50 - arrowVOffset,
  585. left: 100 - arrowHOffset,
  586. name: 'arrow_sw'
  587. } );
  588. } );
  589. it( 'should define the "northWestArrowSouthMiddleWest" position', () => {
  590. expect( positions.northWestArrowSouthMiddleWest( targetRect, balloonRect ) ).to.deep.equal( {
  591. top: 50 - arrowVOffset,
  592. left: 87.5 - arrowHOffset,
  593. name: 'arrow_smw'
  594. } );
  595. } );
  596. it( 'should define the "northWestArrowSouthEast" position', () => {
  597. expect( positions.northWestArrowSouthEast( targetRect, balloonRect ) ).to.deep.equal( {
  598. top: 50 - arrowVOffset,
  599. left: 50 + arrowHOffset,
  600. name: 'arrow_se'
  601. } );
  602. } );
  603. it( 'should define the "northWestArrowSouthMiddleEast" position', () => {
  604. expect( positions.northWestArrowSouthMiddleEast( targetRect, balloonRect ) ).to.deep.equal( {
  605. top: 50 - arrowVOffset,
  606. left: 62.5 + arrowHOffset,
  607. name: 'arrow_sme'
  608. } );
  609. } );
  610. // ------- North east
  611. it( 'should define the "northEastArrowSouth" position', () => {
  612. expect( positions.northEastArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  613. top: 50 - arrowVOffset,
  614. left: 175,
  615. name: 'arrow_s'
  616. } );
  617. } );
  618. it( 'should define the "northEastArrowSouthEast" position', () => {
  619. expect( positions.northEastArrowSouthEast( targetRect, balloonRect ) ).to.deep.equal( {
  620. top: 50 - arrowVOffset,
  621. left: 150 + arrowHOffset,
  622. name: 'arrow_se'
  623. } );
  624. } );
  625. it( 'should define the "northEastArrowSouthMiddleEast" position', () => {
  626. expect( positions.northEastArrowSouthMiddleEast( targetRect, balloonRect ) ).to.deep.equal( {
  627. top: 50 - arrowVOffset,
  628. left: 162.5 + arrowHOffset,
  629. name: 'arrow_sme'
  630. } );
  631. } );
  632. it( 'should define the "northEastArrowSouthWest" position', () => {
  633. expect( positions.northEastArrowSouthWest( targetRect, balloonRect ) ).to.deep.equal( {
  634. top: 50 - arrowVOffset,
  635. left: 200 - arrowHOffset,
  636. name: 'arrow_sw'
  637. } );
  638. } );
  639. it( 'should define the "northEastArrowSouthMiddleWest" position', () => {
  640. expect( positions.northEastArrowSouthMiddleWest( targetRect, balloonRect ) ).to.deep.equal( {
  641. top: 50 - arrowVOffset,
  642. left: 187.5 - arrowHOffset,
  643. name: 'arrow_smw'
  644. } );
  645. } );
  646. // ------- South
  647. it( 'should define the "southArrowNorth" position', () => {
  648. expect( positions.southArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  649. top: 200 + arrowVOffset,
  650. left: 125,
  651. name: 'arrow_n'
  652. } );
  653. } );
  654. it( 'should define the "southArrowNorthEast" position', () => {
  655. expect( positions.southArrowNorthEast( targetRect, balloonRect ) ).to.deep.equal( {
  656. top: 200 + arrowVOffset,
  657. left: 100 + arrowHOffset,
  658. name: 'arrow_ne'
  659. } );
  660. } );
  661. it( 'should define the "southArrowNorthMiddleEast" position', () => {
  662. expect( positions.southArrowNorthMiddleEast( targetRect, balloonRect ) ).to.deep.equal( {
  663. top: 200 + arrowVOffset,
  664. left: 112.5 + arrowHOffset,
  665. name: 'arrow_nme'
  666. } );
  667. } );
  668. it( 'should define the "southArrowNorthWest" position', () => {
  669. expect( positions.southArrowNorthWest( targetRect, balloonRect ) ).to.deep.equal( {
  670. top: 200 + arrowVOffset,
  671. left: 150 - arrowHOffset,
  672. name: 'arrow_nw'
  673. } );
  674. } );
  675. it( 'should define the "southArrowNorthMiddleWest" position', () => {
  676. expect( positions.southArrowNorthMiddleWest( targetRect, balloonRect ) ).to.deep.equal( {
  677. top: 200 + arrowVOffset,
  678. left: 137.5 - arrowHOffset,
  679. name: 'arrow_nmw'
  680. } );
  681. } );
  682. // ------- South west
  683. it( 'should define the "southWestArrowNorth" position', () => {
  684. expect( positions.southWestArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  685. top: 200 + arrowVOffset,
  686. left: 75,
  687. name: 'arrow_n'
  688. } );
  689. } );
  690. it( 'should define the "southWestArrowNorthWest" position', () => {
  691. expect( positions.southWestArrowNorthWest( targetRect, balloonRect ) ).to.deep.equal( {
  692. top: 200 + arrowVOffset,
  693. left: 100 - arrowHOffset,
  694. name: 'arrow_nw'
  695. } );
  696. } );
  697. it( 'should define the "southWestArrowNorthMiddleWest" position', () => {
  698. expect( positions.southWestArrowNorthMiddleWest( targetRect, balloonRect ) ).to.deep.equal( {
  699. top: 200 + arrowVOffset,
  700. left: 87.5 - arrowHOffset,
  701. name: 'arrow_nmw'
  702. } );
  703. } );
  704. it( 'should define the "southWestArrowNorthEast" position', () => {
  705. expect( positions.southWestArrowNorthEast( targetRect, balloonRect ) ).to.deep.equal( {
  706. top: 200 + arrowVOffset,
  707. left: 50 + arrowHOffset,
  708. name: 'arrow_ne'
  709. } );
  710. } );
  711. it( 'should define the "southWestArrowNorthMiddleEast" position', () => {
  712. expect( positions.southWestArrowNorthMiddleEast( targetRect, balloonRect ) ).to.deep.equal( {
  713. top: 200 + arrowVOffset,
  714. left: 62.5 + arrowHOffset,
  715. name: 'arrow_nme'
  716. } );
  717. } );
  718. // ------- South east
  719. it( 'should define the "southEastArrowNorth" position', () => {
  720. expect( positions.southEastArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  721. top: 200 + arrowVOffset,
  722. left: 175,
  723. name: 'arrow_n'
  724. } );
  725. } );
  726. it( 'should define the "southEastArrowNorthEast" position', () => {
  727. expect( positions.southEastArrowNorthEast( targetRect, balloonRect ) ).to.deep.equal( {
  728. top: 200 + arrowVOffset,
  729. left: 150 + arrowHOffset,
  730. name: 'arrow_ne'
  731. } );
  732. } );
  733. it( 'should define the "southEastArrowNorthMiddleEast" position', () => {
  734. expect( positions.southEastArrowNorthMiddleEast( targetRect, balloonRect ) ).to.deep.equal( {
  735. top: 200 + arrowVOffset,
  736. left: 162.5 + arrowHOffset,
  737. name: 'arrow_nme'
  738. } );
  739. } );
  740. it( 'should define the "southEastArrowNorthWest" position', () => {
  741. expect( positions.southEastArrowNorthWest( targetRect, balloonRect ) ).to.deep.equal( {
  742. top: 200 + arrowVOffset,
  743. left: 200 - arrowHOffset,
  744. name: 'arrow_nw'
  745. } );
  746. } );
  747. it( 'should define the "southEastArrowNorthMiddleWest" position', () => {
  748. expect( positions.southEastArrowNorthMiddleWest( targetRect, balloonRect ) ).to.deep.equal( {
  749. top: 200 + arrowVOffset,
  750. left: 187.5 - arrowHOffset,
  751. name: 'arrow_nmw'
  752. } );
  753. } );
  754. } );
  755. } );
  756. function mockBoundingBox( element, data ) {
  757. const boundingBox = Object.assign( {}, data );
  758. boundingBox.right = boundingBox.left + boundingBox.width;
  759. boundingBox.bottom = boundingBox.top + boundingBox.height;
  760. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  761. }