From eba7e82ba21d647f29c792ceaa9b89be6e145d55 Mon Sep 17 00:00:00 2001 From: Jacob Paris Date: Sun, 28 Jun 2020 05:25:28 -0400 Subject: [PATCH 1/5] Adds matcher name to multiple elements error --- src/queries/role.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/queries/role.js b/src/queries/role.js index 07fe0771..b62ee28e 100644 --- a/src/queries/role.js +++ b/src/queries/role.js @@ -107,8 +107,8 @@ function queryAllByRole( }) } -const getMultipleError = (c, role) => - `Found multiple elements with the role "${role}"` +const getMultipleError = (c, role, {name} = {}) => + `Found multiple elements with the role "${role}" matching ${name}` const getMissingError = ( container, From 600f9b5aea193339645f10204a4a59752be8e41e Mon Sep 17 00:00:00 2001 From: Jacob Paris Date: Sun, 28 Jun 2020 05:43:54 -0400 Subject: [PATCH 2/5] Adds quotes around strings if needed --- src/queries/role.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/queries/role.js b/src/queries/role.js index b62ee28e..1d9d3211 100644 --- a/src/queries/role.js +++ b/src/queries/role.js @@ -107,8 +107,18 @@ function queryAllByRole( }) } -const getMultipleError = (c, role, {name} = {}) => - `Found multiple elements with the role "${role}" matching ${name}` +const getMultipleError = (c, role, {name} = {}) => { + let nameHint = '' + if (name === undefined) { + nameHint = '' + } else if (typeof name === 'string') { + nameHint = ` and name "${name}"` + } else { + nameHint = ` and name \`${name}\`` + } + + return `Found multiple elements with the role "${role}"${nameHint}` +} const getMissingError = ( container, From a1822d494846415c9806c7896b10a04ffae82eaf Mon Sep 17 00:00:00 2001 From: Jacob Paris Date: Sun, 28 Jun 2020 09:23:06 -0400 Subject: [PATCH 3/5] Tests accessible name in error for multiple found --- src/__tests__/role.js | 47 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/__tests__/role.js b/src/__tests__/role.js index a187a77b..2e172084 100644 --- a/src/__tests__/role.js +++ b/src/__tests__/role.js @@ -346,7 +346,9 @@ Here are the accessible roles: test('has no useful error message in findBy', async () => { const {findByRole} = render(`
  • `) - await expect(findByRole('option', {timeout: 1})).rejects.toThrow('Unable to find role="option"') + await expect(findByRole('option', {timeout: 1})).rejects.toThrow( + 'Unable to find role="option"', + ) }) test('explicit role is most specific', () => { @@ -378,6 +380,49 @@ Here are the accessible roles: `) }) +test('accessible name in error message for multiple found', () => { + const {getByRole} = render(` +
    + + + +
    + `) + + expect(() => getByRole('button', {name: /value/i})) + .toThrowErrorMatchingInlineSnapshot(` +"Found multiple elements with the role "button" and name \`/value/i\` + +(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)). + +
    + + +
    + + + + + + + + + + + +
    + + +
    " +`) +}) + describe('configuration', () => { let originalConfig beforeEach(() => { From eaa264d04b2ae53247f733a16f682d0e97d47a95 Mon Sep 17 00:00:00 2001 From: Jacob Paris Date: Sun, 28 Jun 2020 09:30:06 -0400 Subject: [PATCH 4/5] Fixes whitespace in test --- src/__tests__/role.js | 45 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/__tests__/role.js b/src/__tests__/role.js index 2e172084..dc99a2ac 100644 --- a/src/__tests__/role.js +++ b/src/__tests__/role.js @@ -381,13 +381,12 @@ Here are the accessible roles: }) test('accessible name in error message for multiple found', () => { - const {getByRole} = render(` -
    - - - -
    - `) + const {getByRole} = render( + ``, + ) expect(() => getByRole('button', {name: /value/i})) .toThrowErrorMatchingInlineSnapshot(` @@ -396,29 +395,15 @@ test('accessible name in error message for multiple found', () => { (If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
    - - -
    - - - - - - - - - - - -
    - - + + +
    " `) }) From 1febc64da0c163febaf591510515b05ebf36e914 Mon Sep 17 00:00:00 2001 From: Jacob Paris Date: Sun, 28 Jun 2020 09:46:19 -0400 Subject: [PATCH 5/5] Adds test for string case --- src/__tests__/role.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/__tests__/role.js b/src/__tests__/role.js index dc99a2ac..29eec2a2 100644 --- a/src/__tests__/role.js +++ b/src/__tests__/role.js @@ -380,7 +380,7 @@ Here are the accessible roles: `) }) -test('accessible name in error message for multiple found', () => { +test('accessible regex name in error message for multiple found', () => { const {getByRole} = render( ` { `) }) +test('accessible string name in error message for multiple found', () => { + const {getByRole} = render( + ``, + ) + + expect(() => getByRole('button', {name: 'Submit'})) + .toThrowErrorMatchingInlineSnapshot(` +"Found multiple elements with the role "button" and name "Submit" + +(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)). + +
    + + + +
    " +`) +}) + describe('configuration', () => { let originalConfig beforeEach(() => {