add<TBloc extends JuiceBloc<BlocState>> method

TBloc add<TBloc extends JuiceBloc<BlocState>>(
  1. TBloc bloc
)

Watches bloc, which the test built: its emissions are recorded from now on and must all be consumed by expect. The flow closes it when the test ends. Returns bloc.

Build the bloc inside the flow, without a logger of its own (the customLogger constructor argument): a bloc that reports its telemetry elsewhere would hide its errors from the flow, and is refused.

A bloc the test does NOT hand to the flow is not recorded (its sends are still waited for).

Implementation

TBloc add<TBloc extends JuiceBloc<BlocState>>(TBloc bloc) {
  _checkRunning();
  if (_watched.containsKey(bloc)) {
    throw ArgumentError('juiceFlowTest: this ${bloc.runtimeType} is '
        'already watched');
  }
  if (bloc.isClosing) {
    throw ArgumentError('juiceFlowTest: this ${bloc.runtimeType} is '
        'closed or closing');
  }
  if (!bloc.reportsTo(_recorder)) {
    throw ArgumentError('juiceFlowTest: this ${bloc.runtimeType} does not '
        'report to the flow — it was given its own logger, or built '
        'before the flow started — so its errors would go unseen. Build '
        'it inside the flow, without a logger.');
  }
  final watch = _Watch(typeName(bloc.runtimeType));
  watch.untap = bloc.tapEmissions((status) {
    watch.pending.add(JuiceEmission<BlocState>(
      status,
      Set<String>.of(status.event?.groupsToRebuild ?? const <String>{}),
    ));
  });
  _watched[bloc] = watch;
  return bloc;
}