text.js 3.1 KB

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