flutter_application_1/test/widget_test.dart

332 lines
11 KiB
Dart
Raw Normal View History

2024-06-12 12:48:30 +02:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/enums.dart';
import 'package:flutter_application_1/pages/chart_page.dart';
import 'package:flutter_application_1/pages/milestone_page.dart';
import 'package:flutter_application_1/utils.dart';
import 'package:flutter_application_1/widgets/chart_widget.dart';
import 'package:flutter_application_1/widgets/error_widget.dart';
import 'package:flutter_application_1/widgets/input_widget.dart';
import 'package:flutter_application_1/widgets/interval_widget.dart';
import 'package:flutter_application_1/widgets/milestone_timeline_widget.dart';
import 'package:flutter_application_1/widgets/result_widget.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
void main() {
group('InputWidget Tests', () {
testWidgets('Displays label, tooltip, and input field', (WidgetTester tester) async {
const label = 'Test Label';
const tooltipText = 'This is a tooltip';
const suffixText = 'suffix';
final controller = TextEditingController();
final focusNode = FocusNode();
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('Test InputWidget'),
),
child: InputWidget(
label: label,
controller: controller,
focusNode: focusNode,
isValid: false,
suffixText: suffixText,
tooltipText: tooltipText,
),
),
),
);
expect(find.text(label), findsOneWidget);
expect(find.text(suffixText), findsOneWidget);
expect(find.byType(CupertinoTextField), findsOneWidget);
expect(find.byType(Tooltip), findsOneWidget);
});
testWidgets('Displays validation icon correctly', (WidgetTester tester) async {
final controller = TextEditingController();
final focusNode = FocusNode();
await tester.pumpWidget(
CupertinoApp(
home: InputWidget(
label: 'Test Label',
controller: controller,
focusNode: focusNode,
isValid: false,
suffixText: 'suffix',
tooltipText: 'This is a tooltip',
),
),
);
expect(find.byIcon(CupertinoIcons.clear_circled_solid), findsOneWidget);
expect(find.byIcon(CupertinoIcons.check_mark_circled_solid), findsNothing);
await tester.pumpWidget(
CupertinoApp(
home: InputWidget(
label: 'Test Label',
controller: controller,
focusNode: focusNode,
isValid: true,
suffixText: 'suffix',
tooltipText: 'This is a tooltip',
),
),
);
expect(find.byIcon(CupertinoIcons.check_mark_circled_solid), findsOneWidget);
expect(find.byIcon(CupertinoIcons.clear_circled_solid), findsNothing);
});
testWidgets('Text input updates controller', (WidgetTester tester) async {
final controller = TextEditingController();
final focusNode = FocusNode();
await tester.pumpWidget(
CupertinoApp(
home: InputWidget(
label: 'Test Label',
controller: controller,
focusNode: focusNode,
isValid: false,
suffixText: 'suffix',
tooltipText: 'This is a tooltip',
),
),
);
await tester.enterText(find.byType(CupertinoTextField), '12345');
expect(controller.text, '12345');
});
});
group('Interal Widget Tests', () {
testWidgets('Displays label and selected interval', (WidgetTester tester) async {
final selectedInterval = translateInterval(PayoutInterval.yearly);
final Widget widget = CupertinoApp(
home: Scaffold(
body: IntervalWidget(
selectedInterval: selectedInterval,
onChanged: (interval) {},
),
),
);
await tester.pumpWidget(widget);
expect(find.text('Ausschüttungsintervall'), findsOneWidget);
expect(find.text(selectedInterval), findsOneWidget);
});
testWidgets('Expands on button press', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: IntervalWidget(
selectedInterval: translateInterval(PayoutInterval.yearly),
onChanged: (interval) {},
),
),
),
);
await tester.tap(find.byType(ElevatedButton));
await tester.pump();
expect(find.text(translateInterval(PayoutInterval.yearly)), findsNWidgets(2));
expect(find.text(translateInterval(PayoutInterval.monthly)), findsOneWidget);
});
testWidgets('Selects interval on tap', (WidgetTester tester) async {
String selectedInterval = '';
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: IntervalWidget(
selectedInterval: selectedInterval,
onChanged: (interval) {
selectedInterval = interval;
},
),
),
),
);
await tester.tap(find.byType(ElevatedButton));
await tester.pump();
await tester.tap(find.text(translateInterval(PayoutInterval.monthly)));
await tester.pump();
expect(find.text('monatlich'), findsOneWidget);
});
});
group('Chart Widget Tests', () {
testWidgets('Displays chart correctly', (WidgetTester tester) async {
final List<double> lowerValues = [100, 200, 300, 400];
final List<double> upperValues = [50, 150, 250, 350];
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: StackedColumnChart(
lowerValues: lowerValues,
upperValues: upperValues,
),
),
),
);
expect(find.byType(SfCartesianChart), findsOneWidget);
expect(find.text('Einzahlungen'), findsOneWidget);
expect(find.text('Zinsen'), findsOneWidget);
expect(find.byType(CategoryAxis), findsOneWidget);
expect(find.byType(NumericAxis), findsOneWidget);
expect(find.text('Einzahlungen'), findsOneWidget);
expect(find.text('Zinsen'), findsOneWidget);
await tester.pumpAndSettle();
});
});
group('Milestone Timeline Widget Tests', () {
testWidgets('Milestone timeline displays correctly', (WidgetTester tester) async {
final List<Map<String, dynamic>> milestones = [
{'value': 100.0, 'emoji': '😊', 'text': 'First milestone'},
{'value': 200.0, 'emoji': '😃', 'text': 'Second milestone'},
{'value': 300.0, 'emoji': '😁', 'text': 'Third milestone'},
];
const double totalInterest = 250.0;
await tester.pumpWidget(
CupertinoApp(
home: Material(
child: MilestoneTimeline(
milestones: milestones,
totalInterest: totalInterest,
),
),
),
);
expect(find.text('First milestone'), findsOneWidget);
expect(find.text('Second milestone'), findsOneWidget);
expect(find.text('Third milestone'), findsOneWidget);
expect(find.byIcon(CupertinoIcons.check_mark_circled_solid), findsNWidgets(2));
expect(find.byIcon(CupertinoIcons.circle_fill), findsOneWidget);
});
});
group('Result Widget Tests', () {
testWidgets('Displays result values correctly', (WidgetTester tester) async {
const compoundInterest = '1000';
const investedMoney = '500';
const time = '5';
const monthlySavingsRate = '100';
const interestRate = '5';
const payoutInterval = PayoutInterval.monthly;
final List<double> investedMoneyList = [100, 200, 300, 400, 500];
final List<double> compoundInterestList = [100, 200, 300, 400, 1000];
await tester.pumpWidget(
MaterialApp(
home: Material(
child: ResultWidget(
compoundInterest: compoundInterest,
investedMoney: investedMoney,
time: time,
monthlySavingsRate: monthlySavingsRate,
interestRate: interestRate,
payoutInterval: payoutInterval,
investedMoneyList: investedMoneyList,
compoundInterestList: compoundInterestList,
),
),
),
);
expect(find.text('Endkapital:'), findsOneWidget);
expect(find.text('$compoundInterest'), findsOneWidget);
expect(find.text('Einzahlungen:'), findsOneWidget);
expect(find.text('$investedMoney'), findsOneWidget);
expect(find.text('Erhaltene Zinsen:'), findsOneWidget);
expect(find.text('${double.parse(compoundInterest) + double.parse(investedMoney)}'), findsOneWidget);
});
testWidgets('Navigates to ChartPage on "Grafik" button press', (WidgetTester tester) async {
final List<double> investedMoneyList = [100, 200, 300, 400, 500];
final List<double> compoundInterestList = [100, 200, 300, 400, 1000];
await tester.pumpWidget(
MaterialApp(
home: Material(
child: ResultWidget(
compoundInterest: '1000',
investedMoney: '500',
time: '5',
monthlySavingsRate: '100',
interestRate: '5',
payoutInterval: PayoutInterval.monthly,
investedMoneyList: investedMoneyList,
compoundInterestList: compoundInterestList,
),
),
),
);
await tester.tap(find.text('Grafik'));
await tester.pumpAndSettle();
expect(find.byType(ChartPage), findsOneWidget);
});
testWidgets('Navigates to MilestonePage on "Meilensteine" button press', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Material(
child: ResultWidget(
compoundInterest: '1000',
investedMoney: '500',
time: '5',
monthlySavingsRate: '100',
interestRate: '5',
payoutInterval: PayoutInterval.monthly,
investedMoneyList: [],
compoundInterestList: [],
),
),
),
);
await tester.tap(find.text('Meilensteine'));
await tester.pumpAndSettle();
expect(find.byType(MilestonePage), findsOneWidget);
});
});
group('Error Widget Tests', () {
testWidgets('Displays error message correctly', (WidgetTester tester) async {
const String errorMessage = 'This is an error message';
await tester.pumpWidget(
const CupertinoApp(
home: Material(
child: ErrWidget(
errorMessage: errorMessage,
),
),
),
);
expect(find.byIcon(CupertinoIcons.exclamationmark_circle_fill), findsOneWidget);
expect(find.text(errorMessage), findsOneWidget);
});
});
}