testNotifier<SN extends StateNotifier<State>, State> function

  1. @visibleForTesting
Future<void> testNotifier<SN extends StateNotifier<State>, State>({
  1. required FutureOr actions(
    1. SN stateNotifier
    ),
  2. dynamic expect()?,
  3. required SN build(),
  4. Iterable<State> seed()?,
  5. FutureOr<void> verify(
    1. SN stateNotifier
    )?,
  6. FutureOr<void> tearDown()?,
  7. dynamic errors()?,
  8. FutureOr<void> setUp()?,
  9. required int skip,
  10. bool expectInitialState = false,
})

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

Implementation

@visibleForTesting
Future<void> testNotifier<SN extends StateNotifier<State>, State>({
  required FutureOr Function(SN stateNotifier) actions,
  dynamic Function()? expect,
  required SN Function() build,
  Iterable<State> Function()? seed,
  FutureOr<void> Function(SN stateNotifier)? verify,
  FutureOr<void> Function()? tearDown,
  dynamic Function()? errors,
  FutureOr<void> Function()? setUp,
  required int skip,
  bool expectInitialState = false,
}) async {
  final unhandledErrors = <Object>[];

  await setUp?.call();
  List<State> states = <State>[];
  final stateNotifier = build();

  stateNotifier.addListener(
    (state) {
      states.add(state);
    },
    fireImmediately: expectInitialState,
  );

  if (seed != null) {
    final seedStates = seed.call();
    for (var state in seedStates) {
      // ignore: invalid_use_of_protected_member
      stateNotifier.state = state;
    }
  }

  try {
    await actions.call(stateNotifier);
  } catch (error) {
    if (errors == null) rethrow;
    unhandledErrors.add(error);
  }
  stateNotifier.dispose();

  states = states.skip(skip).toList();

  final expected = expect?.call();

  try {
    if (expect != null) {
      test.expect(states, test.wrapMatcher(expected));
    }
    // ignore: nullable_type_in_catch_clause
  } on test.TestFailure catch (e) {
    final diff = _diff(expected: expected, actual: states);
    final message = '${e.message}\n$diff';
    // ignore: only_throw_errors
    throw test.TestFailure(message);
  }
  await verify?.call(stateNotifier);
  await tearDown?.call();
}