Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonataslaw committed Aug 15, 2024
1 parent f13fbaf commit 8728746
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 24 deletions.
8 changes: 6 additions & 2 deletions lib/get_navigation/src/routes/get_router_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
}
var iterator = config;
for (var item in middlewares) {
print(currentConfiguration?.route?.name);
var redirectRes = await item.redirectDelegate(iterator);
if (redirectRes == null) return null;

if (redirectRes == null) {
config.route?.completer?.complete();
return null;
}
if (config != redirectRes) {
config.route?.completer?.complete();
Get.log('Redirect to ${redirectRes.pageSettings?.name}');
}

Expand Down
9 changes: 9 additions & 0 deletions test/navigation/get_main_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,12 @@ class ThirdScreen extends StatelessWidget {
return Container();
}
}

class FourthScreen extends StatelessWidget {
const FourthScreen({super.key});

@override
Widget build(BuildContext context) {
return Container();
}
}
118 changes: 96 additions & 22 deletions test/navigation/middleware_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class RedirectMiddleware extends GetMiddleware {
}
}

class Redirect2Middleware extends GetMiddleware {
@override
Future<RouteDecoder?> redirectDelegate(RouteDecoder route) async {
return RouteDecoder.fromRoute('/first');
}
}

class RedirectMiddlewareNull extends GetMiddleware {
@override
Future<RouteDecoder?> redirectDelegate(RouteDecoder route) async {
Expand All @@ -25,7 +32,12 @@ class RedirectBypassMiddleware extends GetMiddleware {
}

void main() {
tearDown(() {
Get.reset();
});

testWidgets("Middleware should redirect to second screen", (tester) async {
// Test setup
await tester.pumpWidget(
GetMaterialApp(
initialRoute: '/',
Expand All @@ -34,27 +46,26 @@ void main() {
GetPage(
name: '/first',
page: () => const FirstScreen(),
middlewares: [
RedirectMiddleware(),
],
middlewares: [RedirectMiddleware()],
),
GetPage(name: '/second', page: () => const SecondScreen()),
GetPage(name: '/third', page: () => const ThirdScreen()),
],
),
);

// Act
Get.toNamed('/first');

await tester.pumpAndSettle();

// Ensure that we are redirected to the second screen
// Assert
expect(find.byType(SecondScreen), findsOneWidget);
// Ensure that we are not seeing the first screen
expect(find.byType(FirstScreen), findsNothing);
expect(Get.currentRoute, '/second');
});

testWidgets("Middleware should stop navigation", (tester) async {
// Test setup
await tester.pumpWidget(
GetMaterialApp(
initialRoute: '/',
Expand All @@ -63,28 +74,27 @@ void main() {
GetPage(
name: '/first',
page: () => const FirstScreen(),
middlewares: [
RedirectMiddlewareNull(),
],
middlewares: [RedirectMiddlewareNull()],
),
GetPage(name: '/second', page: () => const SecondScreen()),
GetPage(name: '/third', page: () => const ThirdScreen()),
],
),
);

await tester.pump();

// Act
await tester.pumpAndSettle();
Get.toNamed('/first');

await tester.pumpAndSettle();

// Ensure that we remain on the initial route and do not navigate to the first screen
// Assert
expect(find.byType(Home), findsOneWidget);
expect(find.byType(FirstScreen), findsNothing);
expect(Get.currentRoute, '/');
});

testWidgets("Middleware should be bypassed", (tester) async {
// Test setup
await tester.pumpWidget(
GetMaterialApp(
initialRoute: '/',
Expand All @@ -93,27 +103,91 @@ void main() {
GetPage(
name: '/first',
page: () => const FirstScreen(),
middlewares: [
RedirectBypassMiddleware(),
],
middlewares: [RedirectBypassMiddleware()],
),
GetPage(name: '/second', page: () => const SecondScreen()),
GetPage(name: '/third', page: () => const ThirdScreen()),
],
),
);

await tester.pump();

// Act
await tester.pumpAndSettle();
Get.toNamed('/first');

await tester.pumpAndSettle();

// Ensure that the navigation is not redirected and we land on the first screen
// Assert
expect(find.byType(FirstScreen), findsOneWidget);
// Ensure that we do not see the second screen
expect(find.byType(SecondScreen), findsNothing);
// Ensure that we do not see the home screen
expect(find.byType(Home), findsNothing);
expect(Get.currentRoute, '/first');
});

testWidgets("Middleware should redirect twice", (tester) async {
// Test setup
await tester.pumpWidget(
GetMaterialApp(
initialRoute: '/',
getPages: [
GetPage(name: '/', page: () => const Home()),
GetPage(
name: '/first',
page: () => const FirstScreen(),
middlewares: [RedirectMiddleware()],
),
GetPage(name: '/second', page: () => const SecondScreen()),
GetPage(name: '/third', page: () => const ThirdScreen()),
GetPage(
name: '/fourth',
page: () => const FourthScreen(),
middlewares: [Redirect2Middleware()],
),
],
),
);

// Act
Get.toNamed('/fourth');
await tester.pumpAndSettle();

// Assert
expect(find.byType(SecondScreen), findsOneWidget);
expect(find.byType(FirstScreen), findsNothing);
expect(Get.currentRoute, '/second');
});

testWidgets("Navigation history should be correct after redirects",
(tester) async {
// Test setup
await tester.pumpWidget(
GetMaterialApp(
initialRoute: '/',
getPages: [
GetPage(name: '/', page: () => const Home()),
GetPage(
name: '/first',
page: () => const FirstScreen(),
middlewares: [RedirectMiddleware()],
),
GetPage(name: '/second', page: () => const SecondScreen()),
],
),
);

// Act
Get.toNamed('/first');
await tester.pumpAndSettle();

// Assert
expect(Get.currentRoute, '/second');
expect(Get.previousRoute, '/');

// Act: go back
Get.back();
await tester.pumpAndSettle();

// Assert
expect(find.byType(Home), findsOneWidget);
expect(Get.currentRoute, '/');
});
}

0 comments on commit 8728746

Please sign in to comment.