From 2f710174b4e6381e9f679a3a9ac0e56ae5b082a6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 1 Mar 2026 11:20:03 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20add=20widget=20tests=20for=20Mai?= =?UTF-8?q?nLayout=20navigation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added `screens` parameter to `MainLayout` to allow injecting simple mock screens during testing. - Created `test/screens/main_layout_test.dart` to verify that tapping bottom navigation items updates the current screen index correctly. - Added `mocktail` to `dev_dependencies` in `pubspec.yaml` for potential future mocking needs. Co-authored-by: hexamh <264230460+hexamh@users.noreply.github.com> --- lib/screens/main_layout.dart | 24 ++++++---- pubspec.lock | 8 ++++ pubspec.yaml | 1 + test/screens/main_layout_test.dart | 73 ++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 test/screens/main_layout_test.dart diff --git a/lib/screens/main_layout.dart b/lib/screens/main_layout.dart index 6564d77..3c57169 100644 --- a/lib/screens/main_layout.dart +++ b/lib/screens/main_layout.dart @@ -7,7 +7,9 @@ import 'network_screen.dart'; import 'battery_screen.dart'; class MainLayout extends StatefulWidget { - const MainLayout({super.key}); + final List? screens; // Allow injecting mock screens for testing + + const MainLayout({super.key, this.screens}); @override State createState() => _MainLayoutState(); @@ -16,13 +18,19 @@ class MainLayout extends StatefulWidget { class _MainLayoutState extends State { int _currentIndex = 0; - final List _screens = const [ - DashboardScreen(), - HardwareScreen(), - SystemScreen(), - BatteryScreen(), - NetworkScreen(), // We'll put Network here based on the icons design - ]; + late final List _screens; + + @override + void initState() { + super.initState(); + _screens = widget.screens ?? const [ + DashboardScreen(), + HardwareScreen(), + SystemScreen(), + BatteryScreen(), + NetworkScreen(), // We'll put Network here based on the icons design + ]; + } @override Widget build(BuildContext context) { diff --git a/pubspec.lock b/pubspec.lock index dd53855..b01daa9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -272,6 +272,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.17.0" + mocktail: + dependency: "direct dev" + description: + name: mocktail + sha256: "890df3f9688106f25755f26b1c60589a92b3ab91a22b8b224947ad041bf172d8" + url: "https://pub.dev" + source: hosted + version: "1.0.4" native_toolchain_c: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 67c3e3d..65bce67 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -52,6 +52,7 @@ dev_dependencies: # package. See that file for information about deactivating specific lint # rules and activating additional ones. flutter_lints: ^5.0.0 + mocktail: ^1.0.4 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/test/screens/main_layout_test.dart b/test/screens/main_layout_test.dart new file mode 100644 index 0000000..2088725 --- /dev/null +++ b/test/screens/main_layout_test.dart @@ -0,0 +1,73 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:myapp/screens/main_layout.dart'; + +void main() { + Widget createTestWidget() { + return MaterialApp( + home: MainLayout( + screens: [ + Container(key: const Key('screen_0'), color: Colors.red), + Container(key: const Key('screen_1'), color: Colors.green), + Container(key: const Key('screen_2'), color: Colors.blue), + Container(key: const Key('screen_3'), color: Colors.yellow), + Container(key: const Key('screen_4'), color: Colors.purple), + ], + ), + ); + } + + testWidgets('MainLayout initializes properly and renders bottom navigation', (WidgetTester tester) async { + await tester.pumpWidget(createTestWidget()); + + // Initial load, IndexedStack index should be 0 + final indexedStack = tester.widget(find.byType(IndexedStack)); + expect(indexedStack.index, 0); + + // Verify nav items exist + expect(find.text('Dashboard'), findsOneWidget); + expect(find.text('Device'), findsOneWidget); + expect(find.text('System'), findsOneWidget); + expect(find.text('Battery'), findsOneWidget); + expect(find.text('Network'), findsOneWidget); + }); + + testWidgets('MainLayout changes screens when bottom navigation items are tapped', (WidgetTester tester) async { + await tester.pumpWidget(createTestWidget()); + + // Tap on Device (index 1) + await tester.tap(find.text('Device')); + await tester.pumpAndSettle(); + + var indexedStack = tester.widget(find.byType(IndexedStack)); + expect(indexedStack.index, 1); + + // Tap on System (index 2) + await tester.tap(find.text('System')); + await tester.pumpAndSettle(); + + indexedStack = tester.widget(find.byType(IndexedStack)); + expect(indexedStack.index, 2); + + // Tap on Battery (index 3) + await tester.tap(find.text('Battery')); + await tester.pumpAndSettle(); + + indexedStack = tester.widget(find.byType(IndexedStack)); + expect(indexedStack.index, 3); + + // Tap on Network (index 4) + await tester.tap(find.text('Network')); + await tester.pumpAndSettle(); + + indexedStack = tester.widget(find.byType(IndexedStack)); + expect(indexedStack.index, 4); + + // Tap on Dashboard (index 0) + await tester.tap(find.text('Dashboard')); + await tester.pumpAndSettle(); + + indexedStack = tester.widget(find.byType(IndexedStack)); + expect(indexedStack.index, 0); + }); +}