modelconsumable.js 5.2 KB

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