From faae433c194a33c0b6d9909b7008b8fd132ac8a9 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Sun, 10 Nov 2024 11:14:56 +0000 Subject: [PATCH] Routing Mocks to fix tests --- .../features/tasks/actions/create_task.dart | 2 +- .../features/tasks/tasks_adding_test.dart | 25 +++++++++++- app/test/helpers/mock_go_router.dart | 38 +++++++++++++++++++ app/test/helpers/test_util.dart | 11 ++++++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 app/test/helpers/mock_go_router.dart diff --git a/app/lib/features/tasks/actions/create_task.dart b/app/lib/features/tasks/actions/create_task.dart index c63a1eafb5b9..a41eb7a89c12 100644 --- a/app/lib/features/tasks/actions/create_task.dart +++ b/app/lib/features/tasks/actions/create_task.dart @@ -144,8 +144,8 @@ class _CreateTaskWidgetConsumerState extends ConsumerState { const Text(' > '), if (taskList == null) ActerInlineTextButton( - child: Text(lang.selectTaskList), onPressed: _selectTaskList, + child: Text(lang.selectTaskList), ) else InkWell( diff --git a/app/test/features/tasks/tasks_adding_test.dart b/app/test/features/tasks/tasks_adding_test.dart index e28882c34892..7f99b5d3239e 100644 --- a/app/test/features/tasks/tasks_adding_test.dart +++ b/app/test/features/tasks/tasks_adding_test.dart @@ -1,14 +1,26 @@ import 'package:acter/common/providers/space_providers.dart'; import 'package:acter/features/tasks/actions/create_task.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:mocktail/mocktail.dart'; +import 'package:mockingjay/mockingjay.dart'; import '../../helpers/mock_a3sdk.dart'; +import '../../helpers/mock_go_router.dart'; import '../../helpers/mock_tasks_providers.dart'; import '../../helpers/test_util.dart'; void main() { group('Create Task Widget on TaskList', () { + late MockGoRouter mockedGoRouter; + late MockNavigator navigator; + + setUp(() { + mockedGoRouter = MockGoRouter(); + navigator = MockNavigator(); + when(navigator.canPop).thenReturn(true); + when(() => navigator.pop(any())).thenAnswer((_) async {}); + when(() => navigator.push(any())).thenAnswer((_) async {}); + }); + testWidgets('Simple only title', (tester) async { final mockTaskList = MockTaskList(); final mockTaskDraft = MockTaskDraft(); @@ -16,7 +28,10 @@ void main() { when(() => mockTaskDraft.title('My new Task')).thenAnswer((_) => true); when(() => mockTaskDraft.send()) .thenAnswer((_) async => MockEventId(id: 'test')); + await tester.pumpProviderWidget( + navigatorOverride: navigator, + goRouter: mockedGoRouter, overrides: [ selectedSpaceDetailsProvider.overrideWith((_) => null), ], @@ -55,6 +70,8 @@ void main() { when(() => mockTaskDraft.send()) .thenAnswer((_) async => MockEventId(id: 'test')); await tester.pumpProviderWidget( + navigatorOverride: navigator, + goRouter: mockedGoRouter, overrides: [ selectedSpaceDetailsProvider.overrideWith((_) => null), ], @@ -114,6 +131,8 @@ void main() { when(() => mockTaskDraft.send()) .thenAnswer((_) async => MockEventId(id: 'test')); await tester.pumpProviderWidget( + navigatorOverride: navigator, + goRouter: mockedGoRouter, overrides: [ selectedSpaceDetailsProvider.overrideWith((_) => null), ], @@ -180,6 +199,8 @@ void main() { when(() => mockTaskDraft.send()) .thenAnswer((_) async => MockEventId(id: 'test')); await tester.pumpProviderWidget( + navigatorOverride: navigator, + goRouter: mockedGoRouter, overrides: [ selectedSpaceDetailsProvider.overrideWith((_) => null), ], @@ -251,6 +272,8 @@ void main() { when(() => mockTaskDraft.send()) .thenAnswer((_) async => MockEventId(id: 'test')); await tester.pumpProviderWidget( + navigatorOverride: navigator, + goRouter: mockedGoRouter, overrides: [ selectedSpaceDetailsProvider.overrideWith((_) => null), ], diff --git a/app/test/helpers/mock_go_router.dart b/app/test/helpers/mock_go_router.dart new file mode 100644 index 000000000000..981e5eeb36c0 --- /dev/null +++ b/app/test/helpers/mock_go_router.dart @@ -0,0 +1,38 @@ +import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; + +import 'package:mocktail/mocktail.dart'; + +class MockGoRouter extends Mock implements GoRouter { + @override + Future pushNamed( + String? name, { + Map? pathParameters = const {}, + Map? queryParameters = const {}, + Object? extra, + }) { + super.noSuchMethod(Invocation.method( + #pushNamed, [name, extra, pathParameters, queryParameters],),); + return Future.value(null); + } +} + +class MockGoRouterProvider extends StatelessWidget { + const MockGoRouterProvider({ + required this.goRouter, + required this.child, + super.key, + }); + + /// The mock navigator used to mock navigation calls. + final GoRouter goRouter; + + /// The child [Widget] to render. + final Widget child; + + @override + Widget build(BuildContext context) => InheritedGoRouter( + goRouter: goRouter, + child: child, + ); +} diff --git a/app/test/helpers/test_util.dart b/app/test/helpers/test_util.dart index 6424777977c1..0b2deda590bf 100644 --- a/app/test/helpers/test_util.dart +++ b/app/test/helpers/test_util.dart @@ -1,7 +1,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:go_router/go_router.dart'; +import 'package:mockingjay/mockingjay.dart'; +import 'mock_go_router.dart'; import 'test_wrapper_widget.dart'; extension PumpUntilFound on WidgetTester { @@ -44,8 +47,16 @@ extension PumpUntilFound on WidgetTester { extension ActerProviderTesting on WidgetTester { Future pumpProviderWidget({ List? overrides, + MockNavigator? navigatorOverride, + GoRouter? goRouter, required Widget child, }) async { + if (goRouter != null) { + child = MockGoRouterProvider(goRouter: goRouter, child: child); + } + if (navigatorOverride != null) { + child = MockNavigatorProvider(navigator: navigatorOverride, child: child); + } await pumpWidget( ProviderScope( overrides: overrides ?? [],