createStaticInteropMock<T extends Object, U extends Object> function Null safety

T createStaticInteropMock<T extends Object, U extends Object>(
  1. U dartMock,
  2. [Object? proto = null]
)

Given a @staticInterop type T and an instance dartMock of a Dart class U that implements the external extension members of T, creates a forwarding mock.

Optionally, you may provide a JS prototype object e.g. the JS value Window.prototype using proto. This allows instanceof and is checks with @Native types to pass with the returned forwarding mock.

When external extension members are called, they will forward to the corresponding implementing member in dartMock. If U does not implement the needed external extension members of T, or if U does not properly override them, it will be considered a compile-time error.

For example:

@JS()
@staticInterop
class JSClass {}

extension JSClassExtension on JSClass {
  external String stringify(int param);
}

@JSExport()
class DartClass {
  String stringify(num param) => param.toString();
}

...

JSClass mock = createStaticInteropMock<JSClass, DartClass>(DartClass());

Implementation

external T createStaticInteropMock<T extends Object, U extends Object>(
    U dartMock,
    [Object? proto = null]);