|
| 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