Skip to content

Add convenience methods to encourage use for data-testid #1500

@afontcu

Description

@afontcu

As suggested here, I'm opening up this issue to suggest the addition of methods that simplify the usage of test data attributes.

Using data-testid (or data-test, the name specifics is not the goal of the issue) might become one of the suggested ways to query components in VTU. In short, with a test-related data attribute we make the relationship between test and code under test resilient and more explicit (among other benefits).

What problem does this feature solve?

However, right now it is quite hard to type, prone to errors, and even slow to read:

wrapper.find('[data-testid="fooBar"]')

Not to mention how this would look if we needed to add some variables and backticks to the mix.

What does the proposed API look like?

I see two main options to avoid this problem, and thus make the recommended way a bit more comfortable:

  1. To provide a different method: findByTestId('fooBar').
    Is easy to grasp and self-explanatory, but splits the find() API in two. This is the approach used in the Testing Library family (source).

  2. To make data-testid the default behavior for find, so that find('fooBar') becomes find('[data-testid="fooBar"]') internally in VTU.
    This would mean that find('button') would no longer return a button element, but rather the element with data-testid="button". This might be undesirable.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions