element.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* jshint expr: true */
  6. /* bender-tags: treeview */
  7. /* bender-include: ../_tools/tools.js */
  8. 'use strict';
  9. const getIteratorCount = bender.tools.core.getIteratorCount;
  10. const modules = bender.amd.require(
  11. 'core/treeview/node',
  12. 'core/treeview/element'
  13. );
  14. describe( 'Element', () => {
  15. let Element, Node;
  16. before( () => {
  17. Element = modules[ 'core/treeview/element' ];
  18. Node = modules[ 'core/treeview/node' ];
  19. } );
  20. describe( 'constructor', () => {
  21. it( 'should create element without attributes', () => {
  22. let element = new Element( 'elem' );
  23. let parent = new Element( 'parent', [], [ element ] );
  24. expect( element ).to.be.an.instanceof( Node );
  25. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  26. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  27. expect( getIteratorCount( element.getAttrs() ) ).to.equal( 0 );
  28. } );
  29. it( 'should create element with attributes', () => {
  30. let attr = new Attribute( 'foo', 'bar' );
  31. let element = new Element( 'elem', [ attr ] );
  32. let parent = new Element( 'parent', [], [ element ] );
  33. expect( element ).to.be.an.instanceof( Node );
  34. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  35. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  36. expect( getIteratorCount( element.getAttrs() ) ).to.equal( 1 );
  37. expect( element.getAttr( attr.key ) ).to.equal( attr.value );
  38. } );
  39. it( 'should create element with children', () => {
  40. let element = new Element( 'elem', [], 'foo' );
  41. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  42. expect( element.getChildCount() ).to.equal( 3 );
  43. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  44. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  45. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  46. } );
  47. } );
  48. describe( 'insertChildren', () => {
  49. it( 'should add children to the element', () => {
  50. let element = new Element( 'elem', [], [ 'xy' ] );
  51. element.insertChildren( 1, 'foo' );
  52. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  53. expect( element.getChildCount() ).to.equal( 5 );
  54. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  55. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
  56. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  57. expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  58. expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
  59. } );
  60. } );
  61. describe( 'removeChildren', () => {
  62. it( 'should remove children from the element and return them as a NodeList', () => {
  63. let element = new Element( 'elem', [], [ 'foobar' ] );
  64. let o = element.getChild( 2 );
  65. let b = element.getChild( 3 );
  66. let a = element.getChild( 4 );
  67. let removed = element.removeChildren( 2, 3 );
  68. expect( element.getChildCount() ).to.equal( 3 );
  69. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  70. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  71. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  72. expect( o ).to.have.property( 'parent' ).that.is.null;
  73. expect( b ).to.have.property( 'parent' ).that.is.null;
  74. expect( a ).to.have.property( 'parent' ).that.is.null;
  75. expect( removed ).to.be.instanceof( NodeList );
  76. expect( removed.length ).to.equal( 3 );
  77. expect( removed.get( 0 ) ).to.equal( o );
  78. expect( removed.get( 1 ) ).to.equal( b );
  79. expect( removed.get( 2 ) ).to.equal( a );
  80. } );
  81. } );
  82. describe( 'getChildIndex', () => {
  83. it( 'should return child index', () => {
  84. let element = new Element( 'elem', [], [ 'bar' ] );
  85. let b = element.getChild( 0 );
  86. let a = element.getChild( 1 );
  87. let r = element.getChild( 2 );
  88. expect( element.getChildIndex( b ) ).to.equal( 0 );
  89. expect( element.getChildIndex( a ) ).to.equal( 1 );
  90. expect( element.getChildIndex( r ) ).to.equal( 2 );
  91. } );
  92. } );
  93. describe( 'getChildCount', () => {
  94. it( 'should return number of children', () => {
  95. let element = new Element( 'elem', [], [ 'bar' ] );
  96. expect( element.getChildCount() ).to.equal( 3 );
  97. } );
  98. } );
  99. } );