Skip to content

Commit 4dad60d

Browse files
committed
Test that correct pages are displayed for navRailIndexStateProvider value
1 parent 6a886df commit 4dad60d

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,14 @@ packages:
729729
url: "https://pub.dev"
730730
source: hosted
731731
version: "3.0.0"
732+
mockito:
733+
dependency: "direct main"
734+
description:
735+
name: mockito
736+
sha256: "6841eed20a7befac0ce07df8116c8b8233ed1f4486a7647c7fc5a02ae6163917"
737+
url: "https://pub.dev"
738+
source: hosted
739+
version: "5.4.4"
732740
mpv_dart:
733741
dependency: transitive
734742
description:

pubspec.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
name: apidash
22
description: API Dash is a beautiful open-source cross-platform API Client built using Flutter which can help you easily create & customize your API requests, visually inspect responses and generate Dart code on the go.
3-
publish_to: "none"
3+
publish_to: 'none'
44
version: 0.3.0+3
55

66
environment:
7-
sdk: ">=3.0.0 <4.0.0"
8-
flutter: ">=3.7.2"
7+
sdk: '>=3.0.0 <4.0.0'
8+
flutter: '>=3.7.2'
99

1010
dependencies:
1111
flutter:
@@ -55,6 +55,7 @@ dependencies:
5555
dart_style: ^2.3.4
5656
json_text_field: ^1.1.0
5757
csv: ^5.1.1
58+
mockito: ^5.4.4
5859

5960
dev_dependencies:
6061
flutter_test:
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import 'dart:io';
2+
3+
import 'package:apidash/providers/providers.dart';
4+
import 'package:apidash/screens/dashboard.dart';
5+
import 'package:apidash/screens/home_page/home_page.dart';
6+
import 'package:apidash/screens/intro_page.dart';
7+
import 'package:apidash/screens/settings_page.dart';
8+
import 'package:apidash/services/hive_services.dart';
9+
import 'package:flutter/material.dart';
10+
import 'package:flutter/services.dart';
11+
import 'package:flutter_riverpod/flutter_riverpod.dart';
12+
import 'package:flutter_test/flutter_test.dart';
13+
import 'package:mockito/mockito.dart';
14+
15+
class MockPathProviderPlatform extends Mock implements MethodChannel {}
16+
17+
void main() {
18+
TestWidgetsFlutterBinding.ensureInitialized();
19+
20+
setUp(() async {
21+
const MethodChannel channel =
22+
MethodChannel('plugins.flutter.io/path_provider');
23+
final MockPathProviderPlatform mock = MockPathProviderPlatform();
24+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
25+
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
26+
if (methodCall.method == 'getApplicationDocumentsDirectory') {
27+
// Create a mock app doc directory for testing
28+
Directory tempDir =
29+
await Directory.systemTemp.createTemp('mock_app_doc_dir');
30+
return tempDir.path; // Return the path to the mock directory
31+
}
32+
return null;
33+
});
34+
await openBoxes();
35+
});
36+
37+
group('Testing navRailIndexStateProvider', () {
38+
testWidgets('Dashboard should display correct initial page',
39+
(tester) async {
40+
await tester.pumpWidget(
41+
const ProviderScope(
42+
child: MaterialApp(
43+
home: Dashboard(),
44+
),
45+
),
46+
);
47+
48+
// Verify that the HomePage is displayed initially
49+
expect(find.byType(HomePage), findsOneWidget);
50+
expect(find.byType(IntroPage), findsNothing);
51+
expect(find.byType(SettingsPage), findsNothing);
52+
});
53+
54+
testWidgets(
55+
"Dashboard should display IntroPage when navRailIndexStateProvider is 1",
56+
(WidgetTester tester) async {
57+
await tester.pumpWidget(
58+
ProviderScope(
59+
overrides: [
60+
navRailIndexStateProvider.overrideWith((ref) => 1),
61+
],
62+
child: const MaterialApp(
63+
home: Dashboard(),
64+
),
65+
),
66+
);
67+
68+
// Verify that the IntroPage is displayed
69+
expect(find.byType(IntroPage), findsOneWidget);
70+
expect(find.byType(HomePage), findsNothing);
71+
expect(find.byType(SettingsPage), findsNothing);
72+
});
73+
testWidgets(
74+
"Dashboard should display SettingsPage when navRailIndexStateProvider is 2",
75+
(WidgetTester tester) async {
76+
await tester.pumpWidget(
77+
ProviderScope(
78+
overrides: [
79+
navRailIndexStateProvider.overrideWith((ref) => 2),
80+
],
81+
child: const MaterialApp(
82+
home: Dashboard(),
83+
),
84+
),
85+
);
86+
87+
// Verify that the SettingsPage is displayed
88+
expect(find.byType(SettingsPage), findsOneWidget);
89+
expect(find.byType(IntroPage), findsNothing);
90+
expect(find.byType(HomePage), findsNothing);
91+
});
92+
});
93+
}

0 commit comments

Comments
 (0)