|
| 1 | +import {getTextPartsByHighlight, getArrayPartsByHighlight} from '../textUtils'; |
| 2 | + |
| 3 | +describe('Text', () => { |
| 4 | + describe('getTextPartsByHighlight', () => { |
| 5 | + it('should return the whole string as a single part when highlight string is undefined', () => { |
| 6 | + const result = getTextPartsByHighlight('Playground Screen', undefined); |
| 7 | + expect(result).toEqual([{string: 'Playground Screen', shouldHighlight: false}]); |
| 8 | + }); |
| 9 | + it('should return the whole string as a single part when highlight string is empty', () => { |
| 10 | + const result = getTextPartsByHighlight('Playground Screen', ''); |
| 11 | + expect(result).toEqual([{string: 'Playground Screen', shouldHighlight: false}]); |
| 12 | + }); |
| 13 | + it('should return the whole string as a single part when highlight string dont match', () => { |
| 14 | + const result = getTextPartsByHighlight('Playground Screen', 'aaa'); |
| 15 | + expect(result).toEqual([{string: 'Playground Screen', shouldHighlight: false}]); |
| 16 | + }); |
| 17 | + it('should break text to parts according to highlight string', () => { |
| 18 | + const result = getTextPartsByHighlight('Playground Screen', 'Scr'); |
| 19 | + expect(result).toEqual([ |
| 20 | + {string: 'Playground ', shouldHighlight: false}, |
| 21 | + {string: 'Scr', shouldHighlight: true}, |
| 22 | + {string: 'een', shouldHighlight: false} |
| 23 | + ]); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should handle case when highlight repeats more than once', () => { |
| 27 | + const result = getTextPartsByHighlight('Dancing in the Dark', 'Da'); |
| 28 | + expect(result).toEqual([ |
| 29 | + {string: 'Da', shouldHighlight: true}, |
| 30 | + {string: 'ncing in the ', shouldHighlight: false}, |
| 31 | + {string: 'Da', shouldHighlight: true}, |
| 32 | + {string: 'rk', shouldHighlight: false} |
| 33 | + ]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should be case-insensitive', () => { |
| 37 | + const result = getTextPartsByHighlight('Dancing in the Dark', 'da'); |
| 38 | + expect(result).toEqual([ |
| 39 | + {string: 'Da', shouldHighlight: true}, |
| 40 | + {string: 'ncing in the ', shouldHighlight: false}, |
| 41 | + {string: 'Da', shouldHighlight: true}, |
| 42 | + {string: 'rk', shouldHighlight: false} |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('Should handle special characters @', () => { |
| 47 | + const result = getTextPartsByHighlight('@ancing in the @ark', '@a'); |
| 48 | + expect(result).toEqual([ |
| 49 | + {string: '@a', shouldHighlight: true}, |
| 50 | + {string: 'ncing in the ', shouldHighlight: false}, |
| 51 | + {string: '@a', shouldHighlight: true}, |
| 52 | + {string: 'rk', shouldHighlight: false} |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it('Should handle special characters !', () => { |
| 57 | + const result = getTextPartsByHighlight('!ancing in the !ark', '!a'); |
| 58 | + expect(result).toEqual([ |
| 59 | + {string: '!a', shouldHighlight: true}, |
| 60 | + {string: 'ncing in the ', shouldHighlight: false}, |
| 61 | + {string: '!a', shouldHighlight: true}, |
| 62 | + {string: 'rk', shouldHighlight: false} |
| 63 | + ]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('Should handle special characters starts with @', () => { |
| 67 | + const result = getTextPartsByHighlight('[email protected]', '@wix'); |
| 68 | + expect(result).toEqual([ |
| 69 | + {string: 'uilib', shouldHighlight: false}, |
| 70 | + {string: '@wix', shouldHighlight: true}, |
| 71 | + {string: '.com', shouldHighlight: false} |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('Should handle empty string.', () => { |
| 76 | + const result = getTextPartsByHighlight('@ancing in the @ark', ''); |
| 77 | + expect(result).toEqual([{string: '@ancing in the @ark', shouldHighlight: false}]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('Should handle full string.', () => { |
| 81 | + const result = getTextPartsByHighlight('Dancing in the Dark', 'Dancing in the Dark'); |
| 82 | + expect(result).toEqual([{string: 'Dancing in the Dark', shouldHighlight: true}]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Should handle longer string.', () => { |
| 86 | + const result = getTextPartsByHighlight('Dancing in the Dark', 'Dancing in the Darker'); |
| 87 | + expect(result).toEqual([{string: 'Dancing in the Dark', shouldHighlight: false}]); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('getArrayPartsByHighlight', () => { |
| 92 | + it('should return the whole string as a single part when highlight array is empty', () => { |
| 93 | + const result = getArrayPartsByHighlight('Playground Screen', []); |
| 94 | + expect(result).toEqual([{string: 'Playground Screen', shouldHighlight: false}]); |
| 95 | + }); |
| 96 | + it('should return the whole string as a single part when highlight string is empty', () => { |
| 97 | + const result = getArrayPartsByHighlight('Playground Screen', ['']); |
| 98 | + expect(result).toEqual([{string: 'Playground Screen', shouldHighlight: false}]); |
| 99 | + }); |
| 100 | + it('should return the whole string as a single part when highlight string dont match', () => { |
| 101 | + const result = getArrayPartsByHighlight('Playground Screen', ['aaa']); |
| 102 | + expect(result).toEqual([{string: 'Playground Screen', shouldHighlight: false}]); |
| 103 | + }); |
| 104 | + it('should break text to parts according to highlight string', () => { |
| 105 | + const result = getArrayPartsByHighlight('Playground Screen', ['Scr']); |
| 106 | + expect(result).toEqual([ |
| 107 | + {string: 'Playground ', shouldHighlight: false}, |
| 108 | + {string: 'Scr', shouldHighlight: true}, |
| 109 | + {string: 'een', shouldHighlight: false} |
| 110 | + ]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('highlight repeats more than once should color the first match', () => { |
| 114 | + const result = getArrayPartsByHighlight('Dancing in the Dark', ['Da']); |
| 115 | + expect(result).toEqual([ |
| 116 | + {string: 'Da', shouldHighlight: true}, |
| 117 | + {string: 'ncing in the Dark', shouldHighlight: false} |
| 118 | + ]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should be case-insensitive', () => { |
| 122 | + const result = getArrayPartsByHighlight('Dancing in the Dark', ['da']); |
| 123 | + expect(result).toEqual([ |
| 124 | + {string: 'Da', shouldHighlight: true}, |
| 125 | + {string: 'ncing in the Dark', shouldHighlight: false} |
| 126 | + ]); |
| 127 | + }); |
| 128 | + |
| 129 | + it('Should handle special characters @', () => { |
| 130 | + const result = getArrayPartsByHighlight('@ancing in the @ark', ['@a']); |
| 131 | + expect(result).toEqual([ |
| 132 | + {string: '@a', shouldHighlight: true}, |
| 133 | + {string: 'ncing in the @ark', shouldHighlight: false} |
| 134 | + ]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('Should handle special characters !', () => { |
| 138 | + const result = getArrayPartsByHighlight('!ancing in the !ark', ['!a']); |
| 139 | + expect(result).toEqual([ |
| 140 | + {string: '!a', shouldHighlight: true}, |
| 141 | + {string: 'ncing in the !ark', shouldHighlight: false} |
| 142 | + ]); |
| 143 | + }); |
| 144 | + |
| 145 | + it('Should handle special characters starts with @', () => { |
| 146 | + const result = getArrayPartsByHighlight('[email protected]', ['@wix']); |
| 147 | + expect(result).toEqual([ |
| 148 | + {string: 'uilib', shouldHighlight: false}, |
| 149 | + {string: '@wix', shouldHighlight: true}, |
| 150 | + {string: '.com', shouldHighlight: false} |
| 151 | + ]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('Should handle full string.', () => { |
| 155 | + const result = getArrayPartsByHighlight('Dancing in the Dark', ['Dancing in the Dark']); |
| 156 | + expect(result).toEqual([{string: 'Dancing in the Dark', shouldHighlight: true}]); |
| 157 | + }); |
| 158 | + |
| 159 | + it('Should handle longer string.', () => { |
| 160 | + const result = getArrayPartsByHighlight('Dancing in the Dark', ['Dancing in the Darker']); |
| 161 | + expect(result).toEqual([{string: 'Dancing in the Dark', shouldHighlight: false}]); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments