juiceFlowTest function

  1. @isTest
void juiceFlowTest(
  1. String description,
  2. FutureOr<void> body(
    1. JuiceFlow flow
    ), {
  3. bool virtualTime = false,
  4. Object? tags,
  5. Object? skipTest,
})

Declares a test of SEVERAL blocs and the reactions between them.

juiceTest builds one bloc. A flow holds as many as the test hands it, knows when every bloc in the process is idle, and is exhaustive across all of them: it fails on any emission the test did not assert.

juiceFlowTest('a settings change reaches notes', (flow) async {
  final settings = flow.register<SettingsBloc>(() => SettingsBloc());
  final notes = flow.register<NotesBloc>(() => NotesBloc());

  await flow.send(settings, ToggleViewModeEvent());

  flow.expect(settings, [isUpdatingStatus(state: isGrid)]);
  flow.expect(notes, [isUpdatingStatus(state: isGrid)]);
});

The test works in steps: send (or do anything else), let the blocs go idle with JuiceFlow.send or JuiceFlow.settle, assert with JuiceFlow.expect, and again. No sleeps: idle is a fact the framework reports, not a guess about how long a chain takes.

When the body returns, the flow closes every bloc it holds, resets BlocScope, and FAILS the test if:

A flow owns BlocScope for the test: it will not start while anything is registered there, because the reset at its end would destroy it. One flow runs at a time.

Time

Idle includes time: a sendAfter still pending is not idle, so JuiceFlow.send and JuiceFlow.settle wait for it to fire, and a debounce is tested by letting it run out. JuiceFlow.elapse lets a stated amount of time pass WITHOUT waiting for idle — the way to act in the middle of a delay:

juiceFlowTest('a second keystroke replaces the search', (flow) async {
  final shelf = flow.add(ShelfBloc());
  shelf.typed('j');                                  // sendAfter, 300 ms
  await flow.elapse(const Duration(milliseconds: 100));
  shelf.typed('ju');                                 // replaces it
  await flow.settle();
  flow.expect(shelf, [isUpdatingStatus(state: resultsFor('ju'))]);
}, virtualTime: true);

By default a flow runs on the real clock: a 300 ms debounce takes 300 ms. With virtualTime the same test runs on a virtual clock (package:fake_async): whenever the test is waiting and nothing can happen until time passes, the clock JUMPS to the next timer, so the test reads the same and takes no time. Every timer is virtual — a sendAfter, a Future.delayed in a use case, a waitFor timeout, the 5 seconds JuiceFlow.settle allows — and so is the microtask queue.

What a virtual clock cannot advance is the real world: Hive, sqflite, a platform channel, anything that completes on the real event loop. A virtual-time test that waits on one fails at once, saying so. Keep those tests on the real clock, or fake the seam. While the body waits on something of its own, outside every flow call, blocs that never go idle for an hour of virtual time (a sendAfter poll that re-arms itself) are called stuck too, as is a wait beside only periodic timers.

Work a use case starts and nothing awaits — an unawaited Future.delayed, a raw Timer — is not a send, and the flow cannot wait for it. Under virtual time a timer still pending when the flow ends fails the test. On the real clock it is not seen: it runs after the test, against closed blocs. Use sendAfter, which the flow waits for.

Implementation

@isTest
void juiceFlowTest(
  String description,
  FutureOr<void> Function(JuiceFlow flow) body, {
  bool virtualTime = false,
  Object? tags,
  Object? skipTest,
}) {
  scaffolding.test(
    description,
    () => runJuiceFlow(body, virtualTime: virtualTime),
    tags: tags,
    skip: skipTest,
  );
}