/**
* Internal dependencies
*/
import { truncateRenderableHTML } 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 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
...'
);
} );
} );