diff --git a/src/__tests__/suggestions.js b/src/__tests__/suggestions.js index fc871ce8..e291063d 100644 --- a/src/__tests__/suggestions.js +++ b/src/__tests__/suggestions.js @@ -172,13 +172,54 @@ test('should suggest img role w/ alt text', () => { }) test('escapes regular expressions in suggestion', () => { - renderIntoDocument( - `The Problem (picture of a question mark)`, - ) + const {container} = renderIntoDocument(` + + +

+ Loading ... (1) +

+ The Problem (picture of a question mark) + `) expect(() => screen.getByTestId('foo')).toThrowError( /getByRole\('img', \{ name: \/the problem \\\(picture of a question mark\\\)\/i \}\)/, ) + + expect( + getSuggestedQuery( + container.querySelector('img'), + 'get', + 'altText', + ).toString(), + ).toEqual(`getByAltText(/the problem \\(picture of a question mark\\)/i)`) + + expect(getSuggestedQuery(container.querySelector('p')).toString()).toEqual( + `getByText(/loading \\.\\.\\. \\(1\\)/i)`, + ) + + expect( + getSuggestedQuery( + container.querySelector('input'), + 'get', + 'placeholderText', + ).toString(), + ).toEqual(`getByPlaceholderText(/should escape \\+\\-'\\(\\//i)`) + + expect( + getSuggestedQuery( + container.querySelector('input'), + 'get', + 'displayValue', + ).toString(), + ).toEqual(`getByDisplayValue(/my super string \\+\\-\\('\\{\\}\\^\\$\\)/i)`) + + expect( + getSuggestedQuery( + container.querySelector('input'), + 'get', + 'labelText', + ).toString(), + ).toEqual(`getByLabelText(/inp\\-t lab\\^l w\\{th c\\+ars to esc\\\\pe/i)`) }) test('should suggest getByLabelText when no role available', () => { diff --git a/src/suggestions.js b/src/suggestions.js index 730cb9e0..5ab47b11 100644 --- a/src/suggestions.js +++ b/src/suggestions.js @@ -35,7 +35,7 @@ function escapeRegExp(string) { } function getRegExpMatcher(string) { - return new RegExp(string.toLowerCase(), 'i') + return new RegExp(escapeRegExp(string.toLowerCase()), 'i') } function makeSuggestion(queryName, content, {variant, name}) { @@ -46,7 +46,7 @@ function makeSuggestion(queryName, content, {variant, name}) { ] if (name) { - queryArgs.push({name: new RegExp(escapeRegExp(name.toLowerCase()), 'i')}) + queryArgs.push({name: getRegExpMatcher(name)}) } const queryMethod = `${variant}By${queryName}`