Files
sdk/tests/lib/js/js_util/promise_reject_null_test.dart
T
Srujan Gaddam 6929718456 [pkg:js] Handle null value in promise rejection
Closes https://github.com/dart-lang/sdk/issues/44602

Creates an exception to signal a `null`/`undefined` value when a
converted promise is rejected with `null`/`undefined`.

Change-Id: Ic7f14e23c6c1d51d6dbcc5831ffa8491418a4267
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192046
Commit-Queue: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
2021-03-25 01:30:23 +00:00

65 lines
1.5 KiB
Dart

@JS()
library promise_reject_null_test;
import 'package:js/js.dart';
import 'package:js/js_util.dart' show promiseToFuture, NullRejectionException;
import 'package:expect/minitest.dart';
@JS()
external void eval(String s);
@JS('Promise.reject')
external dynamic getRejectedPromise(v);
@JS()
external void reject(v);
@JS()
external dynamic getNewPromise();
void main() async {
eval('''
self.getNewPromise = function () {
return new Promise(function (_, reject) {
self.reject = reject;
});
};
''');
// Rejected promise with a `null` value should trigger a
// `NullRejectionException`.
await promiseToFuture(getRejectedPromise(null)).then((_) {
fail("Expected promise to reject and not fulfill.");
}).catchError((e) {
expect(e is NullRejectionException, true);
expect(e.isUndefined, false);
});
// Similar to the above, except we reject using JS interop.
var future = promiseToFuture(getNewPromise()).then((_) {
fail("Expected promise to reject and not fulfill.");
}).catchError((e) {
expect(e is NullRejectionException, true);
expect(e.isUndefined, false);
});
reject(null);
await future;
// It's also possible to reject with `undefined`. Make sure that the exception
// correctly flags that case.
future = promiseToFuture(getNewPromise()).then((_) {
fail("Expected promise to reject and not fulfill.");
}).catchError((e) {
expect(e is NullRejectionException, true);
expect(e.isUndefined, true);
});
eval('''
self.reject(undefined);
''');
await future;
}