isFocused top-level constant Matchers

Matcher const isFocused

Allows you to assert whether an element has focus or not.

Similar to jest-dom's toHaveFocus matcher.

Examples

<input type="text" name="username" value="jane.doe" />
import 'package:react/react.dart' as react;
import 'package:react_testing_library/matchers.dart' show isFocused;
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.input({'type': 'text', 'name': 'username'}));

    // Use react_testing_library queries to store references to the node(s)
    final input = view.getByRole('textbox');

    // Use the `isFocused` matcher as the second argument of `expect()`
    input.focus();
    expect(input, isFocused);

    input.blur();
    expect(input, isNot(isFocused));
  });
}

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.

{@category Matchers}

Implementation

const Matcher isFocused = _IsFocused();