Skip to content

Commit

Permalink
Give RedirectRoutes a unique name to allow multiple in a collection
Browse files Browse the repository at this point in the history
Resolves an issue where only the last redirect in a collection is persisted
  • Loading branch information
jtothebell committed Aug 15, 2024
1 parent d2a6360 commit 170fd7e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
3 changes: 3 additions & 0 deletions auto_route/lib/src/route/auto_route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
132 changes: 132 additions & 0 deletions auto_route/test/matcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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, '*');
});
});
}

0 comments on commit 170fd7e

Please sign in to comment.