flutter_application_1/test/widget_test.dart

340 lines
11 KiB
Dart

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/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('Initial state', (WidgetTester tester) async {
const selectedInterval = 'jährlich';
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: IntervalWidget(
selectedInterval: selectedInterval,
onChanged: (newInterval) {},
),
),
),
);
expect(find.text('Ausschüttungsintervall'), findsOneWidget);
expect(find.text(selectedInterval), findsOneWidget);
expect(find.byIcon(Icons.keyboard_arrow_down), findsOneWidget);
});
testWidgets('Tapping the button expands the dropdown', (WidgetTester tester) async {
const selectedInterval = 'jährlich';
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: IntervalWidget(
selectedInterval: selectedInterval,
onChanged: (newInterval) {
},
),
),
),
);
await tester.tap(find.byType(ElevatedButton));
await tester.pump();
expect(find.byType(ListTile), findsNWidgets(2));
expect(find.byIcon(CupertinoIcons.checkmark_alt), findsOneWidget);
});
testWidgets('Selecting an interval updates the state', (WidgetTester tester) async {
const selectedInterval = 'jährlich';
var currentInterval = selectedInterval;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: IntervalWidget(
selectedInterval: selectedInterval,
onChanged: (newInterval) {
currentInterval = newInterval;
},
),
),
),
);
await tester.tap(find.byType(ElevatedButton));
await tester.pump();
await tester.tap(find.text('monatlich'));
await tester.pump();
expect(currentInterval, 'monatlich');
expect(find.byType(ListTile), findsNothing);
expect(find.byIcon(Icons.keyboard_arrow_down), 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);
});
});
}