internalControllerTest<C extends BaseController<State>, State> function

  1. @visibleForTesting
Future<void> internalControllerTest<C extends BaseController<State>, State>({
  1. required C build(),
  2. State seed()?,
  3. dynamic act(
    1. C controller
    )?,
  4. Duration? wait,
  5. int skip = 0,
  6. dynamic expect()?,
  7. dynamic verify(
    1. C controller
    )?,
  8. dynamic errors()?,
})

Internal testController runner which is only visible for testing. This should never be used directly -- please use testController instead.

Implementation

@visibleForTesting
Future<void> internalControllerTest<C extends BaseController<State>, State>({
  required C Function() build,
  State Function()? seed,
  Function(C controller)? act,
  Duration? wait,
  int skip = 0,
  dynamic Function()? expect,
  Function(C controller)? verify,
  dynamic Function()? errors,
}) async {
  final unhandledErrors = <Object>[];
  var shallowEquality = false;
  await runZonedGuarded(
    () async {
      final states = <State>[];
      final controller = build();
      // ignore: invalid_use_of_visible_for_testing_member, invalid_use_of_protected_member
      if (seed != null) controller.emit(seed());
      final subscription = controller.stream.skip(skip).listen(states.add);
      try {
        await act?.call(controller);
      } catch (error) {
        unhandledErrors.add(
          error is ControllerUnhandledErrorException ? error.error : error,
        );
      }
      if (wait != null) await Future<void>.delayed(wait);
      await Future<void>.delayed(Duration.zero);
      controller.onClose();
      if (expect != null) {
        final dynamic expected = expect();
        shallowEquality = '$states' == '$expected';
        test.expect(states, test.wrapMatcher(expected));
      }
      await subscription.cancel();
      await verify?.call(controller);
    },
    (Object error, _) {
      if (error is ControllerUnhandledErrorException) {
        unhandledErrors.add(error.error);
      } else if (shallowEquality && error is test.TestFailure) {
        // ignore: only_throw_errors
        throw test.TestFailure(
          // ignore: leading_newlines_in_multiline_strings
          '''${error.message}
WARNING: Please ensure state instances extend Equatable, override == and hashCode, or implement Comparable.
Alternatively, consider using Matchers in the expect of the testController rather than concrete state instances.\n''',
        );
      } else {
        // ignore: only_throw_errors
        throw error;
      }
    },
  );
  if (errors != null) test.expect(unhandledErrors, test.wrapMatcher(errors()));
}