text.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Text from '../../src/model/text';
  6. import Node from '../../src/model/node';
  7. import { jsonParseStringify } from '../../tests/model/_utils/utils';
  8. describe( 'Text', () => {
  9. describe( 'constructor()', () => {
  10. it( 'should create text node without attributes', () => {
  11. let text = new Text( 'bar', { bold: true } );
  12. expect( text ).to.be.instanceof( Node );
  13. expect( text ).to.have.property( 'data' ).that.equals( 'bar' );
  14. expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  15. } );
  16. it( 'should create empty text object', () => {
  17. let empty1 = new Text();
  18. let empty2 = new Text( '' );
  19. expect( empty1.data ).to.equal( '' );
  20. expect( empty2.data ).to.equal( '' );
  21. } );
  22. } );
  23. describe( 'offsetSize', () => {
  24. it( 'should be equal to the number of characters in text node', () => {
  25. expect( new Text( '' ).offsetSize ).to.equal( 0 );
  26. expect( new Text( 'abc' ).offsetSize ).to.equal( 3 );
  27. } );
  28. } );
  29. describe( 'clone', () => {
  30. it( 'should return a new Text instance, with data and attributes equal to cloned text node', () => {
  31. let text = new Text( 'foo', { bold: true } );
  32. let copy = text.clone();
  33. expect( copy.data ).to.equal( 'foo' );
  34. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  35. } );
  36. } );
  37. describe( 'toJSON', () => {
  38. it( 'should serialize text node', () => {
  39. let text = new Text( 'foo', { bold: true } );
  40. expect( jsonParseStringify( text ) ).to.deep.equal( {
  41. attributes: [ [ 'bold', true ] ],
  42. data: 'foo'
  43. } );
  44. } );
  45. } );
  46. describe( 'fromJSON', () => {
  47. it( 'should create text node', () => {
  48. let text = new Text( 'foo', { bold: true } );
  49. let serialized = jsonParseStringify( text );
  50. let deserialized = Text.fromJSON( serialized );
  51. expect( deserialized.data ).to.equal( 'foo' );
  52. expect( Array.from( deserialized.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  53. } );
  54. it( 'should support unicode', () => {
  55. let textQ = new Text( 'நி' );
  56. let json = jsonParseStringify( textQ );
  57. expect( json ).to.deep.equal( {
  58. data: 'நி'
  59. } );
  60. let deserialized = Text.fromJSON( json );
  61. expect( deserialized.data ).to.equal( 'நி' );
  62. } );
  63. } );
  64. } );