1 | const { splitMatches, highlightMatches, highlightChars } = require('.')
|
2 | const { match } = require('fuzzaldrin-plus')
|
3 | const React = require('react')
|
4 |
|
5 | describe('splitMatches', () => {
|
6 | it('simple case', () => {
|
7 | const text = 'The quick brown fox jumps over the lazy dog'
|
8 | const matches = match(text, 'brown')
|
9 | const result = splitMatches(text, matches)
|
10 |
|
11 | expect(result).toEqual([
|
12 | { isMatch: false, str: 'The quick ' },
|
13 | { isMatch: true, str: 'brown' },
|
14 | { isMatch: false, str: ' fox jumps over the lazy dog' }
|
15 | ])
|
16 | })
|
17 |
|
18 | it('fuzzy match', () => {
|
19 | const text = 'The quick brown fox jumps over the lazy dog'
|
20 | const matches = match(text, 'thejumpslazy')
|
21 | const result = splitMatches(text, matches)
|
22 |
|
23 | expect(result).toEqual([
|
24 | { isMatch: true, str: 'The' },
|
25 | { isMatch: false, str: ' quick brown fox ' },
|
26 | { isMatch: true, str: 'jumps' },
|
27 | { isMatch: false, str: ' over the ' },
|
28 | { isMatch: true, str: 'lazy' },
|
29 | { isMatch: false, str: ' dog' }
|
30 | ])
|
31 | })
|
32 |
|
33 | it('no match', () => {
|
34 | const text = 'The quick brown fox jumps over the lazy dog'
|
35 | const matches = match(text, '***')
|
36 | const result = splitMatches(text, matches)
|
37 |
|
38 | expect(result).toEqual([
|
39 | { isMatch: false, str: 'The quick brown fox jumps over the lazy dog' }
|
40 | ])
|
41 | })
|
42 | })
|
43 |
|
44 | describe('highlightMatches', () => {
|
45 | it('no match', () => {
|
46 | const text = 'The quick brown fox jumps over the lazy dog'
|
47 | const result = highlightMatches(text, [], s => `[${s}]`)
|
48 |
|
49 | expect(result.join('')).toBe(text)
|
50 | })
|
51 |
|
52 | it('match using matchesWrapper that return string', () => {
|
53 | const text = 'The quick brown fox jumps over the lazy dog'
|
54 | const matches = match(text, 'brown')
|
55 | const result = highlightMatches(text, matches, s => `[${s}]`)
|
56 |
|
57 | expect(result.join('')).toBe(
|
58 | 'The quick [brown] fox jumps over the lazy dog'
|
59 | )
|
60 | })
|
61 |
|
62 | it('match using matchesWrapper and noMatchesWrapper that return strings', () => {
|
63 | const text = 'The quick brown fox jumps over the lazy dog'
|
64 | const matches = match(text, 'brown')
|
65 | const result = highlightMatches(text, matches, s => `[${s}]`, s => `{${s}}`)
|
66 |
|
67 | expect(result.join('')).toBe(
|
68 | '{The quick }[brown]{ fox jumps over the lazy dog}'
|
69 | )
|
70 | })
|
71 |
|
72 | it('match using matchesWrapper that return React elements', () => {
|
73 | const text = 'The quick brown fox jumps over the lazy dog'
|
74 | const matches = match(text, 'brown')
|
75 | const result = highlightMatches(text, matches, (s, i) =>
|
76 | React.createElement('mark', { key: i }, s)
|
77 | )
|
78 |
|
79 | expect(result).toMatchSnapshot()
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | const result2 = highlightMatches(
|
90 | text,
|
91 | matches,
|
92 | (s, i) => React.createElement('mark', { key: i }, s),
|
93 | (s, i) => React.createElement('span', { key: i }, s)
|
94 | )
|
95 |
|
96 | expect(result2).toMatchSnapshot()
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | })
|
110 | })
|
111 |
|
112 | describe('highlightChars', () => {
|
113 | it('no match', () => {
|
114 | const text = 'The quick brown fox jumps over the lazy dog'
|
115 | const result = highlightChars(text, '***', s => `[${s}]`)
|
116 |
|
117 | expect(result.join('')).toBe(text)
|
118 | })
|
119 |
|
120 | it('match using matchesWrapper that return string', () => {
|
121 | const text = 'The quick brown fox jumps over the lazy dog'
|
122 | const result = highlightChars(text, 'brown', s => `[${s}]`)
|
123 |
|
124 | expect(result.join('')).toBe(
|
125 | 'The quick [brown] fox jumps over the lazy dog'
|
126 | )
|
127 | })
|
128 |
|
129 | it('match using matchesWrapper and noMatchesWrapper that return strings', () => {
|
130 | const text = 'The quick brown fox jumps over the lazy dog'
|
131 | const result = highlightChars(text, 'brown', s => `[${s}]`, s => `{${s}}`)
|
132 |
|
133 | expect(result.join('')).toBe(
|
134 | '{The quick }[brown]{ fox jumps over the lazy dog}'
|
135 | )
|
136 | })
|
137 |
|
138 | it('match using matchesWrapper that return React elements', () => {
|
139 | const text = 'The quick brown fox jumps over the lazy dog'
|
140 | const result = highlightChars(text, 'brown', (s, i) =>
|
141 | React.createElement('mark', { key: i }, s)
|
142 | )
|
143 |
|
144 | expect(result).toMatchSnapshot()
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | const result2 = highlightChars(
|
155 | text,
|
156 | 'brown',
|
157 | (s, i) => React.createElement('mark', { key: i }, s),
|
158 | (s, i) => React.createElement('span', { key: i }, s)
|
159 | )
|
160 |
|
161 | expect(result2).toMatchSnapshot()
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | })
|
175 | })
|