You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Applying suggestion for table cell element query yields no results #334
Hi there
I have a simple table structure where I wanted to select its first cell. While applying a better selector suggested by the application, it no longer selects the element.
Actually this doesn't work because it's using a regex to find the value..
As there are two lines containing the text foo you would need to use single quotes or use getAll*
We could fix that I guess? We currently use regex matches, and the problem is that the regex results in more than one match. We could update the regex in those cases, and see if it would match a single element if it'd include the start and end modifiers, like /^foo$/i.
Alternatively, we could fallback to string matchers, or even default to those. This is a good argument to use string matchers instead of regex.
@smeijer I think it would be interesting, although the way it works with regex is nice, maybe if we could add a clearer message that more than one match was found, the developer wouldn't be confused.
import{render,screen}from"@testing-library/react";importuserfrom"@testing-library/user-event";importAppfrom"./App";test("can receive a new user and show it on a list",()=>{render(<App/>);constnameInput=screen.getByRole("textbox",{name: /name/i});constemailInput=screen.getByRole("textbox",{name: /email/i});constbutton=screen.getByRole("button");user.click(nameInput);user.keyboard("jane");user.click(emailInput);user.keyboard("jane@gmail.com");user.click(button);constname=screen.getByRole("cell",{name: "jane"});constemail=screen.getByRole("cell",{name: "jane@gmail.com"});expect(name).toBeInTheDocument();expect(email).toBeInTheDocument();});
And this test does not pass., but instead gives me an error saying: TestingLibraryElementError: Unable to find accessible element with the role "cell" and name "jane"
Nevermind, this solved it for me:
import{render,screen,waitFor}from"@testing-library/react";importuserfrom"@testing-library/user-event";importAppfrom"./App";test("can receive a new user and show it on a list",async()=>{render(<App/>);constnameInput=screen.getByRole("textbox",{name: /name/i});constemailInput=screen.getByRole("textbox",{name: /email/i});constbutton=screen.getByRole("button");awaituser.click(nameInput);awaituser.keyboard("jane");awaituser.click(emailInput);awaituser.keyboard("jane@gmail.com");awaituser.click(button);constname=screen.getByRole("cell",{name: "jane"});constemail=screen.getByRole("cell",{name: "jane@gmail.com"});expect(name).toBeInTheDocument();expect(email).toBeInTheDocument();});
Activity
brenocota-hotmart commentedon Sep 20, 2022
Hi @ulisses-alves how are you? :)
Actually this doesn't work because it's using a regex to find the value..
As there are two lines containing the text foo you would need to use single quotes or use getAll*
OR
If you update you HTML
smeijer commentedon Sep 20, 2022
We could fix that I guess? We currently use regex matches, and the problem is that the regex results in more than one match. We could update the regex in those cases, and see if it would match a single element if it'd include the start and end modifiers, like
/^foo$/i.Alternatively, we could fallback to string matchers, or even default to those. This is a good argument to use string matchers instead of regex.
What do you think?
brenogcota commentedon Sep 20, 2022
@smeijer I think it would be interesting, although the way it works with regex is nice, maybe if we could add a clearer message that more than one match was found, the developer wouldn't be confused.
ldco2016 commentedon Jul 26, 2023
Well, I developed a test like this:
And this test does not pass., but instead gives me an error saying:
TestingLibraryElementError: Unable to find accessible element with the role "cell" and name "jane"Nevermind, this solved it for me:
brenogcota commentedon Jul 27, 2023
@ldco2016 could you show us your App component?