UNPKG

5.11 kBJavaScriptView Raw
1const { splitMatches, highlightMatches, highlightChars } = require('.')
2const { match } = require('fuzzaldrin-plus')
3const React = require('react')
4
5describe('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
44describe('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 // Array [
82 // "The quick ",
83 // <mark>
84 // brown
85 // </mark>,
86 // " fox jumps over the lazy dog",
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 // Array [
99 // <span>
100 // The quick
101 // </span>,
102 // <mark>
103 // brown
104 // </mark>,
105 // <span>
106 // fox jumps over the lazy dog
107 // </span>,
108 // ]
109 })
110})
111
112describe('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 // Array [
147 // "The quick ",
148 // <mark>
149 // brown
150 // </mark>,
151 // " fox jumps over the lazy dog",
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 // Array [
164 // <span>
165 // The quick
166 // </span>,
167 // <mark>
168 // brown
169 // </mark>,
170 // <span>
171 // fox jumps over the lazy dog
172 // </span>,
173 // ]
174 })
175})