From 6c0708ca77eb96d735fbcc7f7f59454b32ec5c45 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Mon, 6 Mar 2023 20:52:35 +0100 Subject: [PATCH] historylist test --- test/widget_tests/history_list_test.dart | 102 +++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 test/widget_tests/history_list_test.dart diff --git a/test/widget_tests/history_list_test.dart b/test/widget_tests/history_list_test.dart new file mode 100644 index 0000000..3ed6809 --- /dev/null +++ b/test/widget_tests/history_list_test.dart @@ -0,0 +1,102 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:intl/date_symbol_data_local.dart'; +import 'package:intl/intl.dart'; +import 'package:smoke_cess_app/widgets/entry_detail_widget.dart'; +import 'package:smoke_cess_app/widgets/history_list_widget.dart'; + +void main() { + group('HistoryList', () { + initializeDateFormatting('de'); + final List history = ['1', '2', '3']; + dateSelector(String p0) => DateTime.now(); + entryDataSelector(String p0) => p0; + + testWidgets('should produce #(history.length) EntryDetail Widgets', + (widgetTester) async { + await widgetTester.pumpWidget(WrappedHistoryList( + history: history, + dateSelector: dateSelector, + entryDataSelector: entryDataSelector)); + final widgetFinder = find.byType(EntryDetail); + final dateFinder = + find.text(DateFormat.MMMd('de').format(DateTime.now())); + //HistoryList uses Icons.circle as default if there is no IconData provided + final iconFinder = find.byIcon(Icons.circle); + expect(widgetFinder, findsNWidgets(history.length)); + expect(dateFinder, findsNWidgets(history.length)); + expect(iconFinder, findsNWidgets(history.length)); + }); + + testWidgets( + 'should render correct icon if specified with icon selection function', + (widgetTester) async { + IconData iconDataSelector(String p0) => Icons.email; + + await widgetTester.pumpWidget(WrappedHistoryList( + history: history, + dateSelector: dateSelector, + entryDataSelector: entryDataSelector, + iconDataSelector: iconDataSelector, + )); + final circleIconFinder = find.byIcon(Icons.circle); + final correctIconFinder = find.byIcon(Icons.email); + + expect(circleIconFinder, findsNothing); + expect(correctIconFinder, findsNWidgets(history.length)); + }); + + testWidgets('icon field should overwrite iconSelector', + (widgetTester) async { + const IconData icon = Icons.abc; + IconData iconDataSelector(String p0) => Icons.email; + await widgetTester.pumpWidget(WrappedHistoryList( + history: history, + dateSelector: dateSelector, + entryDataSelector: entryDataSelector, + iconDataSelector: iconDataSelector, + icon: Icons.abc, + )); + final circleIconFinder = find.byIcon(Icons.circle); + final emailIconFinder = find.byIcon(Icons.email); + final correctIconFinder = find.byIcon(icon); + + expect(circleIconFinder, findsNothing); + expect(emailIconFinder, findsNothing); + expect(correctIconFinder, findsNWidgets(history.length)); + }); + }); +} + +class WrappedHistoryList extends StatelessWidget { + final List history; + final DateTime Function(T) dateSelector; + final String Function(T) entryDataSelector; + final IconData Function(T)? iconDataSelector; + final String Function(T)? entryCommentSelector; + final IconData? icon; + const WrappedHistoryList( + {super.key, + required this.history, + required this.dateSelector, + required this.entryDataSelector, + this.iconDataSelector, + this.icon, + this.entryCommentSelector}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: Scaffold( + body: Column(children: [ + HistoryList( + history: history, + dateSelector: dateSelector, + entryDataSelector: entryDataSelector, + icon: icon, + iconDataSelector: iconDataSelector, + entryCommentSelector: entryCommentSelector, + ) + ]))); + } +}