From 11e485eacd54f9b5acfc9e790ac11d1c7d9b8beb Mon Sep 17 00:00:00 2001 From: Greg Littlefield Date: Thu, 9 Nov 2023 15:10:08 -0700 Subject: [PATCH] Update _checkEventType to respect mocks that mock `type` --- lib/src/react_client/event_helpers.dart | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/src/react_client/event_helpers.dart b/lib/src/react_client/event_helpers.dart index 584fe0b6..d7c2d5ff 100644 --- a/lib/src/react_client/event_helpers.dart +++ b/lib/src/react_client/event_helpers.dart @@ -760,11 +760,25 @@ SyntheticWheelEvent createSyntheticWheelEvent({ } extension SyntheticEventTypeHelpers on SyntheticEvent { - // Use getProperty(this, 'type') since, although statically we may be dealing with a SyntheticEvent, - // this could be a non-event JS object cast to SyntheticEvent with a null `type`. - // This is unlikely, but is possible, and before the null safety migration this method - // gracefully returned false instead of throwing. - bool _checkEventType(List types) => getProperty(this, 'type') != null && types.any((t) => type.contains(t)); + // Access `type` in a try-catch since, although statically we may be dealing with a SyntheticEvent, + // this could be an object with a `null` `type` which would cause a type error since `type` is non-nullable. + // + // Cases where this could occur: + // - non-event JS object cast to SyntheticEvent + // - a mock class that hasn't mocked `type` + // + // We could use `getProperty(this, 'type')` to handle the JS object case, but for mock classes it would bypass the + // `type` getter and not behave as expected. + bool _checkEventType(List types) { + String? type; + try { + // This is typed as null statically, but if it's not, it will probably throw (depending on the compiler). + type = this.type; + } catch (_) {} + + return type != null && types.any(type.contains); + } + bool _hasProperty(String propertyName) => hasProperty(this, propertyName); /// Uses Duck Typing to detect if the event instance is a [SyntheticClipboardEvent].