run method

Future<void> run({
  1. required BuildContext context,
  2. required ScopeManager scope,
  3. required String name,
  4. Map<String, dynamic> inputs = const {},
  5. Map<String, Map<String, dynamic>> mocks = const {},
  6. String? expectError,
})

Implementation

Future<void> run({
  required BuildContext context,
  required ScopeManager scope,
  required String name,
  Map<String, dynamic> inputs = const {},
  Map<String, Map<String, dynamic>> mocks = const {},
  String? expectError,
}) async {
  final definitions = scope.pageData.actionsMap;
  if (definitions == null || !definitions.containsKey(name)) {
    throw StateError('Global action "$name" is unavailable.');
  }
  for (final dependency in mocks.keys) {
    if (dependency == name) {
      throw StateError('A test cannot mock its target action "$name".');
    }
    if (!definitions.containsKey(dependency)) {
      throw StateError('Mocked global action "$dependency" is unavailable.');
    }
  }
  _target = ExecuteActionAction(name: name, rawInputs: inputs);
  _mocks = mocks;

  // Some production callers deliberately discard their callback futures.
  // Collect their errors in the attempt's zone and wait for owned calls and
  // timers, including callbacks which start more work.
  final done = Completer<void>();
  runZonedGuarded(() async {
    try {
      await runScoped(() =>
          ScreenController().executeActionWithScope(context, scope, _target));
    } catch (error, stack) {
      _recordError(error, stack);
    }
    while (
        !_closed && _error == null && (_pending > 0 || _timers.isNotEmpty)) {
      await (_idle ??= Completer<void>()).future;
    }
    done.complete();
  }, _recordError,
      zoneSpecification: ZoneSpecification(
        createTimer: (self, parent, zone, duration, callback) {
          late final _OwnedTimer timer;
          timer = _OwnedTimer(
              parent.createTimer(zone, duration, () {
                try {
                  if (!_closed) callback();
                } finally {
                  timer.cancel();
                }
              }),
              _timerFinished);
          return _own(timer);
        },
        createPeriodicTimer: (self, parent, zone, duration, callback) {
          late final _OwnedTimer timer;
          timer = _OwnedTimer(
              parent.createPeriodicTimer(zone, duration, (_) {
                if (!_closed) callback(timer);
              }),
              _timerFinished);
          return _own(timer);
        },
      ));
  await done.future;
  close();

  if (_error != null) {
    if (expectError == null || !_error.toString().contains(expectError)) {
      Error.throwWithStackTrace(_error!, _stack!);
    }
  } else if (expectError != null) {
    throw StateError(
        'Expected "$name" to fail with "$expectError", but it completed.');
  }
}