From dffa3808dd5eba4bb30c7c3d126016fd426f1aa8 Mon Sep 17 00:00:00 2001 From: "h.ehrenfried" <2012537@stud.hs-mannheim.de> Date: Mon, 6 Mar 2023 12:09:01 +0100 Subject: [PATCH] create widget tests --- test/widget_drop_down_test.dart | 46 +++++++++++++++++++ test/widget_elevated_card_test.dart | 70 +++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 test/widget_drop_down_test.dart create mode 100644 test/widget_elevated_card_test.dart diff --git a/test/widget_drop_down_test.dart b/test/widget_drop_down_test.dart new file mode 100644 index 0000000..999f125 --- /dev/null +++ b/test/widget_drop_down_test.dart @@ -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])); + }); +} diff --git a/test/widget_elevated_card_test.dart b/test/widget_elevated_card_test.dart new file mode 100644 index 0000000..2571582 --- /dev/null +++ b/test/widget_elevated_card_test.dart @@ -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(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(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(cardFinder); + + // Assert + expect(cardWidget.shape, + const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0)))); + }); + }); +}