1 import truncate, { DEFAULT_TRUNCATE_OMISSION } from './truncate';
3 describe('truncate()', () => {
4 it('returns empty sting if provided string is empty', () => {
5 expect(truncate('', 0)).toEqual('');
6 expect(truncate('', 1)).toEqual('');
7 expect(truncate('', 2)).toEqual('');
10 it('truncates to required length', () => {
13 const result = truncate('abcd', length);
15 expect(result.length).toEqual(length);
18 describe('charsToDisplay', () => {
19 it('defaults to 50', () => {
20 const str = 'a'.repeat(51);
22 const result = truncate(str);
24 expect(result.length).toBe(50);
27 it('returns inputted string if charsToDisplay is the length of the string', () => {
28 const testStrings = ['a', 'ab', 'abc'];
30 const results = testStrings.map((str) => truncate(str, str.length));
32 expect(results).toStrictEqual(testStrings);
35 it('returns inputted string if charsToDisplay is more than the length of the string', () => {
38 const result = truncate(str, str.length + 1);
40 expect(result).toBe(str);
43 it('returns truncated string if charsToDisplay is less than the length of the string', () => {
46 const result = truncate(str, str.length - 1);
48 expect(result).toBe('123' + DEFAULT_TRUNCATE_OMISSION);
52 describe('omission', () => {
53 it('uses default omission if not provided', () => {
54 const result = truncate('ab', 1);
56 expect(result).toEqual(DEFAULT_TRUNCATE_OMISSION);
59 it('uses provided omission', () => {
60 const omission = 'omission';
62 const result = truncate('ab', 1, omission);
64 expect(result).toEqual(omission);