Skip to content

Commit

Permalink
Merge pull request #11 from vuongxuongminh/fix/chain-redirector-not-w…
Browse files Browse the repository at this point in the history
…ait-future

fix: bug chain redirector not wait future.
  • Loading branch information
vuongxuongminh authored Apr 2, 2023
2 parents dd4cecf + 8f5db95 commit 8cca644
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 61 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
4.1.1
-----

+ Fix bug chain redirector not wait future.

4.1.0
-----

Expand Down
4 changes: 2 additions & 2 deletions lib/src/redirector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class ChainRedirector implements Redirector {
Screen screen,
BuildContext context,
GoRouterState state,
) {
) async {
for (final redirector in _redirectors) {
if (redirector is RestrictRedirector &&
!redirector.shouldRedirect(screen)) {
continue;
}

final path = redirector.redirect(screen, context, state);
final path = await redirector.redirect(screen, context, state);

if (path != null) {
return path;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: go_router_plus
description: Building blocks screens, access control and refresh notifiers base on Go Router.
version: 4.1.0
version: 4.1.1
repository: https://github.com/vuongxuongminh/go_router_plus

environment:
Expand Down
8 changes: 8 additions & 0 deletions test/src/go_router_plus_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void main() {
),
);

await tester.pumpAndSettle();

expect(find.text('F'), findsOneWidget);
});

Expand All @@ -48,6 +50,8 @@ void main() {
),
);

await tester.pumpAndSettle();

final t = find.text('F');

await tester.tap(t);
Expand Down Expand Up @@ -100,6 +104,8 @@ void main() {
),
);

await tester.pumpAndSettle();

final t = find.text('F');

await tester.tap(t);
Expand Down Expand Up @@ -132,6 +138,8 @@ void main() {
),
);

await tester.pumpAndSettle();

expect(find.text('Shell screen'), findsOneWidget);

final t = find.text('F');
Expand Down
23 changes: 21 additions & 2 deletions test/src/redirectors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import 'screens.dart';

class RedirectorA implements RestrictRedirector {
@override
String? redirect(Screen screen, BuildContext context, GoRouterState state) {
String? redirect(
Screen screen,
BuildContext context,
GoRouterState state,
) {
return '/b';
}

Expand All @@ -17,7 +21,22 @@ class RedirectorA implements RestrictRedirector {

class RedirectorB implements Redirector {
@override
String? redirect(Screen screen, BuildContext context, GoRouterState state) {
String? redirect(
Screen screen,
BuildContext context,
GoRouterState state,
) {
return '/b';
}
}

class RedirectorC implements Redirector {
@override
Future<String?> redirect(
Screen screen,
BuildContext context,
GoRouterState state,
) async {
return null;
}
}
121 changes: 65 additions & 56 deletions test/src/redirectors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,76 @@ import 'screens.dart';

@GenerateMocks([GoRouterState, BuildContext])
void main() {
test('test non restrict screen, chain redirector should not redirect', () {
final redirector = ChainRedirector([
RedirectorA(),
]);
test(
'test non restrict screen, chain redirector should not redirect',
() async {
final redirector = ChainRedirector([
RedirectorA(),
]);

expect(
redirector.redirect(
ScreenB(),
MockBuildContext(),
MockGoRouterState(),
),
null,
);
});
expect(
await redirector.redirect(
ScreenB(),
MockBuildContext(),
MockGoRouterState(),
),
null,
);
},
);

test('test restrict screen, chain redirector should redirect', () {
final redirector = ChainRedirector([
RedirectorA(),
]);
test(
'test restrict screen, chain redirector should redirect',
() async {
final redirector = ChainRedirector([
RedirectorA(),
]);

expect(
redirector.redirect(
ScreenA(),
MockBuildContext(),
MockGoRouterState(),
),
'/b',
);
});
expect(
await redirector.redirect(
ScreenA(),
MockBuildContext(),
MockGoRouterState(),
),
'/b',
);
},
);

test('test chain redirector always redirect', () {
final redirector = ChainRedirector([
RedirectorA(),
RedirectorB(),
]);
test(
'test chain redirector always redirect',
() async {
final redirector = ChainRedirector([
RedirectorC(),
RedirectorB(),
]);

expect(
redirector.redirect(
ScreenA(),
MockBuildContext(),
MockGoRouterState(),
),
'/b',
);
expect(
redirector.redirect(
ScreenB(),
MockBuildContext(),
MockGoRouterState(),
),
'/b',
);
expect(
redirector.redirect(
ScreenC(),
MockBuildContext(),
MockGoRouterState(),
),
'/b',
);
});
expect(
await redirector.redirect(
ScreenA(),
MockBuildContext(),
MockGoRouterState(),
),
'/b',
);
expect(
await redirector.redirect(
ScreenB(),
MockBuildContext(),
MockGoRouterState(),
),
'/b',
);
expect(
await redirector.redirect(
ScreenC(),
MockBuildContext(),
MockGoRouterState(),
),
'/b',
);
},
);

test('test screen rediretor should redirect', () {
final redirector = ScreenRedirector();
Expand Down

0 comments on commit 8cca644

Please sign in to comment.