Skip to content

Commit 11e485e

Browse files
Update _checkEventType to respect mocks that mock type
1 parent 70cc2b7 commit 11e485e

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lib/src/react_client/event_helpers.dart

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,25 @@ SyntheticWheelEvent createSyntheticWheelEvent({
760760
}
761761

762762
extension SyntheticEventTypeHelpers on SyntheticEvent {
763-
// Use getProperty(this, 'type') since, although statically we may be dealing with a SyntheticEvent,
764-
// this could be a non-event JS object cast to SyntheticEvent with a null `type`.
765-
// This is unlikely, but is possible, and before the null safety migration this method
766-
// gracefully returned false instead of throwing.
767-
bool _checkEventType(List<String> types) => getProperty(this, 'type') != null && types.any((t) => type.contains(t));
763+
// Access `type` in a try-catch since, although statically we may be dealing with a SyntheticEvent,
764+
// this could be an object with a `null` `type` which would cause a type error since `type` is non-nullable.
765+
//
766+
// Cases where this could occur:
767+
// - non-event JS object cast to SyntheticEvent
768+
// - a mock class that hasn't mocked `type`
769+
//
770+
// We could use `getProperty(this, 'type')` to handle the JS object case, but for mock classes it would bypass the
771+
// `type` getter and not behave as expected.
772+
bool _checkEventType(List<String> types) {
773+
String? type;
774+
try {
775+
// This is typed as null statically, but if it's not, it will probably throw (depending on the compiler).
776+
type = this.type;
777+
} catch (_) {}
778+
779+
return type != null && types.any(type.contains);
780+
}
781+
768782
bool _hasProperty(String propertyName) => hasProperty(this, propertyName);
769783

770784
/// Uses Duck Typing to detect if the event instance is a [SyntheticClipboardEvent].

0 commit comments

Comments
 (0)