Skip to content

within for mutliple elements #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
benmonro opened this issue Dec 20, 2019 · 9 comments
Closed

within for mutliple elements #420

benmonro opened this issue Dec 20, 2019 · 9 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@benmonro
Copy link
Member

Describe the feature you'd like:

I'm aware of the callback function that allows you to accomplish this, but we've seen this pattern emerge enough times that it made me wonder if the ability to combine queries would be possible, and maybe make the tests a little more readable:

i.e. if i want to combine getByText & getByRole i know I can do this:

getByText((text, element => text === "Have an issue?" && element.getAttribute('role') === 'button'))

it would be nice to do something like this:

within(getAllByRole("button")).getByText("Have an issue?")

Suggested implementation:

modify within to accept a NodeList / array perhaps?

Describe alternatives you've considered:

none that i can think of

Teachability, Documentation, Adoption, Migration Strategy:

@benmonro
Copy link
Member Author

🎉 I submitted issue #420 What a stony milestone!

@alexkrolick
Copy link
Collaborator

Similar to this #408 (comment)

@benmonro
Copy link
Member Author

ah yeah i was actually initially going to post that here, but as I was talking with a colleague, it occurred to me it might be a bit cleaner to just use the existing testing-lib apis ala within . I think it would actually be pretty easy to implement...

@kentcdodds any thoughts on this?

@kentcdodds
Copy link
Member

Hi 👋

Hmm... I think that's ok. I don't know how many people would use that feature, but I don't see harm in adding it. Should be pretty simple as you said. Feel free to open a pull request for it.

@kentcdodds kentcdodds added enhancement New feature or request help wanted Extra attention is needed labels Jan 21, 2020
@eps1lon
Copy link
Member

eps1lon commented Mar 21, 2020

within(getAllByRole("button")).getByText("Have an issue?")

Now that we have getAllByRole('button', { name: 'Have an issue?' }) why would we need another API? It seems like these would do the same thing.

@kentcdodds
Copy link
Member

I agree with @eps1lon here. This use case is well covered.

@Jiert
Copy link

Jiert commented Mar 24, 2020

@eps1lon and @kentcdodds are there examples you can point me towards where the name option is being used?

I'm trying the following:

test('Buttons',  () => {
  const {getAllByRole} = render(
    <div>
      <button>foo</button>
      <button>bar</button>
    </div>
  );

  const buttons = getAllByRole('button', {name: 'foo'});
})

In this case, buttons is an array of both buttons, NOT just the first button as I'd expect. Am I doing something incorrect here?

I've poured over the docs and while I do see where this concept is explained, I haven't found an example that shows it working.

@alexkrolick
Copy link
Collaborator

@Jiert are you on the latest version of the library? The name option would be ignored on older versions of getAllByRole, returning all the buttons.

@Jiert
Copy link

Jiert commented Mar 25, 2020

@alexkrolick well don't I feel embarrassed. That was it, thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants