hover static method Null safety
Hovers over element
.
Related: UserEvent.unhover
See: testing-library.com/docs/ecosystem-user-event/#hoverelement
Options
eventInit
Use eventInit
to initialize the onMouseOver
event.
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
<div>
<button>Hover over me!</button>
</div>
import 'package:react/hooks.dart';
import 'package:react/react.dart' as react;
import 'package:react_testing_library/matchers.dart' show isInTheDocument;
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('', () {
final HoverButton = react.registerFunctionComponent((props) {
final isShown = useState(false);
return react.div({}, [
react.button({
'onMouseOver': (_) => isShown.set(true),
'onMouseOut': (_) => isShown.set(false)
}, [
'Hover over me!'
]),
if (isShown.value) react.span({}, ['Hello!']) else null,
]);
});
// Render the DOM shown in the example snippet above.
final view = rtl.render(HoverButton({}));
// Use react_testing_library queries to store references to the node.
final button = view.getByRole('button');
expect(view.queryByText('Hello!'), isNull, reason: 'sanity check');
// Use `UserEvent.hover` to simulate a user mousing over a button.
UserEvent.hover(button);
// Use `isInTheDocument` matcher to verify that the new element is present.
expect(view.getByText('Hello!'), isInTheDocument);
});
}
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 hover(
Element element, {
Map eventInit = const {},
bool skipPointerEventsCheck = false,
}) {
final options = {
'skipPointerEventsCheck': skipPointerEventsCheck,
};
eventHandlerErrorCatcher(() {
getProperty(_userEvent, 'hover')(
element,
_jsifyEventData(eventInit),
jsifyAndAllowInterop(options),
);
});
}