Shuffle/app/test/helpers/router_helpers.dart
cschlaefke adf61b3ba2 test: add pumpApp helpers, fakes and coverage matrix template
Refs #31

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-02 20:21:55 +01:00

45 lines
1.5 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:go_router/go_router.dart';
import 'package:shuffle/core/routing/redirect_source.dart';
/// Steuerbare [RedirectSource]: `targets` bildet Pfad -> Weiterleitungsziel ab.
/// [notifyChanged] löst eine Neubewertung der Redirects aus.
class FakeRedirectSource implements RedirectSource {
FakeRedirectSource([Map<String, String>? targets])
: targets = targets ?? <String, String>{};
final Map<String, String> targets;
final _notifier = ValueNotifier<int>(0);
void notifyChanged() => _notifier.value++;
@override
Listenable get changes => _notifier;
@override
String? redirect(GoRouterState state) => targets[state.uri.path];
}
/// Kleiner Test-Router mit eigenen [routes]. Für Screens, die ohne die
/// App-Routen geprüft werden. Der Aufrufer ruft am Ende `dispose` (z. B. per
/// `addTearDown(router.dispose)`).
GoRouter buildTestRouter({
required List<RouteBase> routes,
String initialLocation = '/',
RedirectSource? redirectSource,
}) => GoRouter(
initialLocation: initialLocation,
routes: routes,
refreshListenable: redirectSource?.changes,
redirect: redirectSource == null
? null
: (context, state) => redirectSource.redirect(state),
);
/// Aktueller Pfad des Routers (ohne Query).
String currentPath(GoRouter router) =>
router.routerDelegate.currentConfiguration.uri.path;
/// Aktuelle URI inklusive Query, z. B. für `?from=`-Prüfungen.
Uri currentUri(GoRouter router) =>
router.routerDelegate.currentConfiguration.uri;