8
0

balloonpanelview.js 22 KB

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