You can't directly combine getByTestId and getByLabel to find an element that matches both at the same time using React Testing Library's (RTL) API, as they are both used to retrieve elements based on different criteria and are not designed to be chained together in the way you might be thinking.
However, you can achieve a similar outcome by using one of the query methods and then asserting that the retrieved element also matches the other condition. Here are a couple of ways to do it:
1. Using getByTestId and then asserting it has the correct label
You can first retrieve an element by its data-testid and then verify if it matches the expected label or contains the expected text.
import { render, screen } from '@testing-library/react';
test('element exists with testid and label', () => {
render(<YourComponent />);
const element = screen.getByTestId('your-test-id');
expect(element).toBeInTheDocument(); // Basic existence check, not strictly necessary but good practice
expect(screen.getByText('Label Text')).toBeInTheDocument(); // Checks if label text exists somewhere
// Or, if you're checking the label directly associated with the element (e.g., for input)
expect(element).toHaveAttribute('aria-label', 'Label Text'); // For elements with aria-label
// Or, for an input associated with a label
const label = screen.getByText('Label Text');
expect(label).toBeInTheDocument();
// If your input is associated with the label via 'for' and 'id', you can check the association
expect(label).toHaveAttribute('for', element.id);
});
2. Using getByLabelText and then checking its data-testid
If the element you're looking for is an input or form element associated with a label, getByLabelText is a very accessible way to retrieve it.
import { render, screen } from '@testing-library/react';
test('element exists with label and testid', () => {
render(<YourComponent />);
const element = screen.getByLabelText('Label Text');
expect(element).toBeInTheDocument();
expect(element).toHaveAttribute('data-testid', 'your-test-id');
});
3. Using a more general query and filtering
If the above methods are not suitable, you can use a more general query like within or directly querying the container to filter elements.
import { render, screen, within } from '@testing-library/react';
test('element exists', () => {
render(<YourComponent />);
const container = screen.getByTestId('container-test-id'); // Assuming a container around your element
const element = within(container).getByTestId('your-test-id');
expect(element).toBeInTheDocument();
// Further assertions as needed
});
Choosing the Right Approach
- Use
getByLabelText when you're working with form elements that have associated labels. It's a very accessible and straightforward method.
- Use
getByTestId when you don't have a clear label or when the element isn't associated with a label. You can then assert additional properties or associations.
By using these methods, you can effectively test that an element matches both the data-testid and is associated with a specific label or text, even if RTL doesn't directly support chaining these queries together.