Skip to content
This repository was archived by the owner on Apr 14, 2026. It is now read-only.

Commit 7880cf2

Browse files
committed
feat: initial project
0 parents  commit 7880cf2

9 files changed

Lines changed: 272 additions & 0 deletions

File tree

.gitignore

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.flutter-plugins-dependencies
28+
.packages
29+
.pub-cache/
30+
.pub/
31+
build/
32+
pubspec.lock
33+
34+
# Android related
35+
**/android/**/gradle-wrapper.jar
36+
**/android/.gradle
37+
**/android/captures/
38+
**/android/gradlew
39+
**/android/gradlew.bat
40+
**/android/local.properties
41+
**/android/**/GeneratedPluginRegistrant.java
42+
43+
# iOS/XCode related
44+
**/ios/**/*.mode1v3
45+
**/ios/**/*.mode2v3
46+
**/ios/**/*.moved-aside
47+
**/ios/**/*.pbxuser
48+
**/ios/**/*.perspectivev3
49+
**/ios/**/*sync/
50+
**/ios/**/.sconsign.dblite
51+
**/ios/**/.tags*
52+
**/ios/**/.vagrant/
53+
**/ios/**/DerivedData/
54+
**/ios/**/Icon?
55+
**/ios/**/Pods/
56+
**/ios/**/.symlinks/
57+
**/ios/**/profile
58+
**/ios/**/xcuserdata
59+
**/ios/.generated/
60+
**/ios/Flutter/App.framework
61+
**/ios/Flutter/Flutter.framework
62+
**/ios/Flutter/Flutter.podspec
63+
**/ios/Flutter/Generated.xcconfig
64+
**/ios/Flutter/app.flx
65+
**/ios/Flutter/app.zip
66+
**/ios/Flutter/flutter_assets/
67+
**/ios/Flutter/flutter_export_environment.sh
68+
**/ios/ServiceDefinitions.json
69+
**/ios/Runner/GeneratedPluginRegistrant.*
70+
71+
# Test related
72+
coverage
73+
74+
# Exceptions to above rules.
75+
!**/ios/**/default.mode1v3
76+
!**/ios/**/default.mode2v3
77+
!**/ios/**/default.pbxuser
78+
!**/ios/**/default.perspectivev3

