Skip to content

Commit 8623c5c

Browse files
TreganBas Rops
andauthored
fix: Updates for Flutter 3.16.0/Dart 3.2 (#65)
* MockNavigator.canPop() is now stubbed automatically - Default `false` * Mock OverlayState during hackOverlays to control `mounted` * Fixed failing test * Set flutter to `>=3.16.0` in pubspec.yaml * Removed default stubbing of `canPop` * Fixed failing example tests --------- Co-authored-by: Bas Rops <[email protected]>
1 parent dcfd1bb commit 8623c5c

File tree

8 files changed

+21
-2
lines changed

8 files changed

+21
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class MySettingsPage extends StatelessWidget {
7777
void main() {
7878
testWidgets('pushes SettingsPage when TextButton is tapped', (tester) async {
7979
final navigator = MockNavigator();
80+
when(navigator.canPop).thenReturn(true);
8081
when(() => navigator.push<void>(any())).thenAnswer((_) async {});
8182
8283
await tester.pumpWidget(

example/test/ui/home_screen_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void main() {
2424

2525
setUp(() {
2626
navigator = MockNavigator();
27+
when(() => navigator.canPop()).thenReturn(true);
2728
when(() => navigator.push<String?>(any())).thenAnswer((_) async => null);
2829
when(
2930
() => navigator.push<QuizOption>(any()),

example/test/ui/pincode_screen_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void main() {
1414

1515
setUp(() {
1616
navigator = MockNavigator();
17+
when(() => navigator.canPop()).thenReturn(true);
1718
});
1819

1920
testWidgets('.route renders PincodeScreen', (tester) async {

example/test/ui/quiz_dialog_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void main() {
1414

1515
setUp(() {
1616
navigator = MockNavigator();
17+
when(() => navigator.canPop()).thenReturn(true);
1718
});
1819

1920
testWidgets('.show opens dialog', (tester) async {

lib/src/mock_navigator.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class _MockMaterialPageRoute extends MaterialPageRoute<void> {
66

77
void hackOverlays() {
88
for (var i = 0; i < overlayEntries.length; i++) {
9-
final state = OverlayState();
9+
// Entry can only be inserted when the state is mounted
10+
final state = _MockOverlayState().._mounted = true;
1011
final entry = OverlayEntry(builder: (_) => const SizedBox());
1112
try {
1213
// We need to call insert since that is the only way to populate the
@@ -17,11 +18,21 @@ class _MockMaterialPageRoute extends MaterialPageRoute<void> {
1718
// so we just ignore the error and the hack will do its job.
1819
state.insert(entry);
1920
} catch (_) {}
21+
// Set mounted back to false to make sure the state doesn't get
22+
// marked as dirty during OverlayEntry.remove().
23+
state._mounted = false;
2024
overlayEntries[i] = entry;
2125
}
2226
}
2327
}
2428

29+
class _MockOverlayState extends OverlayState {
30+
late bool _mounted;
31+
32+
@override
33+
bool get mounted => _mounted;
34+
}
35+
2536
class _FakeRoute<T> extends Fake implements Route<T> {}
2637

2738
/// {@template mock_navigator_provider}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ homepage: https://github.com/VeryGoodOpenSource/mockingjay
55

66
environment:
77
sdk: ">=2.18.0 <4.0.0"
8-
flutter: ">=3.7.3"
8+
flutter: ">=3.16.0"
99

1010
dependencies:
1111
flutter:

test/src/example_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class MySettingsPage extends StatelessWidget {
3535
void main() {
3636
testWidgets('pushes SettingsPage when TextButton is tapped', (tester) async {
3737
final navigator = MockNavigator();
38+
when(navigator.canPop).thenReturn(true);
3839
when(() => navigator.push<void>(any())).thenAnswer((_) async {});
3940

4041
await tester.pumpWidget(

test/src/mock_navigator_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void main() {
4545

4646
setUp(() {
4747
navigator = MockNavigator();
48+
when(() => navigator.canPop()).thenReturn(true);
4849
});
4950

5051
test('toString returns normally', () {
@@ -198,6 +199,8 @@ void main() {
198199
),
199200
);
200201

202+
// Called by NavigatorState.didChangeDependencies initially
203+
verify(() => navigator.canPop()).called(1);
201204
await tester.tap(find.byType(TextButton));
202205
verify(() => navigator.canPop()).called(1);
203206
});

0 commit comments

Comments
 (0)