attributelist.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. 'use strict';
  7. import AttributeList from '/ckeditor5/core/treemodel/attributelist.js';
  8. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  9. describe( 'AttributeList', () => {
  10. let list, attrFooBar;
  11. beforeEach( () => {
  12. list = new AttributeList();
  13. attrFooBar = new Attribute( 'foo', 'bar' );
  14. } );
  15. it( 'should extend Map', () => {
  16. expect( list ).to.be.instanceof( Map );
  17. } );
  18. describe( 'constructor', () => {
  19. it( 'should initialize list with passed attributes', () => {
  20. list = new AttributeList( [ attrFooBar ] );
  21. expect( list.size ).to.equal( 1 );
  22. expect( list.has( 'foo' ) ).to.be.true;
  23. expect( list.get( 'foo' ).value ).to.equal( 'bar' );
  24. } );
  25. it( 'should copy passed AttributeList', () => {
  26. list = new AttributeList( [ attrFooBar ] );
  27. let copy = new AttributeList( list );
  28. expect( copy.size ).to.equal( 1 );
  29. expect( copy.has( 'foo' ) ).to.be.true;
  30. expect( copy.get( 'foo' ).value ).to.equal( 'bar' );
  31. } );
  32. } );
  33. describe( 'iterator', () => {
  34. it( 'should iterate over all added attributes', () => {
  35. let attrAbcXyz = new Attribute( 'abc', 'xyz' );
  36. let attrTestTrue = new Attribute( 'test', true );
  37. list = new AttributeList( [ attrFooBar, attrAbcXyz, attrTestTrue ] );
  38. list.delete( 'test' );
  39. let it = list[ Symbol.iterator ]();
  40. let step = it.next();
  41. expect( step.done ).to.be.false;
  42. expect( step.value ).to.equal( attrFooBar );
  43. step = it.next();
  44. expect( step.done ).to.be.false;
  45. expect( step.value ).to.equal( attrAbcXyz );
  46. step = it.next();
  47. expect( step.done ).to.be.true;
  48. } );
  49. } );
  50. describe( 'getValue', () => {
  51. it( 'should return value of set attribute for given key', () => {
  52. list.set( attrFooBar );
  53. expect( list.getValue( 'foo' ) ).to.equal( 'bar' );
  54. } );
  55. it( 'should return null if attribute with given key is not set', () => {
  56. expect( list.getValue( 'foo' ) ).to.be.null;
  57. } );
  58. } );
  59. describe( 'set', () => {
  60. it( 'should insert given attribute', () => {
  61. list.set( attrFooBar );
  62. expect( list.size ).to.equal( 1 );
  63. expect( list.getValue( 'foo' ) ).to.equal( 'bar' );
  64. } );
  65. it( 'should overwrite attribute with the same key', () => {
  66. list.set( attrFooBar );
  67. expect( list.size ).to.equal( 1 );
  68. expect( list.getValue( 'foo' ) ).to.equal( 'bar' );
  69. let attrFooXyz = new Attribute( 'foo', 'xyz' );
  70. list.set( attrFooXyz );
  71. expect( list.size ).to.equal( 1 );
  72. expect( list.getValue( 'foo' ) ).to.equal( 'xyz' );
  73. } );
  74. } );
  75. describe( 'setTo', () => {
  76. it( 'should remove all attributes from the list and set given ones', () => {
  77. list.set( attrFooBar );
  78. list.setTo( [ new Attribute( 'abc', true ), new Attribute( 'bar', false ) ] );
  79. expect( list.has( 'foo' ) ).to.be.false;
  80. expect( list.getValue( 'abc' ) ).to.be.true;
  81. expect( list.getValue( 'bar' ) ).to.be.false;
  82. } );
  83. } );
  84. describe( 'has', () => {
  85. it( 'should return true if list contains given attribute (same key and value)', () => {
  86. list.set( attrFooBar );
  87. expect( list.has( attrFooBar ) ).to.be.true;
  88. } );
  89. it( 'should return true if list contains an attribute with given key', () => {
  90. list.set( attrFooBar );
  91. expect( list.has( 'foo' ) ).to.be.true;
  92. } );
  93. it( 'should return false if list does not contain given attribute', () => {
  94. list.set( attrFooBar );
  95. expect( list.has( new Attribute( 'abc', true ) ) ).to.be.false;
  96. } );
  97. it( 'should return false if list contains given attribute but value differs', () => {
  98. list.set( attrFooBar );
  99. expect( list.has( new Attribute( 'foo', 'foo' ) ) ).to.be.false;
  100. } );
  101. it( 'should return false if list does not contain an attribute with given key', () => {
  102. list.set( attrFooBar );
  103. expect( list.has( 'abc' ) ).to.be.false;
  104. } );
  105. } );
  106. describe( 'isEqual', () => {
  107. it( 'should return false if lists have different size', () => {
  108. let attrAbcXyz = new Attribute( 'abc', 'xyz' );
  109. list.setTo( [ attrFooBar, attrAbcXyz ] );
  110. let other = new AttributeList( [ attrFooBar ] );
  111. expect( list.isEqual( other ) ).to.be.false;
  112. expect( other.isEqual( list ) ).to.be.false;
  113. } );
  114. it( 'should return false if lists have different attributes', () => {
  115. let attrAbcXyz = new Attribute( 'abc', 'xyz' );
  116. list.setTo( [ attrFooBar ] );
  117. let other = new AttributeList( [ attrAbcXyz ] );
  118. expect( list.isEqual( other ) ).to.be.false;
  119. expect( other.isEqual( list ) ).to.be.false;
  120. } );
  121. it( 'should return false if lists have same attributes but different values for them', () => {
  122. let attrAbcXyz = new Attribute( 'abc', 'xyz' );
  123. let attrFooTrue = new Attribute( 'foo', true );
  124. list.setTo( [ attrFooBar, attrAbcXyz ] );
  125. let other = new AttributeList( [ attrFooTrue, attrAbcXyz ] );
  126. expect( list.isEqual( other ) ).to.be.false;
  127. expect( other.isEqual( list ) ).to.be.false;
  128. } );
  129. it( 'should return true if lists have same attributes and same values for them', () => {
  130. let attrAbcXyz = new Attribute( 'abc', 'xyz' );
  131. list.setTo( [ attrFooBar, attrAbcXyz ] );
  132. // Note different order of attributes.
  133. let other = new AttributeList( [ attrAbcXyz, attrFooBar ] );
  134. expect( list.isEqual( other ) ).to.be.true;
  135. expect( other.isEqual( list ) ).to.be.true;
  136. } );
  137. } );
  138. } );