balloonpanelview.js 23 KB

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