From dc289036af7bd59bef8ac1165f81d33f83c726e8 Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Thu, 11 Jun 2020 10:07:42 +0200 Subject: [PATCH 1/3] feat: use regex based text matchers for suggestions --- src/__tests__/suggestions.js | 40 ++++++++++++++++++++---------------- src/suggestions.js | 17 +++++++++++---- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/__tests__/suggestions.js b/src/__tests__/suggestions.js index 581f3994..39353d1b 100644 --- a/src/__tests__/suggestions.js +++ b/src/__tests__/suggestions.js @@ -72,7 +72,9 @@ test(`should not suggest if the suggestion would give different results`, () => test('should suggest by label over title', () => { renderIntoDocument(``) - expect(() => screen.getByTitle('foo')).toThrowError(/getByLabelText\('bar'\)/) + expect(() => screen.getByTitle('foo')).toThrowError( + /getByLabelText\(\/bar\/i\)/, + ) }) test('should not suggest if there would be mixed suggestions', () => { @@ -178,7 +180,7 @@ test('should suggest getByLabelText when no role available', () => { ``, ) expect(() => screen.getByTestId('foo')).toThrowError( - /getByLabelText\('Username'\)/, + /getByLabelText\(\/username\/i\)/, ) }) @@ -191,7 +193,7 @@ test(`should suggest getByLabel on non form elements`, () => { `) expect(() => screen.getByTestId('foo')).toThrowError( - /getByLabelText\('Section One'\)/, + /getByLabelText\(\/section one\/i\)/, ) }) @@ -230,7 +232,7 @@ test(`should suggest label over placeholder text`, () => { ) expect(() => screen.getByPlaceholderText('Username')).toThrowError( - /getByLabelText\('Username'\)/, + /getByLabelText\(\/username\/i\)/, ) }) @@ -238,7 +240,7 @@ test(`should suggest getByPlaceholderText`, () => { renderIntoDocument(``) expect(() => screen.getByTestId('foo')).toThrowError( - /getByPlaceholderText\('Username'\)/, + /getByPlaceholderText\(\/username\/i\)/, ) }) @@ -246,7 +248,7 @@ test(`should suggest getByText for simple elements`, () => { renderIntoDocument(`
hello there
`) expect(() => screen.getByTestId('foo')).toThrowError( - /getByText\('hello there'\)/, + /getByText\(\/hello there\/i\)/, ) }) @@ -256,7 +258,7 @@ test(`should suggest getByDisplayValue`, () => { document.getElementById('lastName').value = 'Prine' // RIP John Prine expect(() => screen.getByTestId('lastName')).toThrowError( - /getByDisplayValue\('Prine'\)/, + /getByDisplayValue\(\/prine\/i\)/, ) }) @@ -269,10 +271,10 @@ test(`should suggest getByAltText`, () => { `) expect(() => screen.getByTestId('input')).toThrowError( - /getByAltText\('last name'\)/, + /getByAltText\(\/last name\/i\)/, ) expect(() => screen.getByTestId('area')).toThrowError( - /getByAltText\('Computer'\)/, + /getByAltText\(\/computer\/i\)/, ) }) @@ -285,27 +287,29 @@ test(`should suggest getByTitle`, () => { `) expect(() => screen.getByTestId('delete')).toThrowError( - /getByTitle\('Delete'\)/, + /getByTitle\(\/delete\/i\)/, ) expect(() => screen.getAllByTestId('delete')).toThrowError( - /getAllByTitle\('Delete'\)/, + /getAllByTitle\(\/delete\/i\)/, ) expect(() => screen.queryByTestId('delete')).toThrowError( - /queryByTitle\('Delete'\)/, + /queryByTitle\(\/delete\/i\)/, ) expect(() => screen.queryAllByTestId('delete')).toThrowError( - /queryAllByTitle\('Delete'\)/, + /queryAllByTitle\(\/delete\/i\)/, ) expect(() => screen.queryAllByTestId('delete')).toThrowError( - /queryAllByTitle\('Delete'\)/, + /queryAllByTitle\(\/delete\/i\)/, ) expect(() => screen.queryAllByTestId('delete')).toThrowError( - /queryAllByTitle\('Delete'\)/, + /queryAllByTitle\(\/delete\/i\)/, ) // Since `ByTitle` and `ByText` will both return the element // `getByText` will always be the suggested query as it is higher up the list. - expect(() => screen.getByTestId('svg')).toThrowError(/getByText\('Close'\)/) + expect(() => screen.getByTestId('svg')).toThrowError( + /getByText\(\/close\/i\)/, + ) }) test('getSuggestedQuery handles `variant` and defaults to `get`', () => { @@ -343,9 +347,9 @@ test('getSuggestedQuery returns rich data for tooling', () => { expect(getSuggestedQuery(div)).toMatchObject({ queryName: 'Text', queryMethod: 'getByText', - queryArgs: ['cancel'], + queryArgs: [/cancel/i], variant: 'get', }) - expect(getSuggestedQuery(div).toString()).toEqual(`getByText('cancel')`) + expect(getSuggestedQuery(div).toString()).toEqual(`getByText(/cancel/i)`) }) diff --git a/src/suggestions.js b/src/suggestions.js index ff960d68..a8b3f0e6 100644 --- a/src/suggestions.js +++ b/src/suggestions.js @@ -30,8 +30,12 @@ function escapeRegExp(string) { return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string } +function getRegExpMatcher(string) { + return new RegExp(string.toLowerCase(), 'i') +} + function makeSuggestion(queryName, content, {variant = 'get', name}) { - const queryArgs = [content] + const queryArgs = [queryName === 'Role' ? content : getRegExpMatcher(content)] if (name) { queryArgs.push({name: new RegExp(escapeRegExp(name.toLowerCase()), 'i')}) @@ -45,12 +49,17 @@ function makeSuggestion(queryName, content, {variant = 'get', name}) { queryArgs, variant, toString() { - const options = queryArgs[1] - ? `, { ${Object.entries(queryArgs[1]) + let [text, options] = queryArgs + + text = typeof text === 'string' ? `'${text}'` : text + + options = options + ? `, { ${Object.entries(options) .map(([k, v]) => `${k}: ${v}`) .join(', ')} }` : '' - return `${queryMethod}('${content}'${options})` + + return `${queryMethod}(${text}${options})` }, } } From 660010ea54fb2fb90a35afbc69a8365db4333cb2 Mon Sep 17 00:00:00 2001 From: Stephan Meijer <stephan.meijer@gmail.com> Date: Thu, 11 Jun 2020 10:09:12 +0200 Subject: [PATCH 2/3] chore: add .idea folder to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8e0c70cb..216094cd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ node_modules coverage dist .DS_Store +.idea # these cause more harm than good # when working with contributors From 9e903d7b62ede54491096878c104d8ec8ae23ea8 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" <me+github@kentcdodds.com> Date: Thu, 11 Jun 2020 09:24:09 -0600 Subject: [PATCH 3/3] Update .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 216094cd..8e0c70cb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ node_modules coverage dist .DS_Store -.idea # these cause more harm than good # when working with contributors