queryAllByTestId<E extends Element> method
- dynamic testId, {
- bool exact = true,
- NormalizerFn normalizer([])?,
Returns a list of elements with the given testId
value for the data-test-id
attribute,
defaulting to an exact
match.
ByTestId Caveats
Guiding Principles / Priority
In the spirit of the guiding principles,
it is recommended to use this only when a more accessible query is not an option.
Using data-test-id
attributes do not resemble how your software is used and should be avoided if possible.
That said, they are way better than querying based on DOM structure or styling css class names. Learn more
about data-test-id
s from the blog post "Making your UI tests resilient to change".
Enabling Test Mode
When using a *ByTestId
query on an OverReact component, you must call enableTestMode()
within main()
of your test(s).
Returns an empty list if no element(s) are found.
Use getAllByTestId
if a RTE is expected.
Related:
queryByTestId
Example
The example below demonstrates the usage of the
getByTestId
query. However, the example is also relevant forgetAllByTestId
,queryByTestId
,queryAllByTestId
,findByTestId
andfindAllByTestId
.Read more about the different types of queries to gain more clarity on which one suits your use-cases best.
<div data-test-id="custom-element" />
import 'package:react/react.dart' as react;
import 'package:react_testing_library/matchers.dart' show isInTheDocument;
import 'package:react_testing_library/react_testing_library.dart' as rtl;
import 'package:test/test.dart';
main() {
test('', () {
// Render the DOM shown in the example snippet above
final view = rtl.render(
react.div({'data-test-id': 'custom-element'}),
);
expect(view.getByTestId('custom-element'), isInTheDocument);
});
}
NOTE:
render()
supports React vDom elements / custom components created using either the
react or over_react packages.
The examples shown here use the
react
package since thereact_testing_library
does not have a direct dependency onover_react
- but both libraries are fully supported.
Options
testId
This argument can be set to a String
, RegExp
, or a Function
which returns true
for a match and false
for a mismatch.
See the JS TextMatch
docs for more details
and examples.
exact
Defaults to true
; matches full strings, case-sensitive. When false
, matches substrings and is
not case-sensitive. It has no effect on regex or function arguments. In most cases using a regex
instead of a string gives you more control over fuzzy matching and should be preferred over exact: false
.
normalizer
An optional function which overrides normalization behavior.
Before running any matching logic against text in the DOM, DOM Testing Library automatically normalizes that text. By default, normalization consists of trimming whitespace from the start and end of text, and collapsing multiple adjacent whitespace characters into a single space.
If you want to prevent that normalization, or provide alternative normalization
(e.g. to remove Unicode control characters), you can provide a normalizer
function.
This function will be given a string and is expected to return a normalized version of that string.
NOTE: Specifying a value for
normalizer
replaces the built-in normalization, but you can call getDefaultNormalizer to obtain a built-in normalizer, either to adjust that normalization or to call it from your own normalizer.
See the JS TextMatch
precision and
JS TextMatch
normalization docs
for more details and examples.
Implementation
List<E> queryAllByTestId<E extends Element>(
/*TextMatch*/ dynamic testId, {
bool exact = true,
NormalizerFn Function([NormalizerOptions?])? normalizer,
}) {
// For strings, we need to jump through a few more hoops to ensure that we support matching a single
// test id value on an element that may have more than one.
List<E> jsQuery(dynamic testIdTextMatchValue) {
return withErrorInterop(
() => _jsQueryAllByTestId(
getContainerForScope(),
testIdTextMatchValue,
buildMatcherOptions(exact: exact, normalizer: normalizer),
).cast<E>(), // <vomit/> https://github.com/dart-lang/sdk/issues/37676
);
}
if (testId is! String) return jsQuery(TextMatch.toJs(testId));
// For strings, we need to jump through a few more hoops to ensure that we support matching a single
// test id value on an element that may have more than one.
try {
return jsQuery(_convertTestIdStringToRegExp(testId, exact: exact));
} catch (e, st) {
if (e is TestingLibraryElementError) {
// Replace regex produced by `_convertTestIdStringToRegExp` in the error message with the string provided
// by the consumer since that is how they authored it.
throw _errorWithMessageUsingStringTestIdProvided(
errorThrownUsingRegExTestIdValue: e, stackTrace: st, testId: testId);
} else {
rethrow;
}
}
}