Skip to content

Commit

Permalink
Routing Mocks to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Nov 10, 2024
1 parent 0f3e633 commit faae433
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/lib/features/tasks/actions/create_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class _CreateTaskWidgetConsumerState extends ConsumerState<CreateTaskWidget> {
const Text(' > '),
if (taskList == null)
ActerInlineTextButton(
child: Text(lang.selectTaskList),
onPressed: _selectTaskList,
child: Text(lang.selectTaskList),
)
else
InkWell(
Expand Down
25 changes: 24 additions & 1 deletion app/test/features/tasks/tasks_adding_test.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
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<void>(any())).thenAnswer((_) async {});
});

testWidgets('Simple only title', (tester) async {
final mockTaskList = MockTaskList();
final mockTaskDraft = MockTaskDraft();
when(() => mockTaskList.taskBuilder()).thenAnswer((_) => mockTaskDraft);
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),
],
Expand Down Expand Up @@ -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),
],
Expand Down Expand Up @@ -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),
],
Expand Down Expand Up @@ -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),
],
Expand Down Expand Up @@ -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),
],
Expand Down
38 changes: 38 additions & 0 deletions app/test/helpers/mock_go_router.dart
Original file line number Diff line number Diff line change
@@ -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<T?> pushNamed<T extends Object?>(
String? name, {
Map<String, String>? pathParameters = const <String, String>{},
Map<String, dynamic>? queryParameters = const <String, dynamic>{},
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,
);
}
11 changes: 11 additions & 0 deletions app/test/helpers/test_util.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -44,8 +47,16 @@ extension PumpUntilFound on WidgetTester {
extension ActerProviderTesting on WidgetTester {
Future<void> pumpProviderWidget({
List<Override>? 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 ?? [],
Expand Down

0 comments on commit faae433

Please sign in to comment.