paste static method Null safety

void paste(
  1. Element element,
  2. String text,
  3. {Map eventInit = const {},
  4. int? initialSelectionStart,
  5. int? initialSelectionEnd}
)

Simulates pasting text into element.

See: testing-library.com/docs/ecosystem-user-event#pasteelement-text-eventinit-options

Options

eventInit

Use eventInit to initialize the clipboard event.

With Selection Range

If element already contains a value, text will be pasted at the end of the existing value by default. To override this behavior and set the selection range to something else, call InputElement.setSelectionRange before calling paste.

In order to set the initial selection range to zero, you must also set initialSelectionStart and initialSelectionEnd to zero along with calling element.setSelectionRange(0, 0).

Example

<input value="This is a bad example" />
import 'dart:html';

import 'package:react/react.dart' as react;
import 'package:react_testing_library/matchers.dart' show hasValue;
import 'package:react_testing_library/react_testing_library.dart' as rtl;
import 'package:react_testing_library/user_event.dart';
import 'package:test/test.dart';

void main() {
  test('', () {
    // Render the DOM shown in the example snippet above.
    final view = rtl.render(react.input({'defaultValue': 'This is a bad example'}));

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

    input.setSelectionRange(10, 13);

    // Use `UserEvent.paste` to simulate a user pasting text into an input.
    UserEvent.paste(input, 'good');

    // Use `hasValue` matcher to verify the value of the input.
    expect(input, hasValue('This is a good example'));
  });
}

Warning About Errors

Unlike the JS API, any uncaught errors thrown during event propagation will get rethrown. This helps surface errors that could otherwise go unnoticed since they aren't printed to the terminal when running tests.

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 UserActions}

Implementation

static void paste(
  Element element,
  String text, {
  Map eventInit = const {},
  int? initialSelectionStart,
  int? initialSelectionEnd,
}) {
  final options = {
    if (initialSelectionStart != null) 'initialSelectionStart': initialSelectionStart,
    if (initialSelectionEnd != null) 'initialSelectionEnd': initialSelectionEnd,
  };
  eventHandlerErrorCatcher(() {
    getProperty(_userEvent, 'paste')(
      element,
      text,
      _jsifyEventData(eventInit),
      jsifyAndAllowInterop(options),
    );
  });
}