Skip to content

Commit

Permalink
fix: Updates for Flutter 3.16.0/Dart 3.2 (#65)
Browse files Browse the repository at this point in the history
* 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 <bas.rops@knowingo.com>
  • Loading branch information
Tregan and Bas Rops committed Nov 22, 2023
1 parent dcfd1bb commit 8623c5c
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class MySettingsPage extends StatelessWidget {
void main() {
testWidgets('pushes SettingsPage when TextButton is tapped', (tester) async {
final navigator = MockNavigator();
when(navigator.canPop).thenReturn(true);
when(() => navigator.push<void>(any())).thenAnswer((_) async {});
await tester.pumpWidget(
Expand Down
1 change: 1 addition & 0 deletions example/test/ui/home_screen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void main() {

setUp(() {
navigator = MockNavigator();
when(() => navigator.canPop()).thenReturn(true);
when(() => navigator.push<String?>(any())).thenAnswer((_) async => null);
when(
() => navigator.push<QuizOption>(any()),
Expand Down
1 change: 1 addition & 0 deletions example/test/ui/pincode_screen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void main() {

setUp(() {
navigator = MockNavigator();
when(() => navigator.canPop()).thenReturn(true);
});

testWidgets('.route renders PincodeScreen', (tester) async {
Expand Down
1 change: 1 addition & 0 deletions example/test/ui/quiz_dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void main() {

setUp(() {
navigator = MockNavigator();
when(() => navigator.canPop()).thenReturn(true);
});

testWidgets('.show opens dialog', (tester) async {
Expand Down
13 changes: 12 additions & 1 deletion lib/src/mock_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class _MockMaterialPageRoute extends MaterialPageRoute<void> {

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

class _MockOverlayState extends OverlayState {
late bool _mounted;

@override
bool get mounted => _mounted;
}

class _FakeRoute<T> extends Fake implements Route<T> {}

/// {@template mock_navigator_provider}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://github.com/VeryGoodOpenSource/mockingjay

environment:
sdk: ">=2.18.0 <4.0.0"
flutter: ">=3.7.3"
flutter: ">=3.16.0"

dependencies:
flutter:
Expand Down
1 change: 1 addition & 0 deletions test/src/example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MySettingsPage extends StatelessWidget {
void main() {
testWidgets('pushes SettingsPage when TextButton is tapped', (tester) async {
final navigator = MockNavigator();
when(navigator.canPop).thenReturn(true);
when(() => navigator.push<void>(any())).thenAnswer((_) async {});

await tester.pumpWidget(
Expand Down
3 changes: 3 additions & 0 deletions test/src/mock_navigator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void main() {

setUp(() {
navigator = MockNavigator();
when(() => navigator.canPop()).thenReturn(true);
});

test('toString returns normally', () {
Expand Down Expand Up @@ -198,6 +199,8 @@ void main() {
),
);

// Called by NavigatorState.didChangeDependencies initially
verify(() => navigator.canPop()).called(1);
await tester.tap(find.byType(TextButton));
verify(() => navigator.canPop()).called(1);
});
Expand Down

0 comments on commit 8623c5c

Please sign in to comment.