/**
* Internal dependencies
*/
import { truncateRenderableHTML, truncate } from '../utils';
describe( 'truncateRenderableHTML', () => {
test( 'it should recover malformed HTML when truncated', () => {
const malformed = '
this is a test sentence';
expect( truncateRenderableHTML( malformed, 7 ) ).toBe(
'
this is
...'
);
} );
test( 'it should not truncate if the length does not exceed', () => {
const sample = '
this is a test sentence
';
expect( truncateRenderableHTML( sample, sample.length ) ).toBe(
sample
);
} );
test( 'it should consider as a single space', () => {
const samplewithSpace = '
this is
';
// this(4 chars) + space (1 char) + (2 chars) = 7
expect( truncateRenderableHTML( samplewithSpace, 7 ) ).toBe(
'
this
...'
);
} );
test( 'it should not count nested tags as text', () => {
const sampleWithNestedTags = '
this
is
';
// this (4 chars) + space (1 char) + space (1char) + is (2 chars)) = 8
expect( truncateRenderableHTML( sampleWithNestedTags, 8 ) ).toBe(
'
this
is
'
);
} );
test( 'it should truncate properly with nested tags', () => {
const sampleWithNestedTags = '
';
// this (4 chars) + space (1 char) + is (2char) + space (1 char) = 7
expect( truncateRenderableHTML( sampleWithNestedTags, 7 ) ).toBe(
'
this is
...'
);
} );
test( 'it should work with unicode text', () => {
const sampleWithUnicode = '
테스트 입니다.
';
expect( truncateRenderableHTML( sampleWithUnicode, 3 ) ).toBe(
'
테스트
...'
);
} );
test( 'it should preserve whole words when truncated', () => {
const sample = '
this is a test sentence
';
// it should return 'this is a' (9 chars) when length 11 is given
// since 'this is a t' (11 chars) cannot include 'test' word without
// breaking the word.
expect( truncateRenderableHTML( sample, 11 ) ).toBe(
'
this is a
...'
);
} );
test( 'it should preserve whole words with emoji when truncated', () => {
const sample = '
🏳️🌈this is a test sentence
';
// it should return '🏳️🌈this is a' (10 chars) when length 12 is given
// since '🏳️🌈this is a t' (12 chars) cannot include 'test' word without
// breaking the word.
expect( truncateRenderableHTML( sample, 12 ) ).toBe(
'
🏳️🌈this is a
...'
);
} );
test( 'it should work with multi-char letters', () => {
const sampleWithUnicode = '
🏳️🌈
';
expect( truncateRenderableHTML( sampleWithUnicode, 1 ) ).toBe(
'
🏳️🌈
'
);
const hindiSample = '
अनुच्छेद
';
expect( truncateRenderableHTML( hindiSample, 5 ) ).toBe(
'
अनुच्छेद
'
);
expect( truncateRenderableHTML( hindiSample, 3 ) ).toBe(
'
अनुच्
...'
);
const demonicSample = '
Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞
';
expect( truncateRenderableHTML( demonicSample, 6 ) ).toBe(
'
Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞
'
);
} );
} );
describe( 'truncate', () => {
it( 'should truncate letters and return truncated string', () => {
expect( truncate( 'this is a test sentence'.split( '' ), 4 ) ).toBe(
'this'
);
} );
it( 'should not contain end-space', () => {
expect( truncate( 'this is a test sentence'.split( '' ), 5 ) ).toBe(
'this'
);
} );
it( 'should preserve whole words', () => {
// "this i" doesn't preserve whole words, so it should be truncated to "this"
expect( truncate( 'this is a test sentence'.split( '' ), 6 ) ).toBe(
'this'
);
expect( truncate( 'this is a test sentence'.split( '' ), 7 ) ).toBe(
'this is'
);
} );
} );