fireEvent function UserActions
Fires a DOM event
on the provided element
.
NOTE:
Most projects have a few use cases for fireEvent, but the majority of the time you should probably use UserEvent utility methods instead.
Read more about interactions vs. events
Example
<button>Submit</button>
import 'package:react/react.dart' as react;
import 'package:react_testing_library/react_testing_library.dart' as rtl;
import 'package:test/test.dart';
void main() {
test('', () {
// Render the DOM shown in the example snippet above.
final view = rtl.render(react.button({}, 'Submit'));
// Use react_testing_library queries to store a reference to the node.
final button = view.getByRole('button', name: 'Submit');
// Use `fireEvent` to create a click event, and emit it on the node.
rtl.fireEvent(
button,
MouseEvent('click'),
);
});
}
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.
Related: fireEventByName
See: testing-library.com/docs/dom-testing-library/api-events/#fireevent
Implementation
bool fireEvent(Element element, Event event) => eventHandlerErrorCatcher(() => _fireEvent(element, event));