Skip to content

Commit 96d2418

Browse files
committed
added suplementary search if dont have /
1 parent b6d0912 commit 96d2418

File tree

4 files changed

+53
-46
lines changed

4 files changed

+53
-46
lines changed

flutter_modular/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## [4.0.0+4] - 2021-09-16
1+
## [4.0.0+5] - 2021-09-16
22

33
* New documentation is here! [https://modular.flutterando.com.br](https://modular.flutterando.com.br).
44
* Modular design now uses Layered Architecture (Clean Architecture) with 100% code coverage.

flutter_modular/lib/src/presenter/navigation/modular_route_information_parser.dart

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import 'package:modular_core/modular_core.dart';
1313

1414
import 'modular_book.dart';
1515

16-
class ModularRouteInformationParser
17-
extends RouteInformationParser<ModularBook> {
16+
class ModularRouteInformationParser extends RouteInformationParser<ModularBook> {
1817
final GetRoute getRoute;
1918
final GetArguments getArguments;
2019
final SetArguments setArguments;
@@ -30,12 +29,10 @@ class ModularRouteInformationParser
3029
});
3130

3231
@override
33-
Future<ModularBook> parseRouteInformation(
34-
RouteInformation routeInformation) async {
32+
Future<ModularBook> parseRouteInformation(RouteInformation routeInformation) async {
3533
var path = '';
3634
if (!_firstParse) {
37-
if (routeInformation.location == null ||
38-
routeInformation.location == '/') {
35+
if (routeInformation.location == null || routeInformation.location == '/') {
3936
// ignore: invalid_use_of_visible_for_testing_member
4037
path = initialRouteDeclaratedInMaterialApp;
4138
} else {
@@ -56,12 +53,10 @@ class ModularRouteInformationParser
5653
return RouteInformation(location: book.uri.toString());
5754
}
5855

59-
Future<ModularBook> selectBook(String path,
60-
{dynamic arguments, void Function(dynamic)? popCallback}) async {
56+
Future<ModularBook> selectBook(String path, {dynamic arguments, void Function(dynamic)? popCallback}) async {
6157
var route = await selectRoute(path, arguments: arguments);
6258

63-
final modularArgs =
64-
getArguments().getOrElse((l) => ModularArguments.empty());
59+
final modularArgs = getArguments().getOrElse((l) => ModularArguments.empty());
6560
if (popCallback != null) {
6661
route = route.copyWith(popCallback: popCallback);
6762
}
@@ -104,8 +99,17 @@ class ModularRouteInformationParser
10499

105100
final params = RouteParmsDTO(url: path, arguments: arguments);
106101
final result = await getRoute.call(params);
107-
return await result.fold<FutureOr<ParallelRoute>>(
108-
(l) => throw l, (route) => _routeSuccess(route));
102+
return await result.fold<FutureOr<ParallelRoute>>((modularError) async {
103+
if (path.endsWith('/')) {
104+
throw modularError;
105+
}
106+
final params = RouteParmsDTO(url: '$path/', arguments: arguments);
107+
final result = await getRoute.call(params);
108+
return await result.fold((l) => throw modularError, (route) {
109+
print('[MODULAR WARNING] - Please, use $path/ instead of $path.');
110+
return _routeSuccess(route);
111+
});
112+
}, (route) => _routeSuccess(route));
109113
}
110114

111115
FutureOr<ParallelRoute> _routeSuccess(ModularRoute? route) async {

flutter_modular/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_modular
22
description: Smart project structure with dependency injection and route management
3-
version: 4.0.0+4
3+
version: 4.0.0+5
44
homepage: https://github.com/Flutterando/modular
55

66
environment:

flutter_modular/test/src/presenter/navigation/modular_route_information_parser_test.dart

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,20 @@ void main() {
7979
when(() => routeMock.parent).thenReturn('/');
8080
when(() => routeMock.schema).thenReturn('/');
8181
when(() => routeMock.middlewares).thenReturn([Guard()]);
82-
when(() => routeMock.copyWith(schema: any(named: 'schema')))
83-
.thenReturn(routeMock);
82+
when(() => routeMock.copyWith(schema: any(named: 'schema'))).thenReturn(routeMock);
8483

8584
final routeParent = ParallelRouteMock();
8685
when(() => routeParent.uri).thenReturn(Uri.parse('/'));
8786
when(() => routeParent.parent).thenReturn('');
8887
when(() => routeParent.schema).thenReturn('');
8988
when(() => routeParent.middlewares).thenReturn([Guard()]);
90-
when(() => routeParent.copyWith(schema: any(named: 'schema')))
91-
.thenReturn(routeParent);
89+
when(() => routeParent.copyWith(schema: any(named: 'schema'))).thenReturn(routeParent);
9290

9391
when(() => reportPush(routeMock)).thenReturn(right(unit));
9492
when(() => reportPush(routeParent)).thenReturn(right(unit));
9593

96-
when(() => getRoute.call(RouteParmsDTO(url: '/test')))
97-
.thenAnswer((_) async => right(routeMock));
98-
when(() => getRoute.call(RouteParmsDTO(url: '/')))
99-
.thenAnswer((_) async => right(routeParent));
94+
when(() => getRoute.call(RouteParmsDTO(url: '/test'))).thenAnswer((_) async => right(routeMock));
95+
when(() => getRoute.call(RouteParmsDTO(url: '/'))).thenAnswer((_) async => right(routeParent));
10096
when(() => getArguments.call()).thenReturn(right(ModularArguments.empty()));
10197

10298
when(() => setArguments.call(any())).thenReturn(right(unit));
@@ -116,26 +112,21 @@ void main() {
116112
when(() => routeMock.parent).thenReturn('/');
117113
when(() => routeMock.schema).thenReturn('/');
118114
when(() => routeMock.middlewares).thenReturn([Guard()]);
119-
when(() => routeMock.copyWith(schema: any(named: 'schema')))
120-
.thenReturn(routeMock);
115+
when(() => routeMock.copyWith(schema: any(named: 'schema'))).thenReturn(routeMock);
121116

122117
final routeParent = ParallelRouteMock();
123118
when(() => routeParent.uri).thenReturn(Uri.parse('/'));
124119
when(() => routeParent.parent).thenReturn('');
125120
when(() => routeParent.schema).thenReturn('');
126121
when(() => routeParent.middlewares).thenReturn([Guard()]);
127-
when(() => routeParent.copyWith(schema: any(named: 'schema')))
128-
.thenReturn(routeParent);
122+
when(() => routeParent.copyWith(schema: any(named: 'schema'))).thenReturn(routeParent);
129123

130124
when(() => reportPush(routeMock)).thenReturn(right(unit));
131125
when(() => reportPush(routeParent)).thenReturn(right(unit));
132126

133-
when(() => getRoute.call(RouteParmsDTO(url: '/oo')))
134-
.thenAnswer((_) async => right(redirect));
135-
when(() => getRoute.call(RouteParmsDTO(url: '/test', arguments: args)))
136-
.thenAnswer((_) async => right(routeMock));
137-
when(() => getRoute.call(RouteParmsDTO(url: '/')))
138-
.thenAnswer((_) async => right(routeParent));
127+
when(() => getRoute.call(RouteParmsDTO(url: '/oo'))).thenAnswer((_) async => right(redirect));
128+
when(() => getRoute.call(RouteParmsDTO(url: '/test', arguments: args))).thenAnswer((_) async => right(routeMock));
129+
when(() => getRoute.call(RouteParmsDTO(url: '/'))).thenAnswer((_) async => right(routeParent));
139130
when(() => getArguments.call()).thenReturn(right(args));
140131

141132
when(() => setArguments.call(any())).thenReturn(right(unit));
@@ -146,6 +137,27 @@ void main() {
146137
expect(book.chapters('/').first.name, '/test');
147138
});
148139

140+
test('selectRoute with resolver route withless /', () async {
141+
final args = ModularArguments.empty();
142+
143+
final routeMock = ParallelRouteMock();
144+
when(() => routeMock.uri).thenReturn(Uri.parse('/test/'));
145+
when(() => routeMock.parent).thenReturn('');
146+
when(() => routeMock.schema).thenReturn('');
147+
when(() => routeMock.middlewares).thenReturn([]);
148+
149+
when(() => reportPush(routeMock)).thenReturn(right(unit));
150+
151+
when(() => getRoute.call(RouteParmsDTO(url: '/test', arguments: args))).thenAnswer((_) async => left(ModularPageException('')));
152+
when(() => getRoute.call(RouteParmsDTO(url: '/test/', arguments: args))).thenAnswer((_) async => right(routeMock));
153+
when(() => getArguments.call()).thenReturn(right(args));
154+
155+
when(() => setArguments.call(any())).thenReturn(right(unit));
156+
157+
final route = await parser.selectRoute('/test', arguments: args);
158+
expect(route.uri.toString(), '/test/');
159+
});
160+
149161
test('restoreRouteInformation', () {
150162
final route = ParallelRouteMock();
151163
when(() => route.uri).thenReturn(Uri.parse('/test'));
@@ -164,10 +176,8 @@ void main() {
164176
when(() => getArguments.call()).thenReturn(right(ModularArguments.empty()));
165177
when(() => routeMock.middlewares).thenReturn([Guard()]);
166178

167-
expect(parser.parseRouteInformation(RouteInformation(location: '/test')),
168-
completion(isA<ModularBook>()));
169-
expect(parser.parseRouteInformation(RouteInformation(location: '/test')),
170-
completion(isA<ModularBook>()));
179+
expect(parser.parseRouteInformation(RouteInformation(location: '/test')), completion(isA<ModularBook>()));
180+
expect(parser.parseRouteInformation(RouteInformation(location: '/test')), completion(isA<ModularBook>()));
171181
expect(Guard().pre(routeMock), routeMock);
172182
});
173183

@@ -179,10 +189,7 @@ void main() {
179189
when(() => routeMock.middlewares).thenReturn([Guard(false)]);
180190
when(() => routeMock.uri).thenReturn(Uri.parse('/'));
181191

182-
expect(
183-
() async =>
184-
await parser.parseRouteInformation(RouteInformation(location: '/')),
185-
throwsA(isA<GuardedRouteException>()));
192+
expect(() async => await parser.parseRouteInformation(RouteInformation(location: '/')), throwsA(isA<GuardedRouteException>()));
186193
});
187194

188195
test('parseRouteInformation with location / and middleware null', () {
@@ -193,10 +200,7 @@ void main() {
193200
when(() => routeMock.middlewares).thenReturn([MiddlewareNull()]);
194201
when(() => routeMock.uri).thenReturn(Uri.parse('/'));
195202

196-
expect(
197-
() async =>
198-
await parser.parseRouteInformation(RouteInformation(location: '/')),
199-
throwsA(isA<Exception>()));
203+
expect(() async => await parser.parseRouteInformation(RouteInformation(location: '/')), throwsA(isA<Exception>()));
200204
});
201205

202206
test('throw error if path be empty', () {
@@ -211,8 +215,7 @@ void main() {
211215
when(() => routeMock.middlewares).thenReturn([]);
212216
when(() => routeMock.uri).thenReturn(Uri.parse('/'));
213217
when(() => routeMock.parent).thenReturn('');
214-
when(() => routeMock.copyWith(popCallback: any(named: 'popCallback')))
215-
.thenReturn(routeMock);
218+
when(() => routeMock.copyWith(popCallback: any(named: 'popCallback'))).thenReturn(routeMock);
216219
expect(parser.selectBook('/', popCallback: (r) {}), completes);
217220
});
218221
}

0 commit comments

Comments
 (0)