|
| 1 | +import { annotateHighlightedText } from '../utils'; |
| 2 | + |
| 3 | +const SAMPLE_INPUT = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'; |
| 4 | + |
| 5 | +describe('annotateHighlightedText', () => { |
| 6 | + it('should highlight matching substrings', () => { |
| 7 | + const highlight = 'ipsum'; |
| 8 | + |
| 9 | + const result = annotateHighlightedText(SAMPLE_INPUT, highlight); |
| 10 | + |
| 11 | + expect(result).toEqual([ |
| 12 | + { text: 'Lorem ', highlighted: false }, |
| 13 | + { text: 'ipsum', highlighted: true }, |
| 14 | + { text: ' dolor sit amet, consectetur adipiscing elit', highlighted: false }, |
| 15 | + ]); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should handle multiple matches', () => { |
| 19 | + const highlight = 'ip'; |
| 20 | + |
| 21 | + const result = annotateHighlightedText(SAMPLE_INPUT, highlight); |
| 22 | + |
| 23 | + expect(result).toEqual([ |
| 24 | + { text: 'Lorem ', highlighted: false }, |
| 25 | + { text: 'ip', highlighted: true }, |
| 26 | + { text: 'sum dolor sit amet, consectetur ad', highlighted: false }, |
| 27 | + { text: 'ip', highlighted: true }, |
| 28 | + { text: 'iscing elit', highlighted: false }, |
| 29 | + ]); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should handle no matches', () => { |
| 33 | + const highlight = 'xyz'; |
| 34 | + |
| 35 | + const result = annotateHighlightedText(SAMPLE_INPUT, highlight); |
| 36 | + |
| 37 | + expect(result).toEqual([{ text: SAMPLE_INPUT, highlighted: false }]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should handle case-insensitive matching', () => { |
| 41 | + const highlight = 'DOLOR'; |
| 42 | + |
| 43 | + const result = annotateHighlightedText(SAMPLE_INPUT, highlight); |
| 44 | + |
| 45 | + expect(result).toEqual([ |
| 46 | + { text: 'Lorem ipsum ', highlighted: false }, |
| 47 | + { text: 'dolor', highlighted: true }, |
| 48 | + { text: ' sit amet, consectetur adipiscing elit', highlighted: false }, |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle empty highlight string', () => { |
| 53 | + const highlight = ''; |
| 54 | + |
| 55 | + const result = annotateHighlightedText(SAMPLE_INPUT, highlight); |
| 56 | + |
| 57 | + expect(result).toEqual([{ text: SAMPLE_INPUT, highlighted: false }]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle empty input string', () => { |
| 61 | + const input = ''; |
| 62 | + const highlight = 'test'; |
| 63 | + |
| 64 | + const result = annotateHighlightedText(input, highlight); |
| 65 | + |
| 66 | + expect(result).toEqual([]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should escape special characters in the highlight string', () => { |
| 70 | + const highlight = '[(){}?*^$|\\.]'; |
| 71 | + |
| 72 | + const result = annotateHighlightedText('test[(){}?*^$|\\.]test', highlight); |
| 73 | + |
| 74 | + expect(result).toEqual([ |
| 75 | + { text: 'test', highlighted: false }, |
| 76 | + { text: '[(){}?*^$|\\.]', highlighted: true }, |
| 77 | + { text: 'test', highlighted: false }, |
| 78 | + ]); |
| 79 | + }); |
| 80 | +}); |
0 commit comments