8
0

modelconsumable.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelConsumable from '../../src/conversion/modelconsumable';
  6. import ModelElement from '../../src/model/element';
  7. import ModelTextProxy from '../../src/model/textproxy';
  8. import ModelText from '../../src/model/text';
  9. describe( 'ModelConsumable', () => {
  10. let modelConsumable, modelElement;
  11. beforeEach( () => {
  12. modelConsumable = new ModelConsumable();
  13. modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  14. } );
  15. describe( 'add', () => {
  16. it( 'should add consumable value from given element of given type', () => {
  17. modelConsumable.add( modelElement, 'type' );
  18. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.true;
  19. } );
  20. it( 'should store multiple values for one element', () => {
  21. modelConsumable.add( modelElement, 'typeA' );
  22. modelConsumable.add( modelElement, 'typeB' );
  23. modelConsumable.add( modelElement, 'typeC' );
  24. expect( modelConsumable.test( modelElement, 'typeA' ) ).to.be.true;
  25. expect( modelConsumable.test( modelElement, 'typeB' ) ).to.be.true;
  26. expect( modelConsumable.test( modelElement, 'typeC' ) ).to.be.true;
  27. } );
  28. it( 'should correctly add text proxy instances', () => {
  29. const modelTextProxy = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  30. modelConsumable.add( modelTextProxy, 'type' );
  31. expect( modelConsumable.test( modelTextProxy, 'type' ) ).to.be.true;
  32. } );
  33. } );
  34. describe( 'consume', () => {
  35. it( 'should remove consumable value of given type for given element and return true', () => {
  36. modelConsumable.add( modelElement, 'type' );
  37. const result = modelConsumable.consume( modelElement, 'type' );
  38. expect( result ).to.be.true;
  39. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.false;
  40. } );
  41. it( 'should return false if given type of consumable was not added for given element', () => {
  42. const result = modelConsumable.consume( modelElement, 'type' );
  43. expect( result ).to.be.false;
  44. } );
  45. it( 'should correctly consume text proxy instances', () => {
  46. const proxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  47. const proxy1To5 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 4 );
  48. const proxyOther1To4 = new ModelTextProxy( new ModelText( 'abcdef' ), 1, 3 );
  49. modelConsumable.add( proxy1To4, 'type' );
  50. expect( modelConsumable.consume( proxy1To5, 'type' ) ).to.be.false;
  51. expect( modelConsumable.consume( proxyOther1To4, 'type' ) ).to.be.false;
  52. const equalProxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  53. const result = modelConsumable.consume( equalProxy1To4, 'type' );
  54. expect( result ).to.be.true;
  55. expect( modelConsumable.test( proxy1To4, 'type' ) ).to.be.false;
  56. } );
  57. } );
  58. describe( 'revert', () => {
  59. it( 'should re-add consumable value if it was already consumed and return true', () => {
  60. modelConsumable.add( modelElement, 'type' );
  61. modelConsumable.consume( modelElement, 'type' );
  62. const result = modelConsumable.revert( modelElement, 'type' );
  63. expect( result ).to.be.true;
  64. expect( modelConsumable.test( modelElement, 'type' ) ).to.be.true;
  65. } );
  66. it( 'should return false if consumable value has not been yet consumed', () => {
  67. modelConsumable.add( modelElement, 'type' );
  68. const result = modelConsumable.revert( modelElement, 'type' );
  69. expect( result ).to.be.false;
  70. } );
  71. it( 'should return null if consumable value of given type has never been added for given element', () => {
  72. const result = modelConsumable.revert( modelElement, 'type' );
  73. expect( result ).to.be.null;
  74. } );
  75. it( 'should correctly revert text proxy instances', () => {
  76. const modelTextProxy = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  77. modelConsumable.add( modelTextProxy, 'type' );
  78. modelConsumable.consume( modelTextProxy, 'type' );
  79. const result = modelConsumable.revert( modelTextProxy, 'type' );
  80. expect( result ).to.be.true;
  81. expect( modelConsumable.test( modelTextProxy, 'type' ) ).to.be.true;
  82. } );
  83. } );
  84. describe( 'test', () => {
  85. it( 'should return null if consumable value of given type has never been added for given element', () => {
  86. expect( modelConsumable.test( modelElement, 'typeA' ) ).to.be.null;
  87. modelConsumable.add( modelElement, 'typeA' );
  88. expect( modelConsumable.test( modelElement, 'typeB' ) ).to.be.null;
  89. } );
  90. it( 'should correctly test for text proxy instances', () => {
  91. const proxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  92. const proxy1To5 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 4 );
  93. const proxyOther1To4 = new ModelTextProxy( new ModelText( 'abcdef' ), 1, 3 );
  94. const equalProxy1To4 = new ModelTextProxy( modelElement.getChild( 0 ), 1, 3 );
  95. modelConsumable.add( proxy1To4, 'type' );
  96. expect( modelConsumable.test( proxy1To4, 'type' ) ).to.be.true;
  97. expect( modelConsumable.test( proxy1To4, 'otherType' ) ).to.be.null;
  98. expect( modelConsumable.test( proxy1To5, 'type' ) ).to.be.null;
  99. expect( modelConsumable.test( proxyOther1To4, 'type' ) ).to.be.null;
  100. expect( modelConsumable.test( equalProxy1To4, 'type' ) ).to.be.true;
  101. } );
  102. } );
  103. } );