From 170fd7e9d19d9c96fb8c2e943a28eecc04a31266 Mon Sep 17 00:00:00 2001 From: Jon Bell Date: Wed, 14 Aug 2024 16:31:58 -0600 Subject: [PATCH] Give RedirectRoutes a unique name to allow multiple in a collection Resolves an issue where only the last redirect in a collection is persisted --- .../lib/src/route/auto_route_config.dart | 3 + auto_route/test/matcher_test.dart | 132 ++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/auto_route/lib/src/route/auto_route_config.dart b/auto_route/lib/src/route/auto_route_config.dart index 52179d1e..33593fdf 100644 --- a/auto_route/lib/src/route/auto_route_config.dart +++ b/auto_route/lib/src/route/auto_route_config.dart @@ -296,6 +296,9 @@ class RedirectRoute extends AutoRoute { page: PageInfo.redirect, fullMatch: true, ); + + @override + String get name => "${page.name}-$path-$redirectTo"; } /// Builds an [AutoRoute] instance with [RouteType.material] type diff --git a/auto_route/test/matcher_test.dart b/auto_route/test/matcher_test.dart index b364642d..c11fd9e3 100644 --- a/auto_route/test/matcher_test.dart +++ b/auto_route/test/matcher_test.dart @@ -108,6 +108,37 @@ void main() { expect(collection.routes, expectedRoutes); }); + test('RouteCollection.fromList root should not remove multiple redirects', + () { + final routeCollection = RouteCollection.fromList( + [ + TestRoute('A', path: '/'), + TestRoute('B', path: '/b'), + RedirectRoute(path: '/c', redirectTo: '/b'), + TestRoute('D', path: '/d'), + RedirectRoute(path: '*', redirectTo: '/'), + ], + root: true, + ); + expect(routeCollection.routes.length, 5); + }); + + test( + 'RouteCollection.fromList non root should not remove multiple redirects', + () { + final routeCollection = RouteCollection.fromList( + [ + TestRoute('A', path: 'a'), + TestRoute('B', path: 'b'), + RedirectRoute(path: 'c', redirectTo: 'b'), + TestRoute('D', path: 'd'), + RedirectRoute(path: '*', redirectTo: 'a'), + ], + root: false, + ); + expect(routeCollection.routes.length, 5); + }); + test('Calling findPathTo to C1 should return a list of route trails[C,C1]', () { expect(collection.findPathTo('C1'), [routeC, subRouteC1]); @@ -743,4 +774,105 @@ void main() { expect(match('/a?foo=bar&foo=baz'), expectedMatches); }); }); + + group( + 'Testing matching of nested routes with a wildcard redirect and other redirects at the root', + () { + final cSubRoute = TestRoute( + 'C1', + path: '', + ); + final aRoute = TestRoute( + 'A', + path: '', + ); + final bRoute = TestRoute( + 'B', + path: 'b', + ); + final cRoute = TestRoute( + 'C', + path: 'c', + children: [ + cSubRoute, + ], + ); + final root = TestRoute( + 'Root', + path: '/', + initial: true, + children: [ + aRoute, + bRoute, + RedirectRoute( + path: 'd', + redirectTo: 'b', + ), + cRoute, + RedirectRoute(path: '*', redirectTo: ''), + ], + ); + + final routeCollection = RouteCollection.fromList( + [root], + root: true, + ); + + final match = RouteMatcher(routeCollection).match; + + test('Should return correct match for /c', () { + final expectedMatches = [ + RouteMatch( + config: root, + key: const ValueKey('Root'), + stringMatch: '/', + segments: const [ + '/' + ], + children: [ + RouteMatch( + config: cRoute, + key: const ValueKey('C'), + stringMatch: 'c', + segments: const ['c'], + children: [ + RouteMatch( + config: cSubRoute, + key: const ValueKey('C1'), + stringMatch: '', + segments: const [], + ), + ], + ) + ]) + ]; + expect(match('/c'), expectedMatches); + }); + + test('Should return correct match for /d', () { + final expectedMatches = [ + RouteMatch( + config: root, + key: const ValueKey('Root'), + stringMatch: '/', + segments: const [ + '/' + ], + children: [ + RouteMatch( + config: bRoute, + key: const ValueKey('B'), + stringMatch: 'b', + redirectedFrom: 'd', + segments: const ['b'], + ) + ]) + ]; + expect(match('/d'), expectedMatches); + }); + + test('Should return a redirect match for bogus url /adsfadsg', () { + expect(match('/adsfadsg')?.first.children?.first.redirectedFrom, '*'); + }); + }); }