8
0

balloonpanelview.js 23 KB

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