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

fix: Solve the problem that selectRoute cannot recover after finding a wildcard route during the initial attempt. #977

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin"))
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_modular/src/domain/errors/errors.dart';
import 'package:result_dart/result_dart.dart';

import '../../../flutter_modular.dart';
Expand Down Expand Up @@ -112,10 +113,16 @@ class ModularRouteInformationParser
path = _resolverPath(path);

final params = RouteParmsDTO(url: path, arguments: arguments);
return getRoute
.call(params) //
.map(_routeSuccess)
.recover((modularError) {

final fistTrying = getRoute.call(params).flatMap<ModularRoute>((success) {
if (success.name == '/**') {
return const Failure(RouteNotFoundException(
'Wildcard is not available for the first time'));
}
return Success(success);
});

return fistTrying.map(_routeSuccess).recover((modularError) {
final params = RouteParmsDTO(url: '$path/', arguments: arguments);
return getRoute
.call(params) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void main() {
when(() => routeMock.uri).thenReturn(Uri.parse('/'));
when(() => routeMock.parent).thenReturn('');
when(() => routeMock.schema).thenReturn('');
when(() => routeMock.name).thenReturn('/');
when(() => getRoute.call(any()))
.thenAnswer((_) async => Success(routeMock));
when(() => getArguments.call())
Expand All @@ -86,6 +87,7 @@ void main() {
when(() => routeMock.uri).thenReturn(Uri.parse('/test'));
when(() => routeMock.parent).thenReturn('/');
when(() => routeMock.schema).thenReturn('/');
when(() => routeMock.name).thenReturn('/test');
when(() => routeMock.middlewares).thenReturn([Guard()]);
when(() => routeMock.copyWith(schema: any(named: 'schema')))
.thenReturn(routeMock);
Expand All @@ -94,6 +96,7 @@ void main() {
when(() => routeParent.uri).thenReturn(Uri.parse('/'));
when(() => routeParent.parent).thenReturn('');
when(() => routeParent.schema).thenReturn('');
when(() => routeParent.name).thenReturn('/');
when(() => routeParent.middlewares).thenReturn([Guard()]);
when(() => routeParent.copyWith(schema: any(named: 'schema')))
.thenReturn(routeParent);
Expand Down Expand Up @@ -124,6 +127,7 @@ void main() {
when(() => routeMock.uri).thenReturn(Uri.parse('/test'));
when(() => routeMock.parent).thenReturn('/');
when(() => routeMock.schema).thenReturn('/');
when(() => routeMock.name).thenReturn('/test');
when(() => routeMock.middlewares).thenReturn([Guard()]);
when(() => routeMock.copyWith(schema: any(named: 'schema')))
.thenReturn(routeMock);
Expand All @@ -132,6 +136,7 @@ void main() {
when(() => routeParent.uri).thenReturn(Uri.parse('/'));
when(() => routeParent.parent).thenReturn('');
when(() => routeParent.schema).thenReturn('');
when(() => routeParent.name).thenReturn('/');
when(() => routeParent.middlewares).thenReturn([Guard()]);
when(() => routeParent.copyWith(schema: any(named: 'schema')))
.thenReturn(routeParent);
Expand Down Expand Up @@ -189,6 +194,7 @@ void main() {
final routeMock = ParallelRouteMock();
when(() => routeMock.uri).thenReturn(Uri.parse('/'));
when(() => routeMock.parent).thenReturn('');
when(() => routeMock.name).thenReturn('/');

when(() => reportPush(routeMock)).thenReturn(const Success(unit));

Expand Down Expand Up @@ -216,6 +222,7 @@ void main() {
.thenReturn(Success(ModularArguments.empty()));
when(() => routeMock.middlewares).thenReturn([Guard(false)]);
when(() => routeMock.uri).thenReturn(Uri.parse('/'));
when(() => routeMock.name).thenReturn('/');

expect(
() =>
Expand All @@ -232,6 +239,7 @@ void main() {
.thenReturn(Success(ModularArguments.empty()));
when(() => routeMock.middlewares).thenReturn([MiddlewareNull()]);
when(() => routeMock.uri).thenReturn(Uri.parse('/'));
when(() => routeMock.name).thenReturn('/');

expect(
() =>
Expand All @@ -252,11 +260,60 @@ void main() {
.thenReturn(Success(ModularArguments.empty()));
when(() => routeMock.middlewares).thenReturn([]);
when(() => routeMock.uri).thenReturn(Uri.parse('/'));
when(() => routeMock.name).thenReturn('/');
when(() => routeMock.parent).thenReturn('');
when(() => routeMock.copyWith(popCallback: any(named: 'popCallback')))
.thenReturn(routeMock);
expect(parser.selectBook('/', popCallback: (r) {}), completes);
});

test('selectRoute with wildcard', () async {
final routeMock = ParallelRouteMock();
when(() => routeMock.uri).thenReturn(Uri.parse('/parent/test'));
when(() => routeMock.parent).thenReturn('/parent');
when(() => routeMock.schema).thenReturn('/parent');
when(() => routeMock.name).thenReturn('/test');
when(() => routeMock.middlewares).thenReturn([Guard()]);
when(() => routeMock.copyWith(schema: any(named: 'schema')))
.thenReturn(routeMock);

final routeMock1 = ParallelRouteMock();
when(() => routeMock1.uri).thenReturn(Uri.parse('/**'));
when(() => routeMock1.parent).thenReturn('');
when(() => routeMock1.schema).thenReturn('');
when(() => routeMock1.name).thenReturn('/**');
when(() => routeMock1.middlewares).thenReturn([Guard()]);
when(() => routeMock1.copyWith(schema: any(named: 'schema')))
.thenReturn(routeMock);

final routeParent = ParallelRouteMock();
when(() => routeParent.uri).thenReturn(Uri.parse('/parent'));
when(() => routeParent.parent).thenReturn('');
when(() => routeParent.schema).thenReturn('');
when(() => routeParent.name).thenReturn('/parent');
when(() => routeParent.middlewares).thenReturn([Guard()]);
when(() => routeParent.copyWith(schema: any(named: 'schema')))
.thenReturn(routeParent);

when(() => reportPush(routeMock)).thenReturn(const Success(unit));
when(() => reportPush(routeMock1)).thenReturn(const Success(unit));
when(() => reportPush(routeParent)).thenReturn(const Success(unit));

when(() => getRoute.call(const RouteParmsDTO(url: '/parent/test')))
.thenAnswer((_) async => Success(routeMock1));
when(() => getRoute.call(const RouteParmsDTO(url: '/parent/test/')))
.thenAnswer((_) async => Success(routeMock));
when(() => getRoute.call(const RouteParmsDTO(url: '/parent')))
.thenAnswer((_) async => Success(routeParent));
when(() => getArguments.call())
.thenReturn(Success(ModularArguments.empty()));

when(() => setArguments.call(any())).thenReturn(const Success(unit));

final book = await parser.selectBook('/parent/test');
expect(book.uri.toString(), '/parent/test');
expect(book.chapters().first.name, '/parent');
});
}

class Guard extends RouteGuard {
Expand Down
Loading