Skip to content

feat: *NthBy* #1056

@ghengeveld

Description

@ghengeveld

Describe the feature you'd like:

I would like to add a new set of APIs to get/find/query for the nth result. So that would give us getNthByText, findNthByRole etc. For example:

await screen.findNthByRole(0, 'button')

This would be similar to doing findAllBy(...)[n], but without the awkward (await findAllByRole('button'))[0] syntax (extra parenthesis needed for the await).

The API is similar to toHaveBeenNthCalledWith in Jest.

Additionally, I am working on the Interactions addon for Storybook which provides a GUI for Testing Library:

Screenshot 2021-10-14 at 15 56 58

However, when you do things like getAllByRole('button')[0], it's no longer able to track the nested call reference, and it falls back to showing the underlying value (the 0th item in the array):

Screenshot 2021-10-14 at 15 58 41

This can only be avoided by having a dedicated API call, for which the result can then be directly fed into the userEvent method.

I'm totally willing to implement this, no problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions