Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give RedirectRoutes a unique name to allow multiple in a collection #2036

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '*');
});
});
}