Tutorials
Check the actual Flutter page
Render a real nested router, control Guard permission and advance timeout clocks explicitly.
A correct logical stack does not prove that Product is visible. This example builds a real WidgetsApp.router, mounts a nested Navigator and checks Flutter finders before and after a controlled Guard.
Set up and run
Use the dependencies from the first test. Save this complete file as test/navigation_host_test.dart and run:
flutter test test/navigation_host_test.dart
Expect two passing widget tests. They run in Flutter's test engine, not on a physical device. No Material or Cupertino dependency is needed.
What the first test proves
- NavigationSession owns the actual routerConfig. Observer inputs are attached before navigation; the borrowed observer does not own the Session.
- WidgetsApp.router renders Shell and its NavigationActiveOutlet. Product's Guard waits on a test-controlled Completer.
- After one pump, commit is still false and find.text('Product') finds nothing. The helper does not secretly approve the Guard.
- The test completes permission, pumps work, checks the real commit and then settles the finite UI animation. Both the Finder and full child stack must agree.
- Pop returns “selected” through the original typed ticket. Product disappears and exactly one popped exit is recorded.
- The Widget tree is unmounted before Session disposal. Observation stops afterwards.
What the second test proves
Flutter testWidgets uses fake time. A 10 ms wait does not expire just because it is awaited. After a 9 ms pump, no timeout has fired; after the final 1 ms, the helper reports its own timeout.
The source Completer is completed separately after that failure. This demonstrates the boundary: ending a wait does not cancel its producer.
Complete file
All routes, Graph configuration, Shell and Guard are included. Versioned source.
import 'dart:async';
import 'package:ark_navigation/ark_navigation.dart';
import 'package:ark_navigation_test/ark_navigation_test.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets(
'observes an actual nested host with an explicitly controlled Guard',
(final WidgetTester tester) async {
final Completer<NavigationGuardDecision> permission =
Completer<NavigationGuardDecision>();
final NavigationTestObserver observer = NavigationTestObserver(
sessionId: _F.session,
);
final NavigationSession session = NavigationSession(
id: _F.session,
graph: _F.graph(_Gate(permission)),
eventSinks: <NavigationEventSink>[observer.eventSink],
lifecycleObservers: <NavigationLifecycleObserver>[observer],
failureSink: observer.failureSink,
);
addTearDown(() {
session.dispose();
observer.stop();
});
await tester.pumpWidget(
WidgetsApp.router(
color: const Color(0xff151210),
routerConfig: session.routerConfig,
),
);
await tester.pumpAndSettle(); // Only waits for this finite initial UI.
expect(find.text('Shell'), findsOneWidget);
final NavigationTicket<String> ticket = const _Product()
.on(session.controller)
.push();
bool committed = false;
unawaited(
ticket.committed.then<void>((final NavigationOperationResult result) {
committed = result.isCommitted;
}),
);
await tester.pump();
expect(committed, isFalse);
expect(find.text('Product'), findsNothing);
// pumpAndSettle would not be proof that this pending Guard resolved.
permission.complete(const NavigationGuardAllowed());
await tester.pump();
final NavigationOperationResult result = await ticket.committed;
expect(result, isA<NavigationCommitted>());
await tester.pumpAndSettle(); // Render the now-committed neutral page.
expect(find.text('Product'), findsOneWidget);
expect(
session.controller.value,
NavigationTestMatchers.hasStack(
_F.catalog,
const <NavigationTestEntryExpectation>[
NavigationTestEntryExpectation(routeId: _F.productRoute),
],
),
);
final NavigationEntryId entryId =
(result as NavigationCommitted).entryId!;
final Future<NavigationOperationResult> pop = session.controller.pop(
scopeId: _F.catalog,
result: 'selected',
);
await tester.pump();
expect(await pop, isA<NavigationCommitted>());
expect(
(await ticket.completed as NavigationPopped<String>).result,
'selected',
);
await tester.pumpAndSettle();
expect(find.text('Product'), findsNothing);
expect(
observer,
NavigationTestMatchers.hasLifecycleExit(
sessionId: _F.session,
entryId: entryId,
reason: NavigationEntryExitReason.popped,
count: 1,
),
);
// The widget tree releases its users before the actual owner is disposed.
await tester.pumpWidget(const SizedBox.shrink());
session.dispose();
observer.stop();
expect(observer, NavigationTestMatchers.hasNoObservedFailures());
expect(tester.takeException(), isNull);
},
);
testWidgets('a wait deadline advances only when the test pumps fake time', (
final WidgetTester tester,
) async {
final Completer<NavigationOperationResult> source =
Completer<NavigationOperationResult>();
Object? failure;
unawaited(
NavigationTestWait.operation(
source.future,
timeout: const Duration(milliseconds: 10),
).then<void>(
(final NavigationOperationResult result) {},
onError: (final Object error, final StackTrace stack) =>
failure = error,
),
);
await tester.pump(const Duration(milliseconds: 9));
expect(failure, isNull);
await tester.pump(const Duration(milliseconds: 1));
expect(failure, isA<NavigationTestWaitException>());
source.complete(const NavigationNoChange('late'));
await tester.pump();
expect(tester.takeException(), isNull);
});
}
final class _Shell extends NavigationDestination<void> {
const _Shell();
}
final class _Product extends NavigationDestination<String> {
const _Product();
}
final class _Gate implements NavigationGuard {
const _Gate(this.permission);
final Completer<NavigationGuardDecision> permission;
@override
FutureOr<NavigationGuardDecision> evaluate(
final NavigationPolicyContext context,
) => context.phase == NavigationGuardPhase.enter
? permission.future
: const NavigationGuardAllowed();
}
abstract final class _F {
static const NavigationSessionId session = NavigationSessionId('widget-test');
static const NavigationScopeId root = NavigationScopeId('root');
static const NavigationScopeId catalog = NavigationScopeId('catalog');
static const NavigationRouteId productRoute = NavigationRouteId('product');
static NavigationGraph graph(final NavigationGuard guard) => NavigationGraph(
rootScopeId: root,
scopes: const <NavigationScopeDefinition>[
NavigationScopeDefinition(id: root),
NavigationScopeDefinition(id: catalog, parentId: root),
],
modules: <NavigationModule>[
NavigationModule(
id: const NavigationModuleId('app'),
routes: <NavigationRouteDefinitionBase>[
NavigationRouteDefinition<_Shell, void>(
id: const NavigationRouteId('shell'),
pageBuilder:
(
final BuildContext context,
final _Shell d,
final NavigationEntrySnapshot e,
) => const Column(
children: <Widget>[
Text('Shell'),
Expanded(child: NavigationActiveOutlet()),
],
),
uriCodec: CallbackNavigationUriCodec<_Shell>(
pattern: '/',
encoder: (final _Shell d) => Uri(path: '/'),
decoder: (final NavigationUriMatch m) => const _Shell(),
),
),
],
),
NavigationModule(
id: const NavigationModuleId('catalog'),
routes: <NavigationRouteDefinitionBase>[
NavigationRouteDefinition<_Product, String>(
id: productRoute,
restorationPolicy: NavigationRestorationPolicy.sessionOnly,
pageBuilder:
(
final BuildContext context,
final _Product d,
final NavigationEntrySnapshot e,
) => const Text('Product'),
guards: <NavigationGuard>[guard],
),
],
),
],
mounts: <NavigationMount>[
NavigationMount(
id: const NavigationMountId('root'),
moduleId: const NavigationModuleId('app'),
scopeId: root,
),
NavigationMount(
id: const NavigationMountId('catalog'),
moduleId: const NavigationModuleId('catalog'),
scopeId: catalog,
),
],
);
}
Avoid two common mistakes
Do not call pumpAndSettle expecting it to resolve a pending network request or Guard. Control the producer explicitly, then use pumps to render the known result.
Do not infer iOS/Android gesture support or browser-history behavior from these tests. Keep those platform checks separate. For application resource cleanup, await the owner's acknowledgement as in the Catalog test, not just didExit.