|
| 1 | +import { trimEnd, trimStart, truncate } from './strings.js'; |
| 2 | + |
| 3 | +describe('helpers', () => { |
| 4 | + describe('trimStart', () => { |
| 5 | + it('should remove invisible characters from the start of a string', () => { |
| 6 | + const input = '\u034f\u034fHello'; |
| 7 | + const result = trimStart(input); |
| 8 | + expect(result).toBe('Hello'); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should return the same string if no invisible characters are at the start', () => { |
| 12 | + const input = 'Hello'; |
| 13 | + const result = trimStart(input); |
| 14 | + expect(result).toBe('Hello'); |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('trimEnd', () => { |
| 19 | + it('should remove invisible characters from the end of a string', () => { |
| 20 | + const input = 'Hello\u034f\u034f'; |
| 21 | + const result = trimEnd(input); |
| 22 | + expect(result).toBe('Hello'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should return the same string if no invisible characters are at the end', () => { |
| 26 | + const input = 'Hello'; |
| 27 | + const result = trimEnd(input); |
| 28 | + expect(result).toBe('Hello'); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('truncate', () => { |
| 33 | + it('should truncate a string to the specified maxLength and append "..."', () => { |
| 34 | + const input = 'This is a long string.'; |
| 35 | + const result = truncate(input, 10); |
| 36 | + expect(result).toBe('This is...'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return the original string if it is shorter than maxLength', () => { |
| 40 | + const input = 'Short'; |
| 41 | + const result = truncate(input, 10); |
| 42 | + expect(result).toBe('Short'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return the original string if maxLength is not provided', () => { |
| 46 | + const input = 'No max length'; |
| 47 | + const result = truncate(input); |
| 48 | + expect(result).toBe('No max length'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return the original string if maxLength is 0', () => { |
| 52 | + const input = 'No max length'; |
| 53 | + const result = truncate(input, 0); |
| 54 | + expect(result).toBe('No max length'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle emojis correctly', () => { |
| 58 | + const input = 'π This is a long string with emojis.'; |
| 59 | + const result = truncate(input, 12); |
| 60 | + expect(result).toBe('π This is...'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle emojis at the truncation point', () => { |
| 64 | + const emoji = 'π©ββ€οΈβπβπ¨'; |
| 65 | + expect(emoji).toHaveLength(11); |
| 66 | + const input = `Truncate this ${emoji} string.`; |
| 67 | + |
| 68 | + expect(truncate(input, 16)).toBe('Truncate this...'); |
| 69 | + expect(truncate(input, 17)).toBe('Truncate this...'); |
| 70 | + expect(truncate(input, 18)).toBe('Truncate this π©ββ€οΈβπβπ¨...'); |
| 71 | + expect(truncate(input, 19)).toBe('Truncate this π©ββ€οΈβπβπ¨...'); |
| 72 | + expect(truncate(input, 20)).toBe('Truncate this π©ββ€οΈβπβπ¨ s...'); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments