attributelist.js 4.1 KB

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