click static method Null safety
Clicks element
, depending on what element
is it can have different
side effects.
See: testing-library.com/docs/ecosystem-user-event/#clickelement-eventinit-options
Options
eventInit
Use eventInit
to set options on the initial MouseEvent. For example,
UserEvent.click(element, eventInit: {'shiftKey': true});
skipHover
click will trigger hover events before clicking. To disable this, set
skipHover
to true
.
clickCount
Use clickCount
to update the initial click count. See documentation on
UIEvent.detail
for more information.
skipPointerEventsCheck
Attempting to interact with an element that has 'pointer-events: none'
set will throw an error. Use skipPointerEventsCheck
to prevent that error
from being thrown by skipping the check of whether any element in the
DOM-tree has 'pointer-events: none'
set. In addition, the underlying
check for pointer events is costly in general and very costly when rendering
large DOM-trees. Can be used to speed up tests.
Example
<input type="checkbox" />
import 'package:react/react.dart' as react;
import 'package:react_testing_library/matchers.dart' show isChecked;
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({'type': 'checkbox'}));
// Use react_testing_library queries to store references to the node.
final checkbox = view.getByRole('checkbox');
// Use `UserEvent.click` to simulate a user clicking the checkbox.
UserEvent.click(checkbox);
// Use `isChecked` matcher to verify that the checkbox was clicked.
expect(checkbox, isChecked);
});
}
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 click(
Element element, {
Map eventInit = const {},
bool skipHover = false,
int clickCount = 0,
bool skipPointerEventsCheck = false,
}) {
final options = {
'skipHover': skipHover,
'clickCount': clickCount,
'skipPointerEventsCheck': skipPointerEventsCheck,
};
eventHandlerErrorCatcher(() {
getProperty(_userEvent, 'click')(
element,
_jsifyEventData(eventInit),
jsifyAndAllowInterop(options),
);
});
}