Sub topic 14
Testing state transitions as plain functions
This chapter assumes Dart 3, sealed classes, pattern matching, generics, and basic Flutter syntax. You should already know how a build method maps state to widgets. The focus here is the logic before the UI: given a current state and an event, what state should come next?
A state transition is easiest to test when it is a plain function. A plain function does not need a widget tree, a BuildContext, or a Flutter test binding. This makes tests fast and keeps business rules separate from presentation.
Keep the reducer independent of Flutter
A reducer is a function that receives the current state and an event. It returns the next state.
For example, a loading feature can accept Fetch, Succeeded, and Errored events:
sealed class LoadState<T> {
const LoadState();
}
final class Loading<T> extends LoadState<T> {
const Loading();
}
final class Loaded<T> extends LoadState<T> {
const Loaded(this.data);
final T data;
}
final class Failed<T> extends LoadState<T> {
const Failed(this.error);
final Object error;
}
sealed class Event {
const Event();
}
final class Fetch extends Event {
const Fetch();
}
final class Succeeded extends Event {
const Succeeded(this.items);
final List<String> items;
}
final class Errored extends Event {
const Errored(this.error);
final Object error;
}
LoadState<List<String>> reduce(
LoadState<List<String>> state,
Event event,
) {
return switch ((state, event)) {
(_, Fetch()) => const Loading<List<String>>(),
(Loading(), Succeeded(items: final items)) => Loaded(items),
(Loading(), Errored(error: final error)) => Failed(error),
(Loaded() || Failed(), Succeeded() || Errored()) => state,
};
}The reducer has no side effects. It does not fetch data or display a message. A separate layer can perform the fetch and send either Succeeded or Errored back to the reducer.
Test outcomes and payloads
Add the test package to your Dart package, then run the tests with dart test. The following file contains the reducer and tests together, so it can run as one test file:
import 'package:test/test.dart';
void main() {
group('reduce', () {
test('Fetch moves any state to Loading', () {
final state = Loaded<List<String>>(['old']);
final next = reduce(state, const Fetch());
expect(next, isA<Loading<List<String>>>());
});
test('Succeeded carries returned items into Loaded', () {
final next = reduce(
const Loading<List<String>>(),
Succeeded(['red', 'blue']),
);
expect(next, isA<Loaded<List<String>>>());
expect((next as Loaded<List<String>>).data, ['red', 'blue']);
});
test('Errored carries the failure into Failed', () {
final error = StateError('network unavailable');
final next = reduce(
const Loading<List<String>>(),
Errored(error),
);
expect(next, isA<Failed<List<String>>>());
expect((next as Failed<List<String>>).error, same(error));
});
test('ignores a success received after loading has ended', () {
final state = Loaded<List<String>>(['current']);
final next = reduce(state, Succeeded(['stale']));
expect(next, same(state));
});
});
}A useful test checks two things: the variant and its payload. Checking only that the result is Loaded would miss an error where the reducer returned the wrong list.
Test every meaningful transition, including events that should be ignored. The final test verifies that a late response cannot replace already displayed data. This rule matters when requests can finish out of order.
Test the boundary cases
Pattern guards create additional branches. For example, if an empty successful response should produce an empty state instead of a normal list, write a separate test for that rule. Each test should name the transition it protects.
Tests also document the reducer’s contract. When you add a new sealed state or event, Dart may report an incomplete switch. Update the reducer and add tests for the new transitions before changing the UI.
Exercise
Why should a reducer test assert both the returned state variant and its data?
Show answer
The variant proves that the transition selected the right state, while the data assertion proves that the state contains the correct payload.