|
1 | | -# mock_navigator |
| 1 | +# 🕊 mockingjay |
2 | 2 |
|
3 | | -An experimental package that attempts to make it easy to mock Flutter's navigator routes. |
| 3 | +[](https://verygood.ventures) |
| 4 | + |
| 5 | +Developed with 💙 by [Very Good Ventures](https://verygood.ventures) 🦄 |
| 6 | + |
| 7 | +[](https://github.com/jeroen-meijer/mockingjay/actions) |
| 8 | +[](https://pub.dartlang.org/packages/mockingjay) |
| 9 | +[](https://opensource.org/licenses/MIT) |
| 10 | +[![style: very good analysis][badge]][badge_link] |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +A package that makes it easy to mock, test and verify navigation calls in Flutter. It works in tandem with [`mocktail`][mocktail], allowing you to mock a navigator the same way you would any other object, making it easier to test navigation behavior independently from the UI it's supposed to render. |
4 | 15 |
|
5 | 16 | ## Usage |
6 | 17 |
|
7 | | -TODO |
| 18 | +To use the package in your tests, add it to your dev dependencies in your `pubspec.yaml`: |
| 19 | + |
| 20 | +```yaml |
| 21 | +dev_dependencies: |
| 22 | + mockingjay: ^0.1.0 |
| 23 | +``` |
| 24 | +
|
| 25 | +Then, in your tests, create a `MockNavigator` class like so: |
| 26 | + |
| 27 | +```dart |
| 28 | +import 'package:mockingjay/mockingjay.dart'; |
| 29 | +
|
| 30 | +final navigator = MockNavigator(); |
| 31 | +``` |
| 32 | + |
| 33 | +Now you can create a new `MockNavigator` and pass it to a `MockNavigatorProvider`. |
| 34 | + |
| 35 | +Any widget looking up the nearest `Navigator.of(context)` from that point will now receive the `MockNavigator`, allowing you to mock (using `when`) and `verify` any navigation calls. Use the included matchers to more easily match specific route names and types. |
| 36 | + |
| 37 | +**Note**: make sure the `MockNavigatorProvider` is constructed **below** the `MaterialApp`. Otherwise, any `Navigator.of(context)` call will return a real `NavigatorState` instead of the mock. |
| 38 | + |
| 39 | +```dart |
| 40 | +void main() { |
| 41 | + late MockNavigator navigator; |
| 42 | +
|
| 43 | + setUp(() { |
| 44 | + navigator = MockNavigator(); |
| 45 | + when(() => navigator.push(any())).thenAnswer((_) async => null); |
| 46 | + }); |
8 | 47 |
|
9 | | -## Getting Started |
| 48 | + group('MyButton', () { |
| 49 | + testWidgets( |
| 50 | + 'pushes a new screen when button is pressed', |
| 51 | + (tester) async { |
| 52 | + await tester.pumpWidget( |
| 53 | + MaterialApp( |
| 54 | + home: MockNavigatorProvider( |
| 55 | + navigator: navigator, |
| 56 | + child: Scaffold( |
| 57 | + body: MyButton(), |
| 58 | + ), |
| 59 | + ), |
| 60 | + ), |
| 61 | + ); |
10 | 62 |
|
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. |
| 63 | + await tester.tap(find.byType(MyButton)); |
| 64 | + verify( |
| 65 | + () => navigator.push(any(that: isRoute<void>(named: '/second_screen'))), |
| 66 | + ).called(1); |
| 67 | + }, |
| 68 | + ); |
| 69 | + }); |
| 70 | +} |
| 71 | +``` |
15 | 72 |
|
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. |
| 73 | +[very good analysis]: https://github.com/VeryGoodOpenSource/very_good_analysis |
| 74 | +[badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg |
| 75 | +[badge_link]: https://pub.dev/packages/mockingjay |
| 76 | +[mocktail]: https://pub.dev/packages/mocktail |
0 commit comments