2016-06-13 18:23:23 +03:00
|
|
|
/**
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-06-13 18:23:23 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-06-13 18:23:23 +03:00
|
|
|
*
|
2018-05-11 05:06:46 +03:00
|
|
|
* @format
|
2017-11-02 16:14:11 +03:00
|
|
|
* @emails oncall+react_native
|
2016-06-13 18:23:23 +03:00
|
|
|
*/
|
2018-05-11 05:06:46 +03:00
|
|
|
|
2016-08-09 16:32:41 +03:00
|
|
|
'use strict';
|
2016-06-13 18:23:23 +03:00
|
|
|
|
|
|
|
describe('truncate', () => {
|
2019-05-08 18:44:25 +03:00
|
|
|
const truncate = require('../truncate');
|
2016-06-13 18:23:23 +03:00
|
|
|
|
2016-08-09 16:32:41 +03:00
|
|
|
it('should truncate', () => {
|
2018-05-11 05:06:46 +03:00
|
|
|
expect(truncate('Hello, world.', 5)).toBe('He...');
|
2016-06-13 18:23:23 +03:00
|
|
|
});
|
2019-03-15 14:31:48 +03:00
|
|
|
|
2016-08-09 16:32:41 +03:00
|
|
|
it('should not truncate', () => {
|
2018-05-11 05:06:46 +03:00
|
|
|
expect(truncate('Hello, world.', 50)).toBe('Hello, world.');
|
2016-06-13 18:23:23 +03:00
|
|
|
});
|
2019-03-15 14:31:48 +03:00
|
|
|
|
2016-08-09 16:32:41 +03:00
|
|
|
it('should not truncate more than minDelta chars.', () => {
|
2018-05-11 05:06:46 +03:00
|
|
|
expect(truncate('Hello, world.', 7, {minDelta: 10})).toBe('Hello, world.');
|
|
|
|
expect(truncate('Hello, world.', 7, {minDelta: 9})).toBe('Hell...');
|
2016-06-13 18:23:23 +03:00
|
|
|
});
|
2019-03-15 14:31:48 +03:00
|
|
|
|
2016-08-09 16:32:41 +03:00
|
|
|
it('should break in the middle of words', () => {
|
2018-05-11 05:06:46 +03:00
|
|
|
expect(
|
|
|
|
truncate('Hello, world. How are you?', 18, {breakOnWords: false}),
|
|
|
|
).toBe('Hello, world. H...');
|
|
|
|
expect(
|
|
|
|
truncate('Hello, world.\nHow are you?', 18, {breakOnWords: false}),
|
|
|
|
).toBe('Hello, world.\nHo...');
|
2016-06-13 18:23:23 +03:00
|
|
|
});
|
2019-03-15 14:31:48 +03:00
|
|
|
|
|
|
|
it('should add another character if the truncations happens in the middle of a wide char', () => {
|
|
|
|
expect(
|
|
|
|
truncate('Hello, world. āA weird character', 18, {breakOnWords: false}),
|
|
|
|
).toBe('Hello, world. āA...');
|
|
|
|
});
|
|
|
|
|
2016-08-09 16:32:41 +03:00
|
|
|
it('should break at word boundaries', () => {
|
2018-05-11 05:06:46 +03:00
|
|
|
expect(
|
|
|
|
truncate('Hello, world. How are you?', 18, {breakOnWords: true}),
|
|
|
|
).toBe('Hello, world....');
|
|
|
|
expect(
|
|
|
|
truncate('Hello, world.\nHow are you?', 18, {breakOnWords: true}),
|
|
|
|
).toBe('Hello, world....');
|
2016-06-13 18:23:23 +03:00
|
|
|
});
|
2019-03-15 14:31:48 +03:00
|
|
|
|
2016-08-09 16:32:41 +03:00
|
|
|
it('should uses custom elipses', () => {
|
2018-05-11 05:06:46 +03:00
|
|
|
expect(truncate('Hello, world.', 9, {elipsis: '·'})).toBe(
|
|
|
|
'He·',
|
|
|
|
);
|
2016-06-13 18:23:23 +03:00
|
|
|
});
|
2019-03-15 14:31:48 +03:00
|
|
|
|
2018-05-11 05:06:46 +03:00
|
|
|
it("shouldn't barf with weird input", () => {
|
|
|
|
expect(truncate('Hello, world.', 0)).toBe('Hello,...');
|
|
|
|
expect(truncate('Hello, world.', -132)).toBe('...');
|
|
|
|
expect(truncate('', 0)).toBe('');
|
|
|
|
expect(truncate(null, 0)).toBe(null);
|
2016-06-13 18:23:23 +03:00
|
|
|
});
|
|
|
|
});
|