.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: adc687823a831bbebe28bdccfac1a628ca621513
8+
channel: stable
9+
10+
project_type: package

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [0.0.1] - TODO: Add release date.
2+
3+
* TODO: Describe initial release.

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add your license here.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# mock_navigator
2+
3+
An experimental package that attempts to make it easy to mock Flutter's navigator routes.
4+
5+
## Usage
6+
7+
TODO
8+
9+
## Getting Started
10+
11+
This project is a starting point for a Dart
12+
[package](https://flutter.dev/developing-packages/),
13+
a library module containing code that can be shared easily across
14+
multiple Flutter or Dart projects.
15+
16+
For help getting started with Flutter, view our
17+
[online documentation](https://flutter.dev/docs), which offers tutorials,
18+
samples, guidance on mobile development, and a full API reference.

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:very_good_analysis/analysis_options.yaml

lib/mock_navigator.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/// An experimental package that attempts to make it easy to mock Flutter's
2+
/// navigator routes.
3+
library mock_navigator;
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:mocktail/mocktail.dart' as mocktail;
7+
import 'package:mockito/mockito.dart' as mockito;
8+
9+
/// A template fake route, useful for registering fallbacks when using the
10+
/// `mocktail` package.
11+
class FakeRoute<T> extends mocktail.Fake implements Route<T> {}
12+
13+
/// {@template mock_navigator_provider}
14+
/// The widget that provides an instance of a [MockNavigator].
15+
/// {@endtemplate}
16+
class MockNavigatorProvider extends Navigator {
17+
/// {@macro mock_navigator_provider}
18+
const MockNavigatorProvider({
19+
Key? key,
20+
required this.child,
21+
required this.navigator,
22+
}) : super(key: key);
23+
24+
/// The [MockNavigator] used to mock navigation calls.
25+
final MockNavigator navigator;
26+
27+
/// The [Widget] to render.
28+
final Widget child;
29+
30+
@override
31+
NavigatorState createState() {
32+
return _MockNavigatorState(navigator: navigator)..child = child;
33+
}
34+
35+
@override
36+
RouteFactory? get onGenerateRoute {
37+
return (_) => MaterialPageRoute(builder: (_) => child);
38+
}
39+
}
40+
41+
/// The navigator of which the behavior can be defined through using either the
42+
/// [MocktailNavigator] or [MockitoNavigator].a
43+
abstract class MockNavigator implements NavigatorState {}
44+
45+
/// The navigator of which the behavior can be defined through mocking using the
46+
/// `mocktail` package.
47+
class MocktailNavigator extends mocktail.Mock
48+
with _DiagnosticStringMixin
49+
implements MockNavigator {}
50+
51+
/// The navigator of which the behavior can be defined through mocking using the
52+
/// `mockito` package.
53+
class MockitoNavigator extends mocktail.Mock
54+
with _DiagnosticStringMixin
55+
implements MockNavigator {}
56+
57+
mixin _DiagnosticStringMixin on Object {
58+
@override
59+
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
60+
return super.toString();
61+
}
62+
}
63+
64+
class _MockNavigatorState extends NavigatorState {
65+
_MockNavigatorState({required this.navigator});
66+
67+
MockNavigator navigator;
68+
Widget? child;
69+
70+
@override
71+
Future<T?> push<T extends Object?>(Route<T> route) => navigator.push(route);
72+
73+
@override
74+
Widget build(BuildContext context) => child!;
75+
}

pubspec.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: mock_navigator
2+
description: An experimental package that attempts to make it easy to mock
3+
Flutter's navigator routes.
4+
version: 0.0.1
5+
homepage: https://github.com/jeroen-meijer/mock_navigator
6+
7+
environment:
8+
sdk: ">=2.12.0 <3.0.0"
9+
flutter: ">=1.17.0"
10+
11+
dependencies:
12+
flutter:
13+
sdk: flutter
14+
15+
dev_dependencies:
16+
flutter_test:
17+
sdk: flutter
18+
coverage: 1.0.2
19+
mockito: 5.0.5
20+
mocktail: 0.1.1
21+
very_good_analysis: 2.0.3

test/mock_navigator_test.dart

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
import 'package:mock_navigator/mock_navigator.dart';
5+
import 'package:mocktail/mocktail.dart';
6+
7+
extension on WidgetTester {
8+
Future<void> pumpTest(Widget widget) async {
9+
await pumpWidget(
10+
MaterialApp(
11+
title: 'Mock Navigator Test',
12+
home: Scaffold(
13+
body: widget,
14+
),
15+
),
16+
);
17+
}
18+
}
19+
20+
void main() {
21+
group('MockNavigator', () {
22+
late MockNavigator navigator;
23+
24+
setUpAll(() {
25+
registerFallbackValue<Route<Object?>>(FakeRoute<Object?>());
26+
});
27+
28+
setUp(() {
29+
navigator = MocktailNavigator();
30+
});
31+
32+
testWidgets('mocks .push calls', (tester) async {
33+
var pushCalled = 0;
34+
35+
when(() => navigator.push(any())).thenAnswer((_) async {
36+
pushCalled++;
37+
return null;
38+
});
39+
40+
await tester.pumpTest(
41+
MockNavigatorProvider(
42+
navigator: navigator,
43+
child: Builder(
44+
builder: (context) {
45+
return TextButton(
46+
onPressed: () {
47+
Navigator.of(context).push(
48+
MaterialPageRoute(
49+
builder: (_) => const Text('I should not build.'),
50+
),
51+
);
52+
},
53+
child: const Text('Trigger'),
54+
);
55+
},
56+
),
57+
),
58+
);
59+
60+
await tester.tap(find.byType(TextButton));
61+
62+
expect(pushCalled, equals(1));
63+
});
64+
});
65+
}

0 commit comments

Comments
 (0)