clear static method Null safety
- Element element
Selects the text inside element
and deletes it.
element
can be an InputElement or TextAreaElement.
See: testing-library.com/docs/ecosystem-user-event/#clearelement
Example
<input value="Hello, World!" />
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': 'Hello, World!',
}));
// Use react_testing_library queries to store references to the nodes.
final input = view.getByRole('textbox');
expect(input, hasValue('Hello, World!'), reason: 'sanity check');
// Use `UserEvent.clear` to simulate a user deleting the contents of the input.
UserEvent.clear(input);
// Use the `hasValue` matcher to verify the value of the input.
expect(input, hasValue(''));
});
}
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 thereact_testing_library
does not have a direct dependency onover_react
- but both libraries are fully supported.
{@category UserActions}
Implementation
static void clear(Element element) => eventHandlerErrorCatcher(() => getProperty(_userEvent, 'clear')(element));