insertat.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: dom, browser-only */
  7. import insertAt from 'ckeditor5/utils/dom/insertat.js';
  8. describe( 'insertAt', () => {
  9. it( 'should insert at given position', () => {
  10. const div = document.createElement( 'div' );
  11. const p0 = document.createElement( 'p' );
  12. const p1 = document.createElement( 'p' );
  13. const p2 = document.createElement( 'p' );
  14. div.appendChild( p0 );
  15. div.appendChild( p2 );
  16. insertAt( div, 1, p1 );
  17. expect( div.childNodes.length ).to.equal( 3 );
  18. expect( div.childNodes[ 0 ] ).to.equal( p0 );
  19. expect( div.childNodes[ 1 ] ).to.equal( p1 );
  20. expect( div.childNodes[ 2 ] ).to.equal( p2 );
  21. } );
  22. it( 'should insert at the beginning', () => {
  23. const div = document.createElement( 'div' );
  24. const p0 = document.createElement( 'p' );
  25. const p1 = document.createElement( 'p' );
  26. const p2 = document.createElement( 'p' );
  27. div.appendChild( p1 );
  28. div.appendChild( p2 );
  29. insertAt( div, 0, p0 );
  30. expect( div.childNodes.length ).to.equal( 3 );
  31. expect( div.childNodes[ 0 ] ).to.equal( p0 );
  32. expect( div.childNodes[ 1 ] ).to.equal( p1 );
  33. expect( div.childNodes[ 2 ] ).to.equal( p2 );
  34. } );
  35. it( 'should insert at the end', () => {
  36. const div = document.createElement( 'div' );
  37. const p0 = document.createElement( 'p' );
  38. const p1 = document.createElement( 'p' );
  39. const p2 = document.createElement( 'p' );
  40. div.appendChild( p0 );
  41. div.appendChild( p1 );
  42. insertAt( div, 2, p2 );
  43. expect( div.childNodes.length ).to.equal( 3 );
  44. expect( div.childNodes[ 0 ] ).to.equal( p0 );
  45. expect( div.childNodes[ 1 ] ).to.equal( p1 );
  46. expect( div.childNodes[ 2 ] ).to.equal( p2 );
  47. } );
  48. } );