From 10023dc870367481abdefb77e0f521177512e16e Mon Sep 17 00:00:00 2001 From: marcosvega91 Date: Sun, 12 Jul 2020 19:50:48 +0200 Subject: [PATCH 1/2] test: add failing tests --- src/__tests__/suggestions.js | 47 +++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) 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', () => { From c7a6a89135ff7aea7f870a261030004d63030d5a Mon Sep 17 00:00:00 2001 From: marcosvega91 Date: Sun, 12 Jul 2020 19:53:39 +0200 Subject: [PATCH 2/2] fix: escape regex for all queries fix #693 --- src/suggestions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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}`