findByDisplayValue<E extends Element> method Null safety

Future<E> findByDisplayValue<E extends Element>(
  1. dynamic value,
  2. {bool exact = true,
  3. NormalizerFn normalizer(
    1. [NormalizerOptions?]
    )?,
  4. Duration? timeout,
  5. Duration interval = defaultAsyncCallbackCheckInterval,
  6. QueryTimeoutFn? onTimeout,
  7. MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}
)
inherited

Returns a future with a single InputElement, TextAreaElement or SelectElement that has the matching value displayed, defaulting to an exact match after waiting 1000ms (or the provided timeout duration).

If there is a specific condition you want to wait for other than the DOM node being on the page, wrap a non-async query like getByDisplayValue or queryByDisplayValue in a waitFor function.

Throws if exactly one element is not found.

Prefer using ByLabelText queries for form elements when possible in order to query for elements in a way that most reflects how the user would interact with them.

Related: findAllByDisplayValue

See: testing-library.com/docs/queries/bydisplayvalue/

Example

The examples below demonstrate the usage of the getByDisplayValue query. However, the example is also relevant for getAllByDisplayValue, queryByDisplayValue, queryAllByDisplayValue, findByDisplayValue and findAllByDisplayValue.

Read more about the different types of queries to gain more clarity on which one suits your use-cases best.

<input type="text" name="lastName" value="Norris" />
<textarea name="message">Hello World</textarea>
<select name="usStates">
  <option value="">State</option>
  <option value="AL">Alabama</option>
  <option selected value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
</select>
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({},
      react.input({
        'type': 'text',
        'name': 'lastName',
        'defaultValue': 'Norris',
      }),
      react.textarea({
        'name': 'message',
        'defaultValue': 'Hello World',
      }),
      react.select({'name': 'usStates'},
        react.option({'value': ''}, 'State'),
        react.option({'value': 'AL'}, 'Alabama'),
        react.option({'value': 'AK', 'selected': true}, 'Alaska'),
        react.option({'value': 'AZ'}, 'Arizona'),
      ),
    ));

    expect(view.getByDisplayValue('Norris'), isInTheDocument);

    expect(view.getByDisplayValue('Hello World'), isInTheDocument);

    // In the case of a `<select>`, this will search for a `<select>`
    // whose selected `<option>` matches the given `TextMatch`.
    expect(view.getByDisplayValue('Alaska'), 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 the react_testing_library does not have a direct dependency on over_react - but both libraries are fully supported.

Options

value

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.

Async Options

timeout

How long to wait for the node to appear in the DOM before throwing a TestFailure, defaulting to 1000ms.

interval

How often the callback is called, defaulting to 50ms.

onTimeout

Is called if the timeout duration passes before the node is found in the DOM, and can be used to customize a TestFailure message.

mutationObserverOptions

The default values are:

{subtree: true, childList: true, attributes: true, characterData: true}

which will detect additions and removals of child elements (including text nodes) in the container and any of its descendants. It will also detect attribute changes. When any of those changes occur, it will re-run the callback.

Implementation

Future<E> findByDisplayValue<E extends Element>(
  /*TextMatch*/ dynamic value, {
  bool exact = true,
  NormalizerFn Function([NormalizerOptions?])? normalizer,
  Duration? timeout,
  Duration interval = defaultAsyncCallbackCheckInterval,
  QueryTimeoutFn? onTimeout,
  MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions,
}) {
  // NOTE: Using our own Dart `waitFor` as a wrapper around `getByDisplayValue` instead of an
  // interop like `_jsFindByDisplayValue` to give consumers better async stack traces.
  return waitFor(
    () => getByDisplayValue(
      value,
      exact: exact,
      normalizer: normalizer,
    ),
    container: getContainerForScope(),
    timeout: timeout,
    interval: interval,
    onTimeout: onTimeout,
    mutationObserverOptions: mutationObserverOptions,
  );
}