balloonpanelview.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /**
  2. * @license Copyright (c) 2003-2019, 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.southArrowNorthWest,
  137. BalloonPanelView.defaultPositions.southArrowNorthEast,
  138. BalloonPanelView.defaultPositions.northArrowSouth,
  139. BalloonPanelView.defaultPositions.northArrowSouthWest,
  140. BalloonPanelView.defaultPositions.northArrowSouthEast
  141. ],
  142. limiter: document.body,
  143. fitInViewport: true
  144. } ) );
  145. } );
  146. it( 'should parse optimal position offset to int', () => {
  147. testUtils.sinon.stub( BalloonPanelView, '_getOptimalPosition' ).returns( {
  148. top: 10.345,
  149. left: 10.345,
  150. name: 'position'
  151. } );
  152. view.attachTo( { target, limiter } );
  153. expect( view.top ).to.equal( 10 );
  154. expect( view.left ).to.equal( 10 );
  155. } );
  156. describe( 'limited by limiter element', () => {
  157. beforeEach( () => {
  158. // Mock limiter element dimensions.
  159. mockBoundingBox( limiter, {
  160. left: 0,
  161. top: 0,
  162. width: 500,
  163. height: 500
  164. } );
  165. } );
  166. it( 'should put balloon on the `south east` side of the target element at default', () => {
  167. // Place target element at the center of the limiter.
  168. mockBoundingBox( target, {
  169. top: 225,
  170. left: 225,
  171. width: 50,
  172. height: 50
  173. } );
  174. view.attachTo( { target, limiter } );
  175. expect( view.position ).to.equal( 'arrow_n' );
  176. } );
  177. it( 'should put balloon on the `south east` side of the target element when ' +
  178. 'target is on the top left side of the limiter', () => {
  179. mockBoundingBox( target, {
  180. top: 0,
  181. left: 0,
  182. width: 50,
  183. height: 50
  184. } );
  185. view.attachTo( { target, limiter } );
  186. expect( view.position ).to.equal( 'arrow_nw' );
  187. } );
  188. it( 'should put balloon on the `south west` side of the target element when target is on the right side of the limiter', () => {
  189. mockBoundingBox( target, {
  190. top: 0,
  191. left: 450,
  192. width: 50,
  193. height: 50
  194. } );
  195. view.attachTo( { target, limiter } );
  196. expect( view.position ).to.equal( 'arrow_ne' );
  197. } );
  198. it( 'should put balloon on the `north east` side of the target element when target is on the bottom of the limiter ', () => {
  199. mockBoundingBox( target, {
  200. top: 450,
  201. left: 0,
  202. width: 50,
  203. height: 50
  204. } );
  205. view.attachTo( { target, limiter } );
  206. expect( view.position ).to.equal( 'arrow_sw' );
  207. } );
  208. it( 'should put balloon on the `north west` side of the target element when ' +
  209. 'target is on the bottom right of the limiter', () => {
  210. mockBoundingBox( target, {
  211. top: 450,
  212. left: 450,
  213. width: 50,
  214. height: 50
  215. } );
  216. view.attachTo( { target, limiter } );
  217. expect( view.position ).to.equal( 'arrow_se' );
  218. } );
  219. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  220. it( 'works in a positioned ancestor (position: absolute)', () => {
  221. const positionedAncestor = document.createElement( 'div' );
  222. positionedAncestor.style.position = 'absolute';
  223. positionedAncestor.style.top = '100px';
  224. positionedAncestor.style.left = '100px';
  225. positionedAncestor.appendChild( view.element );
  226. document.body.appendChild( positionedAncestor );
  227. positionedAncestor.appendChild( view.element );
  228. mockBoundingBox( positionedAncestor, {
  229. top: 100,
  230. left: 100,
  231. width: 10,
  232. height: 10
  233. } );
  234. mockBoundingBox( target, {
  235. top: 0,
  236. left: 0,
  237. width: 100,
  238. height: 100
  239. } );
  240. view.attachTo( { target, limiter } );
  241. expect( view.top ).to.equal( BalloonPanelView.arrowVerticalOffset );
  242. expect( view.left ).to.equal( -100 );
  243. positionedAncestor.remove();
  244. } );
  245. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  246. it( 'works in a positioned ancestor (position: static)', () => {
  247. const positionedAncestor = document.createElement( 'div' );
  248. positionedAncestor.style.position = 'static';
  249. positionedAncestor.appendChild( view.element );
  250. document.body.appendChild( positionedAncestor );
  251. positionedAncestor.appendChild( view.element );
  252. mockBoundingBox( target, {
  253. top: 0,
  254. left: 0,
  255. width: 100,
  256. height: 100
  257. } );
  258. view.attachTo( { target, limiter } );
  259. expect( view.top ).to.equal( BalloonPanelView.arrowVerticalOffset + 100 );
  260. expect( view.left ).to.equal( 0 );
  261. positionedAncestor.remove();
  262. } );
  263. } );
  264. describe( 'limited by viewport', () => {
  265. it( 'should put balloon on the `south west` position when `south east` is limited', () => {
  266. mockBoundingBox( limiter, {
  267. left: 0,
  268. top: 0,
  269. width: 500,
  270. height: 500
  271. } );
  272. mockBoundingBox( target, {
  273. top: 0,
  274. left: 225,
  275. width: 50,
  276. height: 50
  277. } );
  278. // Note: No sandboxing here. Otherwise, it would restore to the previously stubbed value.
  279. sinon.stub( window, 'innerWidth' ).value( 275 );
  280. view.attachTo( { target, limiter } );
  281. expect( view.position ).to.equal( 'arrow_ne' );
  282. } );
  283. it( 'should put balloon on the `south east` position when `south west` is limited', () => {
  284. mockBoundingBox( limiter, {
  285. top: 0,
  286. left: -400,
  287. width: 500,
  288. height: 500
  289. } );
  290. mockBoundingBox( target, {
  291. top: 0,
  292. left: 0,
  293. width: 50,
  294. height: 50
  295. } );
  296. view.attachTo( { target, limiter } );
  297. expect( view.position ).to.equal( 'arrow_nw' );
  298. } );
  299. it( 'should put balloon on the `north east` position when `south east` is limited', () => {
  300. mockBoundingBox( limiter, {
  301. left: 0,
  302. top: 0,
  303. width: 500,
  304. height: 500
  305. } );
  306. mockBoundingBox( target, {
  307. top: 225,
  308. left: 0,
  309. width: 50,
  310. height: 50
  311. } );
  312. // Note: No sandboxing here. Otherwise, it would restore to the previously stubbed value.
  313. sinon.stub( window, 'innerHeight' ).value( 275 );
  314. view.attachTo( { target, limiter } );
  315. expect( view.position ).to.equal( 'arrow_sw' );
  316. } );
  317. it( 'should put balloon on the `south east` position when `north east` is limited', () => {
  318. mockBoundingBox( limiter, {
  319. left: 0,
  320. top: -400,
  321. width: 500,
  322. height: 500
  323. } );
  324. mockBoundingBox( target, {
  325. top: 0,
  326. left: 0,
  327. width: 50,
  328. height: 50
  329. } );
  330. view.attachTo( { target, limiter } );
  331. expect( view.position ).to.equal( 'arrow_nw' );
  332. } );
  333. } );
  334. } );
  335. describe( 'pin() and unpin()', () => {
  336. let attachToSpy, target, targetParent, limiter, notRelatedElement;
  337. beforeEach( () => {
  338. attachToSpy = sinon.spy( view, 'attachTo' );
  339. limiter = document.createElement( 'div' );
  340. targetParent = document.createElement( 'div' );
  341. target = document.createElement( 'div' );
  342. notRelatedElement = document.createElement( 'div' );
  343. view.show();
  344. targetParent.appendChild( target );
  345. document.body.appendChild( targetParent );
  346. document.body.appendChild( limiter );
  347. document.body.appendChild( notRelatedElement );
  348. } );
  349. afterEach( () => {
  350. targetParent.remove();
  351. limiter.remove();
  352. notRelatedElement.remove();
  353. } );
  354. describe( 'pin()', () => {
  355. it( 'should show the balloon', () => {
  356. const spy = sinon.spy( view, 'show' );
  357. view.hide();
  358. view.pin( { target, limiter } );
  359. sinon.assert.calledOnce( spy );
  360. } );
  361. it( 'should start pinning when the balloon is visible', () => {
  362. view.pin( { target, limiter } );
  363. sinon.assert.calledOnce( attachToSpy );
  364. view.hide();
  365. targetParent.dispatchEvent( new Event( 'scroll' ) );
  366. view.show();
  367. sinon.assert.calledTwice( attachToSpy );
  368. targetParent.dispatchEvent( new Event( 'scroll' ) );
  369. sinon.assert.calledThrice( attachToSpy );
  370. } );
  371. it( 'should stop pinning when the balloon becomes invisible', () => {
  372. view.show();
  373. view.pin( { target, limiter } );
  374. sinon.assert.calledOnce( attachToSpy );
  375. view.hide();
  376. targetParent.dispatchEvent( new Event( 'scroll' ) );
  377. sinon.assert.calledOnce( attachToSpy );
  378. } );
  379. it( 'should unpin if already pinned', () => {
  380. const unpinSpy = testUtils.sinon.spy( view, 'unpin' );
  381. view.show();
  382. sinon.assert.notCalled( attachToSpy );
  383. view.pin( { target, limiter } );
  384. sinon.assert.calledOnce( attachToSpy );
  385. view.pin( { target, limiter } );
  386. sinon.assert.calledTwice( unpinSpy );
  387. targetParent.dispatchEvent( new Event( 'scroll' ) );
  388. sinon.assert.calledThrice( attachToSpy );
  389. } );
  390. it( 'should keep the balloon pinned to the target when any of the related elements is scrolled', () => {
  391. view.pin( { target, limiter } );
  392. sinon.assert.calledOnce( attachToSpy );
  393. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  394. targetParent.dispatchEvent( new Event( 'scroll' ) );
  395. sinon.assert.calledTwice( attachToSpy );
  396. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  397. limiter.dispatchEvent( new Event( 'scroll' ) );
  398. sinon.assert.calledThrice( attachToSpy );
  399. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  400. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  401. // Nothing's changed.
  402. sinon.assert.calledThrice( attachToSpy );
  403. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  404. } );
  405. it( 'should keep the balloon pinned to the target when the browser window is being resized', () => {
  406. view.pin( { target, limiter } );
  407. sinon.assert.calledOnce( attachToSpy );
  408. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  409. window.dispatchEvent( new Event( 'resize' ) );
  410. sinon.assert.calledTwice( attachToSpy );
  411. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  412. } );
  413. it( 'should stop attaching when the balloon is hidden', () => {
  414. view.pin( { target, limiter } );
  415. sinon.assert.calledOnce( attachToSpy );
  416. view.hide();
  417. window.dispatchEvent( new Event( 'resize' ) );
  418. window.dispatchEvent( new Event( 'scroll' ) );
  419. // Still once.
  420. sinon.assert.calledOnce( attachToSpy );
  421. } );
  422. it( 'should stop attaching once the view is destroyed', () => {
  423. view.pin( { target, limiter } );
  424. sinon.assert.calledOnce( attachToSpy );
  425. view.destroy();
  426. view.element.remove();
  427. view = null;
  428. window.dispatchEvent( new Event( 'resize' ) );
  429. window.dispatchEvent( new Event( 'scroll' ) );
  430. // Still once.
  431. sinon.assert.calledOnce( attachToSpy );
  432. } );
  433. it( 'should set document.body as the default limiter', () => {
  434. view.pin( { target } );
  435. sinon.assert.calledOnce( attachToSpy );
  436. document.body.dispatchEvent( new Event( 'scroll' ) );
  437. sinon.assert.calledTwice( attachToSpy );
  438. } );
  439. it( 'should work for Range as a target', () => {
  440. const element = document.createElement( 'div' );
  441. const range = document.createRange();
  442. element.appendChild( document.createTextNode( 'foo bar' ) );
  443. document.body.appendChild( element );
  444. range.selectNodeContents( element );
  445. view.pin( { target: range } );
  446. sinon.assert.calledOnce( attachToSpy );
  447. element.dispatchEvent( new Event( 'scroll' ) );
  448. sinon.assert.calledTwice( attachToSpy );
  449. element.remove();
  450. } );
  451. it( 'should work for a Rect as a target', () => {
  452. // Just check if this normally works without errors.
  453. const rect = {};
  454. view.pin( { target: rect, limiter } );
  455. sinon.assert.calledOnce( attachToSpy );
  456. limiter.dispatchEvent( new Event( 'scroll' ) );
  457. sinon.assert.calledTwice( attachToSpy );
  458. } );
  459. it( 'should work for a function as a target/limiter', () => {
  460. // Just check if this normally works without errors.
  461. const rect = {};
  462. view.pin( {
  463. target() { return rect; },
  464. limiter() { return limiter; }
  465. } );
  466. sinon.assert.calledOnce( attachToSpy );
  467. limiter.dispatchEvent( new Event( 'scroll' ) );
  468. sinon.assert.calledTwice( attachToSpy );
  469. } );
  470. // https://github.com/ckeditor/ckeditor5-ui/issues/227
  471. it( 'should react to #scroll from anywhere when the target is not an HTMLElement or Range', () => {
  472. const rect = {};
  473. view.pin( { target: rect } );
  474. sinon.assert.calledOnce( attachToSpy );
  475. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  476. sinon.assert.calledTwice( attachToSpy );
  477. } );
  478. // https://github.com/ckeditor/ckeditor5-ui/issues/260
  479. it( 'should react to #scroll from anywhere when the limiter is not an HTMLElement` or Range', () => {
  480. const rect = {};
  481. view.pin( { target, limiter: rect } );
  482. sinon.assert.calledOnce( attachToSpy );
  483. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  484. sinon.assert.calledTwice( attachToSpy );
  485. } );
  486. } );
  487. describe( 'unpin()', () => {
  488. it( 'should hide the balloon if pinned', () => {
  489. const spy = sinon.spy( view, 'hide' );
  490. view.pin( { target, limiter } );
  491. view.unpin();
  492. sinon.assert.calledOnce( spy );
  493. } );
  494. it( 'should stop attaching', () => {
  495. view.pin( { target, limiter } );
  496. sinon.assert.calledOnce( attachToSpy );
  497. view.unpin();
  498. view.hide();
  499. window.dispatchEvent( new Event( 'resize' ) );
  500. document.dispatchEvent( new Event( 'scroll' ) );
  501. view.show();
  502. window.dispatchEvent( new Event( 'resize' ) );
  503. document.dispatchEvent( new Event( 'scroll' ) );
  504. sinon.assert.calledOnce( attachToSpy );
  505. } );
  506. } );
  507. } );
  508. describe( 'defaultPositions', () => {
  509. let positions, balloonRect, targetRect, arrowHOffset, arrowVOffset;
  510. beforeEach( () => {
  511. positions = BalloonPanelView.defaultPositions;
  512. arrowHOffset = BalloonPanelView.arrowHorizontalOffset;
  513. arrowVOffset = BalloonPanelView.arrowVerticalOffset;
  514. targetRect = {
  515. top: 100,
  516. bottom: 200,
  517. left: 100,
  518. right: 200,
  519. width: 100,
  520. height: 100
  521. };
  522. balloonRect = {
  523. top: 0,
  524. bottom: 0,
  525. left: 0,
  526. right: 0,
  527. width: 50,
  528. height: 50
  529. };
  530. } );
  531. it( 'should have a proper length', () => {
  532. expect( Object.keys( positions ) ).to.have.length( 18 );
  533. } );
  534. // ------- North
  535. it( 'should define the "northArrowSouth" position', () => {
  536. expect( positions.northArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  537. top: 50 - arrowVOffset,
  538. left: 125,
  539. name: 'arrow_s'
  540. } );
  541. } );
  542. it( 'should define the "northArrowSouthEast" position', () => {
  543. expect( positions.northArrowSouthEast( targetRect, balloonRect ) ).to.deep.equal( {
  544. top: 50 - arrowVOffset,
  545. left: 100 + arrowHOffset,
  546. name: 'arrow_se'
  547. } );
  548. } );
  549. it( 'should define the "northArrowSouthWest" position', () => {
  550. expect( positions.northArrowSouthWest( targetRect, balloonRect ) ).to.deep.equal( {
  551. top: 50 - arrowVOffset,
  552. left: 150 - arrowHOffset,
  553. name: 'arrow_sw'
  554. } );
  555. } );
  556. // ------- North west
  557. it( 'should define the "northWestArrowSouth" position', () => {
  558. expect( positions.northWestArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  559. top: 50 - arrowVOffset,
  560. left: 75,
  561. name: 'arrow_s'
  562. } );
  563. } );
  564. it( 'should define the "northWestArrowSouthWest" position', () => {
  565. expect( positions.northWestArrowSouthWest( targetRect, balloonRect ) ).to.deep.equal( {
  566. top: 50 - arrowVOffset,
  567. left: 100 - arrowHOffset,
  568. name: 'arrow_sw'
  569. } );
  570. } );
  571. it( 'should define the "northWestArrowSouthEast" position', () => {
  572. expect( positions.northWestArrowSouthEast( targetRect, balloonRect ) ).to.deep.equal( {
  573. top: 50 - arrowVOffset,
  574. left: 50 + arrowHOffset,
  575. name: 'arrow_se'
  576. } );
  577. } );
  578. // ------- North east
  579. it( 'should define the "northEastArrowSouth" position', () => {
  580. expect( positions.northEastArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  581. top: 50 - arrowVOffset,
  582. left: 175,
  583. name: 'arrow_s'
  584. } );
  585. } );
  586. it( 'should define the "northEastArrowSouthEast" position', () => {
  587. expect( positions.northEastArrowSouthEast( targetRect, balloonRect ) ).to.deep.equal( {
  588. top: 50 - arrowVOffset,
  589. left: 150 + arrowHOffset,
  590. name: 'arrow_se'
  591. } );
  592. } );
  593. it( 'should define the "northEastArrowSouthWest" position', () => {
  594. expect( positions.northEastArrowSouthWest( targetRect, balloonRect ) ).to.deep.equal( {
  595. top: 50 - arrowVOffset,
  596. left: 200 - arrowHOffset,
  597. name: 'arrow_sw'
  598. } );
  599. } );
  600. // ------- South
  601. it( 'should define the "southArrowNorth" position', () => {
  602. expect( positions.southArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  603. top: 200 + arrowVOffset,
  604. left: 125,
  605. name: 'arrow_n'
  606. } );
  607. } );
  608. it( 'should define the "southArrowNorthEast" position', () => {
  609. expect( positions.southArrowNorthEast( targetRect, balloonRect ) ).to.deep.equal( {
  610. top: 200 + arrowVOffset,
  611. left: 100 + arrowHOffset,
  612. name: 'arrow_ne'
  613. } );
  614. } );
  615. it( 'should define the "southArrowNorthWest" position', () => {
  616. expect( positions.southArrowNorthWest( targetRect, balloonRect ) ).to.deep.equal( {
  617. top: 200 + arrowVOffset,
  618. left: 150 - arrowHOffset,
  619. name: 'arrow_nw'
  620. } );
  621. } );
  622. // ------- South west
  623. it( 'should define the "southWestArrowNorth" position', () => {
  624. expect( positions.southWestArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  625. top: 200 + arrowVOffset,
  626. left: 75,
  627. name: 'arrow_n'
  628. } );
  629. } );
  630. it( 'should define the "southWestArrowNorthWest" position', () => {
  631. expect( positions.southWestArrowNorthWest( targetRect, balloonRect ) ).to.deep.equal( {
  632. top: 200 + arrowVOffset,
  633. left: 100 - arrowHOffset,
  634. name: 'arrow_nw'
  635. } );
  636. } );
  637. it( 'should define the "southWestArrowNorthEast" position', () => {
  638. expect( positions.southWestArrowNorthEast( targetRect, balloonRect ) ).to.deep.equal( {
  639. top: 200 + arrowVOffset,
  640. left: 50 + arrowHOffset,
  641. name: 'arrow_ne'
  642. } );
  643. } );
  644. // ------- South east
  645. it( 'should define the "southEastArrowNorth" position', () => {
  646. expect( positions.southEastArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  647. top: 200 + arrowVOffset,
  648. left: 175,
  649. name: 'arrow_n'
  650. } );
  651. } );
  652. it( 'should define the "southEastArrowNorthEast" position', () => {
  653. expect( positions.southEastArrowNorthEast( targetRect, balloonRect ) ).to.deep.equal( {
  654. top: 200 + arrowVOffset,
  655. left: 150 + arrowHOffset,
  656. name: 'arrow_ne'
  657. } );
  658. } );
  659. it( 'should define the "southEastArrowNorthWest" position', () => {
  660. expect( positions.southEastArrowNorthWest( targetRect, balloonRect ) ).to.deep.equal( {
  661. top: 200 + arrowVOffset,
  662. left: 200 - arrowHOffset,
  663. name: 'arrow_nw'
  664. } );
  665. } );
  666. } );
  667. } );
  668. function mockBoundingBox( element, data ) {
  669. const boundingBox = Object.assign( {}, data );
  670. boundingBox.right = boundingBox.left + boundingBox.width;
  671. boundingBox.bottom = boundingBox.top + boundingBox.height;
  672. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  673. }