Files
receipity/test/widget_test.dart
T
2026-08-28 18:23:56 +02:00

67 lines
2.1 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:myapp/main.dart';
import 'package:myapp/models/item.dart';
import 'package:myapp/services/item_repository.dart';
import 'package:myapp/services/storage_service.dart';
import 'package:myapp/utils/constants.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
Future<void> pumpApp(WidgetTester tester) async {
SharedPreferences.setMockInitialValues({});
final storage = StorageService();
await storage.init();
await tester.pumpWidget(
MyApp(
storage: storage,
repository: ItemRepository(),
),
);
}
testWidgets('splash shows the app name then opens Home', (tester) async {
await pumpApp(tester);
expect(find.text(kAppName), findsWidgets);
await tester.pump(kSplashDuration);
await tester.pump(const Duration(milliseconds: 450));
await tester.pump(const Duration(milliseconds: 350));
expect(find.text('Home'), findsWidgets);
expect(find.text('Featured items'), findsOneWidget);
expect(find.text('Search'), findsOneWidget);
expect(find.text('Profile'), findsOneWidget);
expect(find.text('Settings'), findsOneWidget);
});
testWidgets('tapping a list card opens the detail screen', (tester) async {
await pumpApp(tester);
await tester.pump(kSplashDuration);
await tester.pump(const Duration(milliseconds: 450));
await tester.pump(const Duration(milliseconds: 350));
await tester.tap(find.text('Material 3 surfaces'));
await tester.pump();
await tester.pump(const Duration(milliseconds: 400));
expect(find.text('Details'), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
});
test('Item.fromJson maps a JSONPlaceholder post', () {
final item = Item.fromJson({
'id': 3,
'title': 'hello world',
'body': 'First sentence. Second sentence.',
});
expect(item.id, 'remote-3');
expect(item.title, 'Hello world');
expect(item.subtitle, 'First sentence.');
expect(item.category, ItemCategory.lifestyle);
});
}