getpositionedancestor.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import getPositionedAncestor from '../../src/dom/getpositionedancestor';
  7. describe( 'getPositionedAncestor', () => {
  8. let element;
  9. beforeEach( () => {
  10. element = document.createElement( 'a' );
  11. document.body.appendChild( element );
  12. } );
  13. afterEach( () => {
  14. element.remove();
  15. } );
  16. it( 'should return null when there is no element', () => {
  17. expect( getPositionedAncestor() ).to.be.null;
  18. } );
  19. it( 'should return null when there is no positioned ancestor', () => {
  20. expect( getPositionedAncestor( element ) ).to.be.null;
  21. } );
  22. it( 'should not consider the passed element', () => {
  23. element.style.position = 'relative';
  24. expect( getPositionedAncestor( element ) ).to.be.null;
  25. } );
  26. it( 'should find the positioned ancestor (direct parent)', () => {
  27. const parent = document.createElement( 'div' );
  28. parent.appendChild( element );
  29. document.body.appendChild( parent );
  30. parent.style.position = 'absolute';
  31. expect( getPositionedAncestor( element ) ).to.equal( parent );
  32. parent.remove();
  33. } );
  34. it( 'should find the positioned ancestor (far ancestor)', () => {
  35. const parentA = document.createElement( 'div' );
  36. const parentB = document.createElement( 'div' );
  37. parentB.appendChild( element );
  38. parentA.appendChild( parentB );
  39. document.body.appendChild( parentA );
  40. parentA.style.position = 'absolute';
  41. expect( getPositionedAncestor( element ) ).to.equal( parentA );
  42. parentA.remove();
  43. } );
  44. } );