8
0

element.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'core/treeview/node',
  11. 'core/treeview/element'
  12. );
  13. describe( 'Element', () => {
  14. let ViewElement, Node;
  15. before( () => {
  16. ViewElement = modules[ 'core/treeview/element' ];
  17. Node = modules[ 'core/treeview/node' ];
  18. } );
  19. describe( 'constructor', () => {
  20. it( 'should create element without attributes', () => {
  21. const elem = new ViewElement( 'p' );
  22. expect( elem ).to.be.an.instanceof( Node );
  23. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  24. expect( elem ).to.have.property( 'parent' ).that.is.null;
  25. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 0 );
  26. } );
  27. it( 'should create element with attributes as plain object', () => {
  28. const elem = new ViewElement( 'p', { 'foo': 'bar' } );
  29. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  30. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 1 );
  31. expect( elem.getAttr( 'foo' ) ).to.equal( 'bar' );
  32. } );
  33. it( 'should create element with attributes as map', () => {
  34. const attrs = new Map();
  35. attrs.set( 'foo', 'bar' );
  36. const elem = new ViewElement( 'p', attrs );
  37. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  38. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 1 );
  39. expect( elem.getAttr( 'foo' ) ).to.equal( 'bar' );
  40. } );
  41. it( 'should create element with children', () => {
  42. const child = new ViewElement( 'p', { 'foo': 'bar' } );
  43. const parent = new ViewElement( 'div', [], [ child ] );
  44. expect( parent ).to.have.property( 'name' ).that.equals( 'div' );
  45. expect( parent.getChildCount() ).to.equal( 1 );
  46. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  47. } );
  48. } );
  49. describe( 'children manipulation methods', () => {
  50. let parent, e1, e2, e3, e4;
  51. beforeEach( () => {
  52. parent = new ViewElement( 'p' );
  53. e1 = new ViewElement( 'e1' );
  54. e2 = new ViewElement( 'e2' );
  55. e3 = new ViewElement( 'e3' );
  56. e4 = new ViewElement( 'e4' );
  57. } );
  58. describe( 'insertion', () => {
  59. it( 'should insert children', () => {
  60. parent.insertChildren( 0, [ e1, e3 ] );
  61. parent.insertChildren( 1, e2 );
  62. expect( parent.getChildCount() ).to.equal( 3 );
  63. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  64. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e2' );
  65. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'e3' );
  66. } );
  67. it( 'should append children', () => {
  68. parent.insertChildren( 0, e1 );
  69. parent.appendChildren( e2 );
  70. parent.appendChildren( e3 );
  71. expect( parent.getChildCount() ).to.equal( 3 );
  72. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  73. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e2' );
  74. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'e3' );
  75. } );
  76. } );
  77. describe( 'getChildIndex', () => {
  78. it( 'should return child index', () => {
  79. parent.appendChildren( e1 );
  80. parent.appendChildren( e2 );
  81. parent.appendChildren( e3 );
  82. expect( parent.getChildCount() ).to.equal( 3 );
  83. expect( parent.getChildIndex( e1 ) ).to.equal( 0 );
  84. expect( parent.getChildIndex( e2 ) ).to.equal( 1 );
  85. expect( parent.getChildIndex( e3 ) ).to.equal( 2 );
  86. } );
  87. } );
  88. describe( 'getChildren', () => {
  89. it( 'should renturn children iterator', () => {
  90. parent.appendChildren( e1 );
  91. parent.appendChildren( e2 );
  92. parent.appendChildren( e3 );
  93. const expected = [ e1, e2, e3 ];
  94. let i = 0;
  95. for ( let child of parent.getChildren() ) {
  96. expect( child ).to.equal( expected[ i ] );
  97. i++;
  98. }
  99. expect( i ).to.equal( 3 );
  100. } );
  101. } );
  102. describe( 'removeChildren', () => {
  103. it( 'should remove children', () => {
  104. parent.appendChildren( e1 );
  105. parent.appendChildren( e2 );
  106. parent.appendChildren( e3 );
  107. parent.appendChildren( e4 );
  108. parent.removeChildren( 1, 2 );
  109. expect( parent.getChildCount() ).to.equal( 2 );
  110. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  111. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e4' );
  112. expect( e1.parent ).to.equal( parent );
  113. expect( e2.parent ).to.be.null;
  114. expect( e3.parent ).to.be.null;
  115. expect( e4.parent ).equal( parent );
  116. } );
  117. } );
  118. } );
  119. describe( 'attributes manipulation methods', () => {
  120. let elem;
  121. beforeEach( () => {
  122. elem = new ViewElement( 'p' );
  123. } );
  124. describe( 'getAttr', () => {
  125. it( 'should return attribute', () => {
  126. elem.setAttr( 'foo', 'bar' );
  127. expect( elem.getAttr( 'foo' ) ).to.equal( 'bar' );
  128. expect( elem.getAttr( 'bom' ) ).to.not.be.ok;
  129. } );
  130. } );
  131. describe( 'hasAttr', () => {
  132. it( 'should return true if element has attribute', () => {
  133. elem.setAttr( 'foo', 'bar' );
  134. expect( elem.hasAttr( 'foo' ) ).to.be.true;
  135. expect( elem.hasAttr( 'bom' ) ).to.be.false;
  136. } );
  137. } );
  138. describe( 'getAttrKeys', () => {
  139. it( 'should return keys', () => {
  140. elem.setAttr( 'foo', true );
  141. elem.setAttr( 'bar', true );
  142. const expected = [ 'foo', 'bar' ];
  143. let i = 0;
  144. for ( let child of elem.getAttrKeys() ) {
  145. expect( child ).to.equal( expected[ i ] );
  146. i++;
  147. }
  148. expect( i ).to.equal( 2 );
  149. } );
  150. } );
  151. describe( 'removeAttr', () => {
  152. it( 'should remove attributes', () => {
  153. elem.setAttr( 'foo', true );
  154. expect( elem.hasAttr( 'foo' ) ).to.be.true;
  155. elem.removeAttr( 'foo' );
  156. expect( elem.hasAttr( 'foo' ) ).to.be.false;
  157. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 0 );
  158. } );
  159. } );
  160. } );
  161. } );