8
0

attributelist.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'core/treemodel/attributelist',
  11. 'core/treemodel/attribute'
  12. );
  13. describe( 'AttributeList', () => {
  14. let AttributeList, Attribute;
  15. before( () => {
  16. AttributeList = modules[ 'core/treemodel/attributelist' ];
  17. Attribute = modules[ 'core/treemodel/attribute' ];
  18. } );
  19. let list, attrFooBar;
  20. beforeEach( () => {
  21. list = new AttributeList();
  22. attrFooBar = new Attribute( 'foo', 'bar' );
  23. } );
  24. describe( 'setAttr', () => {
  25. it( 'should insert an attribute', () => {
  26. list.setAttr( attrFooBar );
  27. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
  28. expect( list.getAttr( attrFooBar.key ) ).to.equal( attrFooBar.value );
  29. } );
  30. it( 'should overwrite attribute with the same key', () => {
  31. list.setAttr( attrFooBar );
  32. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
  33. expect( list.getAttr( 'foo' ) ).to.equal( 'bar' );
  34. let attrFooXyz = new Attribute( 'foo', 'xyz' );
  35. list.setAttr( attrFooXyz );
  36. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
  37. expect( list.getAttr( 'foo' ) ).to.equal( 'xyz' );
  38. } );
  39. } );
  40. describe( 'setAttrsTo', () => {
  41. it( 'should remove all attributes and set passed ones', () => {
  42. list.setAttr( attrFooBar );
  43. let attrs = [ new Attribute( 'abc', true ), new Attribute( 'xyz', false ) ];
  44. list.setAttrsTo( attrs );
  45. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 2 );
  46. expect( list.getAttr( 'foo' ) ).to.be.null;
  47. expect( list.getAttr( 'abc' ) ).to.be.true;
  48. expect( list.getAttr( 'xyz' ) ).to.be.false;
  49. } );
  50. it( 'should copy attributes array, not pass by reference', () => {
  51. let attrs = [ new Attribute( 'attr', true ) ];
  52. list.setAttrsTo( attrs );
  53. attrs.pop();
  54. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
  55. } );
  56. } );
  57. describe( 'getAttr', () => {
  58. beforeEach( () => {
  59. list.setAttr( attrFooBar );
  60. } );
  61. it( 'should return attribute value if key of previously set attribute has been passed', () => {
  62. expect( list.getAttr( 'foo' ) ).to.equal( attrFooBar.value );
  63. } );
  64. it( 'should return null if attribute with given key has not been found', () => {
  65. expect( list.getAttr( 'bar' ) ).to.be.null;
  66. } );
  67. } );
  68. describe( 'removeAttr', () => {
  69. it( 'should remove an attribute', () => {
  70. let attrA = new Attribute( 'a', 'A' );
  71. let attrB = new Attribute( 'b', 'B' );
  72. let attrC = new Attribute( 'c', 'C' );
  73. list.setAttr( attrA );
  74. list.setAttr( attrB );
  75. list.setAttr( attrC );
  76. list.removeAttr( attrB.key );
  77. expect( getIteratorCount( list.getAttrs() ) ).to.equal( 2 );
  78. expect( list.getAttr( attrA.key ) ).to.equal( attrA.value );
  79. expect( list.getAttr( attrC.key ) ).to.equal( attrC.value );
  80. expect( list.getAttr( attrB.key ) ).to.be.null;
  81. } );
  82. } );
  83. describe( 'hasAttr', () => {
  84. it( 'should check attribute by key', () => {
  85. list.setAttr( attrFooBar );
  86. expect( list.hasAttr( 'foo' ) ).to.be.true;
  87. } );
  88. it( 'should return false if attribute was not found by key', () => {
  89. expect( list.hasAttr( 'bar' ) ).to.be.false;
  90. } );
  91. it( 'should check attribute by object', () => {
  92. list.setAttr( attrFooBar );
  93. expect( list.hasAttr( attrFooBar ) ).to.be.true;
  94. } );
  95. it( 'should return false if attribute was not found by object', () => {
  96. expect( list.hasAttr( attrFooBar ) ).to.be.false;
  97. } );
  98. } );
  99. describe( 'getAttrs', () => {
  100. it( 'should return all set attributes', () => {
  101. let attrA = new Attribute( 'a', 'A' );
  102. let attrB = new Attribute( 'b', 'B' );
  103. let attrC = new Attribute( 'c', 'C' );
  104. list.setAttrsTo( [
  105. attrA,
  106. attrB,
  107. attrC
  108. ] );
  109. list.removeAttr( attrB.key );
  110. let attrsIt = list.getAttrs();
  111. let attrs = [];
  112. for ( let attr of attrsIt ) {
  113. attrs.push( attr );
  114. }
  115. expect( [ attrA, attrC ] ).to.deep.equal( attrs );
  116. } );
  117. } );
  118. } );