Description
With dart:js_interop
, mocking needs to be done at the JS level due to the use of extension types.
For example:
extension type Window._(JSObject _) implements JSObject {
external String get name;
void method() {}
}
name
will call the member within the underlying JS object, whereas method
is unmockable (due to static dispatch).
To help create mocks for this as well as to export Dart objects, we added @JSExport()
and createJSInteropWrapper
e.g.
@JSExport()
class WindowFake {
String get name => 'fake';
}
void main() {
var wrapper = createJSInteropWrapper(WindowFake()) as Window;
wrapper.name; // 'fake'
}
This creates a JS wrapper around the Dart object and forwards the JS interop calls to the Dart object. @JSExport
is needed in order to do some validation and identify a class as "exportable".
GenerateNiceMocks
allows users to generate a mock given a definition like WindowFake
. This allows users to stub calls instead of just using a fake, but it requires the generated mock to have a @JSExport
annotation, something which we don't have support for today.