insertat.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /* globals document */
  6. import insertAt from '../../src/dom/insertat';
  7. describe( 'insertAt', () => {
  8. it( 'should insert at given position', () => {
  9. const div = document.createElement( 'div' );
  10. const p0 = document.createElement( 'p' );
  11. const p1 = document.createElement( 'p' );
  12. const p2 = document.createElement( 'p' );
  13. div.appendChild( p0 );
  14. div.appendChild( p2 );
  15. insertAt( div, 1, p1 );
  16. expect( div.childNodes.length ).to.equal( 3 );
  17. expect( div.childNodes[ 0 ] ).to.equal( p0 );
  18. expect( div.childNodes[ 1 ] ).to.equal( p1 );
  19. expect( div.childNodes[ 2 ] ).to.equal( p2 );
  20. } );
  21. it( 'should insert at the beginning', () => {
  22. const div = document.createElement( 'div' );
  23. const p0 = document.createElement( 'p' );
  24. const p1 = document.createElement( 'p' );
  25. const p2 = document.createElement( 'p' );
  26. div.appendChild( p1 );
  27. div.appendChild( p2 );
  28. insertAt( div, 0, p0 );
  29. expect( div.childNodes.length ).to.equal( 3 );
  30. expect( div.childNodes[ 0 ] ).to.equal( p0 );
  31. expect( div.childNodes[ 1 ] ).to.equal( p1 );
  32. expect( div.childNodes[ 2 ] ).to.equal( p2 );
  33. } );
  34. it( 'should insert at the end', () => {
  35. const div = document.createElement( 'div' );
  36. const p0 = document.createElement( 'p' );
  37. const p1 = document.createElement( 'p' );
  38. const p2 = document.createElement( 'p' );
  39. div.appendChild( p0 );
  40. div.appendChild( p1 );
  41. insertAt( div, 2, p2 );
  42. expect( div.childNodes.length ).to.equal( 3 );
  43. expect( div.childNodes[ 0 ] ).to.equal( p0 );
  44. expect( div.childNodes[ 1 ] ).to.equal( p1 );
  45. expect( div.childNodes[ 2 ] ).to.equal( p2 );
  46. } );
  47. } );