create widget tests

main
Hinrik Ehrenfried 2023-03-06 12:09:01 +01:00
parent baa104d118
commit dffa3808dd
2 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/providers/input_provider.dart';
import 'package:smoke_cess_app/widgets/drop_down.dart';
void main() {
testWidgets('DropDown should display items and update input model', (WidgetTester tester) async {
// Define the list of items
final items = ['Item 1', 'Item 2', 'Item 3'];
// Create an instance of the InputProvider and add it to the widget tree
final inputProvider = InputProvider();
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: inputProvider),
],
child: MaterialApp(
home: Scaffold(
body: DropDown(items),
),
),
),
);
// Verify that the DropDown displays the first item
expect(find.text(items[0]), findsOneWidget);
// Tap the DropDown to open the menu
await tester.tap(find.byType(DropdownButtonFormField));
await tester.pump();
// Verify that the menu displays the correct items
for (final item in items) {
expect(find.text(item), findsOneWidget);
}
// Select the second item
await tester.tap(find.text(items[1]));
await tester.pump();
// Verify that the input model was updated with the selected item
expect(inputProvider.relapseCategory, equals(items[1]));
});
}

View File

@ -0,0 +1,70 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:smoke_cess_app/widgets/elevated_card.dart';
void main() {
group('ElevatedCard', () {
testWidgets('Renders the title and child', (WidgetTester tester) async {
// Arrange
const title = 'My Card Title';
const childText = 'My Card Content';
final child = Text(childText);
final card = ElevatedCard (title: title, child: child);
// Act
await tester.pumpWidget(MaterialApp(home: Scaffold(body: card)));
final titleFinder = find.text(title);
final childFinder = find.text(childText);
// Assert
expect(titleFinder, findsOneWidget);
expect(childFinder, findsOneWidget);
});
testWidgets('Uses default title style', (WidgetTester tester) async {
// Arrange
final card = ElevatedCard(title: 'My Title', child: Container());
// Act
await tester.pumpWidget(MaterialApp(home: Scaffold(body: card)));
final titleFinder = find.text('My Title');
final titleWidget = tester.widget<Text>(titleFinder);
// Assert
expect(titleWidget.style?.fontSize, equals(16.0));
expect(titleWidget.style?.fontWeight, equals(FontWeight.bold));
});
testWidgets('Uses custom title style', (WidgetTester tester) async {
// Arrange
final card = ElevatedCard(
title: 'My Title',
child: Container(),
);
// Act
await tester.pumpWidget(MaterialApp(home: Scaffold(body: card)));
final titleFinder = find.text('My Title');
final titleWidget = tester.widget<Text>(titleFinder);
// Assert
expect(titleWidget.style?.fontSize, equals(20.0));
expect(titleWidget.style?.fontWeight, equals(FontWeight.bold));
});
testWidgets('Has rounded corners', (WidgetTester tester) async {
// Arrange
final card = ElevatedCard(title: 'My Title', child: Container());
// Act
await tester.pumpWidget(MaterialApp(home: Scaffold(body: card)));
final cardFinder = find.byType(Card);
final cardWidget = tester.widget<Card>(cardFinder);
// Assert
expect(cardWidget.shape,
const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0))));
});
});
}