Skip to content

Commit aaceea2

Browse files
authored
fix: escape regexps that are used as query suggestion arguments (#694)
1 parent d8c3942 commit aaceea2

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/__tests__/suggestions.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,54 @@ test('should suggest img role w/ alt text', () => {
172172
})
173173

174174
test('escapes regular expressions in suggestion', () => {
175-
renderIntoDocument(
176-
`<img src="foo.png" alt="The Problem (picture of a question mark)" data-testid="foo" />`,
177-
)
175+
const {container} = renderIntoDocument(`
176+
<label for="superInput">inp-t lab^l w{th c+ars to esc\\pe</label>
177+
<input id="superInput" type="text" value="my super string +-('{}^$)" placeholder="should escape +-'(/" />
178+
<p>
179+
Loading ... (1)
180+
</p>
181+
<img src="foo.png" alt="The Problem (picture of a question mark)" data-testid="foo" />
182+
`)
178183

179184
expect(() => screen.getByTestId('foo')).toThrowError(
180185
/getByRole\('img', \{ name: \/the problem \\\(picture of a question mark\\\)\/i \}\)/,
181186
)
187+
188+
expect(
189+
getSuggestedQuery(
190+
container.querySelector('img'),
191+
'get',
192+
'altText',
193+
).toString(),
194+
).toEqual(`getByAltText(/the problem \\(picture of a question mark\\)/i)`)
195+
196+
expect(getSuggestedQuery(container.querySelector('p')).toString()).toEqual(
197+
`getByText(/loading \\.\\.\\. \\(1\\)/i)`,
198+
)
199+
200+
expect(
201+
getSuggestedQuery(
202+
container.querySelector('input'),
203+
'get',
204+
'placeholderText',
205+
).toString(),
206+
).toEqual(`getByPlaceholderText(/should escape \\+\\-'\\(\\//i)`)
207+
208+
expect(
209+
getSuggestedQuery(
210+
container.querySelector('input'),
211+
'get',
212+
'displayValue',
213+
).toString(),
214+
).toEqual(`getByDisplayValue(/my super string \\+\\-\\('\\{\\}\\^\\$\\)/i)`)
215+
216+
expect(
217+
getSuggestedQuery(
218+
container.querySelector('input'),
219+
'get',
220+
'labelText',
221+
).toString(),
222+
).toEqual(`getByLabelText(/inp\\-t lab\\^l w\\{th c\\+ars to esc\\\\pe/i)`)
182223
})
183224

184225
test('should suggest getByLabelText when no role available', () => {

src/suggestions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function escapeRegExp(string) {
3535
}
3636

3737
function getRegExpMatcher(string) {
38-
return new RegExp(string.toLowerCase(), 'i')
38+
return new RegExp(escapeRegExp(string.toLowerCase()), 'i')
3939
}
4040

4141
function makeSuggestion(queryName, content, {variant, name}) {
@@ -46,7 +46,7 @@ function makeSuggestion(queryName, content, {variant, name}) {
4646
]
4747

4848
if (name) {
49-
queryArgs.push({name: new RegExp(escapeRegExp(name.toLowerCase()), 'i')})
49+
queryArgs.push({name: getRegExpMatcher(name)})
5050
}
5151

5252
const queryMethod = `${variant}By${queryName}`

0 commit comments

Comments
 (0)