|
| 1 | +import React from 'react' |
| 2 | +import flexibleStringReplace from '../source' |
| 3 | + |
| 4 | +test('flexibleStringReplace is defined', () => { |
| 5 | + expect(flexibleStringReplace).toBeDefined() |
| 6 | +}) |
| 7 | + |
| 8 | +test('string pattern & string replacer for first match', () => { |
| 9 | + const str = 'The rain in Spain falls mainly on the plain. Spain is nice.' |
| 10 | + const pattern = 'Spain' |
| 11 | + const replacer = 'foo bar baz' |
| 12 | + const res = flexibleStringReplace(pattern, replacer, str) |
| 13 | + |
| 14 | + expect(res).toEqual([ |
| 15 | + 'The rain in ', |
| 16 | + 'foo bar baz', |
| 17 | + ' falls mainly on the plain. Spain is nice.' |
| 18 | + ]) |
| 19 | +}) |
| 20 | + |
| 21 | +test('string pattern & html interpolation for first match', () => { |
| 22 | + const str = 'The rain in Spain falls mainly on the plain. Spain is nice.' |
| 23 | + const pattern = 'Spain' |
| 24 | + const replacer = (match: string): string => `<mark>${match}</mark>` |
| 25 | + const res = flexibleStringReplace(pattern, replacer, str) |
| 26 | + |
| 27 | + expect(res).toEqual([ |
| 28 | + 'The rain in ', |
| 29 | + '<mark>Spain</mark>', |
| 30 | + ' falls mainly on the plain. Spain is nice.' |
| 31 | + ]) |
| 32 | +}) |
| 33 | + |
| 34 | +test('regexp pattern & html string interpolation function with matches', () => { |
| 35 | + const str = 'The rain in Spain falls mainly on the plain. Spain is nice.' |
| 36 | + const pattern = new RegExp('Spain', 'igm') |
| 37 | + const replacer = (match: string): string => `<mark>${match}</mark>` |
| 38 | + const res = flexibleStringReplace(pattern, replacer, str) |
| 39 | + |
| 40 | + expect(res).toEqual([ |
| 41 | + 'The rain in ', |
| 42 | + '<mark>Spain</mark>', |
| 43 | + ' falls mainly on the plain. ', |
| 44 | + '<mark>Spain</mark>', |
| 45 | + ' is nice.' |
| 46 | + ]) |
| 47 | +}) |
| 48 | + |
| 49 | +test('forwards matching group parts in replacer params', () => { |
| 50 | + const str = 'Dear @diary, I hope this test #passes #pray' |
| 51 | + const pattern = /\s(@\w+)|\s(#\w+)/gm |
| 52 | + const ats: string[] = [] |
| 53 | + const tags: string[] = [] |
| 54 | + const replacer = (match: string, at: string, tag: string): void => { |
| 55 | + if (at) { |
| 56 | + ats.push(at) |
| 57 | + } |
| 58 | + if (tag) { |
| 59 | + tags.push(tag) |
| 60 | + } |
| 61 | + } |
| 62 | + flexibleStringReplace(pattern, replacer, str) |
| 63 | + |
| 64 | + expect(ats).toEqual(['@diary']) |
| 65 | + expect(tags).toEqual(['#passes', '#pray']) |
| 66 | +}) |
| 67 | + |
| 68 | +test('regexp pattern & react interpolation function', () => { |
| 69 | + const str = 'The rain in Spain falls mainly on the plain. Spain is nice.' |
| 70 | + const pattern = new RegExp('Spain', 'igm') |
| 71 | + const replacer = (match: string, offset: string): JSX.Element => ( |
| 72 | + <mark key={offset}>{match}</mark> |
| 73 | + ) |
| 74 | + const res = flexibleStringReplace(pattern, replacer, str) |
| 75 | + |
| 76 | + expect(res).toEqual([ |
| 77 | + 'The rain in ', |
| 78 | + <mark key={12}>Spain</mark>, |
| 79 | + ' falls mainly on the plain. ', |
| 80 | + <mark key={45}>Spain</mark>, |
| 81 | + ' is nice.' |
| 82 | + ]) |
| 83 | +}) |
0 commit comments