text.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Node from '../../src/view/node';
  6. import Text from '../../src/view/text';
  7. describe( 'Text', () => {
  8. describe( 'constructor()', () => {
  9. it( 'should create element without attributes', () => {
  10. const text = new Text( 'foo' );
  11. expect( text ).to.be.an.instanceof( Node );
  12. expect( text.data ).to.equal( 'foo' );
  13. expect( text ).to.have.property( 'parent' ).that.is.null;
  14. } );
  15. } );
  16. describe( 'is()', () => {
  17. let text;
  18. before( () => {
  19. text = new Text( 'foo' );
  20. } );
  21. it( 'should return true for node, text', () => {
  22. expect( text.is( 'node' ) ).to.be.true;
  23. expect( text.is( 'view:node' ) ).to.be.true;
  24. expect( text.is( 'text' ) ).to.be.true;
  25. expect( text.is( 'view:text' ) ).to.be.true;
  26. } );
  27. it( 'should return false for other accept values', () => {
  28. expect( text.is( 'textProxy' ) ).to.be.false;
  29. expect( text.is( 'view:textProxy' ) ).to.be.false;
  30. expect( text.is( 'element' ) ).to.be.false;
  31. expect( text.is( 'view:element' ) ).to.be.false;
  32. expect( text.is( 'containerElement' ) ).to.be.false;
  33. expect( text.is( 'attributeElement' ) ).to.be.false;
  34. expect( text.is( 'uiElement' ) ).to.be.false;
  35. expect( text.is( 'emptyElement' ) ).to.be.false;
  36. expect( text.is( 'rootElement' ) ).to.be.false;
  37. expect( text.is( 'documentFragment' ) ).to.be.false;
  38. expect( text.is( 'model:text' ) ).to.be.false;
  39. expect( text.is( 'model:node' ) ).to.be.false;
  40. } );
  41. } );
  42. describe( '_clone()', () => {
  43. it( 'should return new text with same data', () => {
  44. const text = new Text( 'foo bar' );
  45. const clone = text._clone();
  46. expect( clone ).to.not.equal( text );
  47. expect( clone.data ).to.equal( text.data );
  48. } );
  49. } );
  50. describe( 'isSimilar', () => {
  51. const text = new Text( 'foo' );
  52. it( 'should return false when comparing to non-text', () => {
  53. expect( text.isSimilar( null ) ).to.be.false;
  54. expect( text.isSimilar( {} ) ).to.be.false;
  55. } );
  56. it( 'should return true when the same text node is provided', () => {
  57. expect( text.isSimilar( text ) ).to.be.true;
  58. } );
  59. it( 'should return true when data is the same', () => {
  60. const other = new Text( 'foo' );
  61. expect( text.isSimilar( other ) ).to.be.true;
  62. } );
  63. it( 'should return false when data is not the same', () => {
  64. const other = text._clone();
  65. other._data = 'not-foo';
  66. expect( text.isSimilar( other ) ).to.be.false;
  67. } );
  68. } );
  69. describe( 'setText', () => {
  70. it( 'should change the text', () => {
  71. const text = new Text( 'foo' );
  72. text._data = 'bar';
  73. expect( text.data ).to.equal( 'bar' );
  74. } );
  75. it( 'works when using addition assignment operator (+=)', () => {
  76. const foo = new Text( 'foo' );
  77. const bar = new Text( 'bar' );
  78. foo._data += bar.data;
  79. expect( foo.data ).to.equal( 'foobar' );
  80. } );
  81. } );
  82. } );