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

Commit 0e879a6

Browse files
authored
feat: export package:mocktail and update docs (#10)
1 parent a500790 commit 0e879a6

4 files changed

Lines changed: 110 additions & 32 deletions

File tree

.github/workflows/main.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ jobs:
3131
with:
3232
path: coverage/lcov.info
3333

34-
- name: Upload coverage to Codecov
35-
uses: codecov/codecov-action@v1
36-
with:
37-
token: ${{ secrets.CODECOV_TOKEN }}
38-
3934
pana:
4035
runs-on: ubuntu-latest
4136

README.md

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,62 @@ Any widget looking up the nearest `Navigator.of(context)` from that point will n
3636

3737
**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.
3838

39+
## Example
40+
3941
```dart
40-
void main() {
41-
late MockNavigator navigator;
42+
import 'package:flutter/material.dart';
43+
import 'package:flutter_test/flutter_test.dart';
44+
import 'package:mockingjay/mockingjay.dart';
4245
43-
setUp(() {
44-
navigator = MockNavigator();
45-
when(() => navigator.push(any())).thenAnswer((_) async => null);
46-
});
46+
class MyHomePage extends StatelessWidget {
47+
const MyHomePage({Key? key}) : super(key: key);
48+
49+
@override
50+
Widget build(BuildContext context) {
51+
return Scaffold(
52+
body: TextButton(
53+
onPressed: () => Navigator.of(context).push(MySettingsPage.route()),
54+
child: const Text('Navigate'),
55+
),
56+
);
57+
}
58+
}
59+
60+
class MySettingsPage extends StatelessWidget {
61+
const MySettingsPage({Key? key}) : super(key: key);
4762
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-
);
62-
63-
await tester.tap(find.byType(MyButton));
64-
verify(
65-
() => navigator.push(any(that: isRoute<void>(named: '/second_screen'))),
66-
).called(1);
67-
},
63+
static Route route() {
64+
return MaterialPageRoute(
65+
builder: (_) => const MySettingsPage(),
66+
settings: const RouteSettings(name: '/settings'),
6867
);
68+
}
69+
70+
@override
71+
Widget build(BuildContext context) {
72+
return const Scaffold();
73+
}
74+
}
75+
76+
void main() {
77+
testWidgets('pushes SettingsPage when TextButton is tapped', (tester) async {
78+
final navigator = MockNavigator();
79+
when(() => navigator.push(any())).thenAnswer((_) async {});
80+
81+
await tester.pumpWidget(
82+
MaterialApp(
83+
home: MockNavigatorProvider(
84+
navigator: navigator,
85+
child: const MyHomePage(),
86+
),
87+
),
88+
);
89+
90+
await tester.tap(find.byType(TextButton));
91+
92+
verify(
93+
() => navigator.push(any(that: isRoute<void>(named: '/settings'))),
94+
).called(1);
6995
});
7096
}
7197
```

lib/mockingjay.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
/// An experimental package that makes it easy to mock, test and verify
1+
/// A package that makes it easy to mock, test and verify
22
/// navigation calls in Flutter.
33
library mockingjay;
44

5+
export 'package:mocktail/mocktail.dart';
6+
57
export 'src/matchers.dart';
68
export 'src/mock_navigator.dart';

test/src/example_test.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:mockingjay/mockingjay.dart';
4+
5+
class MyHomePage extends StatelessWidget {
6+
const MyHomePage({Key? key}) : super(key: key);
7+
8+
@override
9+
Widget build(BuildContext context) {
10+
return Scaffold(
11+
body: TextButton(
12+
onPressed: () => Navigator.of(context).push(MySettingsPage.route()),
13+
child: const Text('Navigate'),
14+
),
15+
);
16+
}
17+
}
18+
19+
class MySettingsPage extends StatelessWidget {
20+
const MySettingsPage({Key? key}) : super(key: key);
21+
22+
static Route route() {
23+
return MaterialPageRoute(
24+
builder: (_) => const MySettingsPage(),
25+
settings: const RouteSettings(name: '/settings'),
26+
);
27+
}
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
return const Scaffold();
32+
}
33+
}
34+
35+
void main() {
36+
testWidgets('pushes SettingsPage when TextButton is tapped', (tester) async {
37+
final navigator = MockNavigator();
38+
when(() => navigator.push(any())).thenAnswer((_) async {});
39+
40+
await tester.pumpWidget(
41+
MaterialApp(
42+
home: MockNavigatorProvider(
43+
navigator: navigator,
44+
child: const MyHomePage(),
45+
),
46+
),
47+
);
48+
49+
await tester.tap(find.byType(TextButton));
50+
51+
verify(
52+
() => navigator.push(any(that: isRoute<void>(named: '/settings'))),
53+
).called(1);
54+
});
55+
}

0 commit comments

Comments
 (0)