From e81bb990ea3a4a47fd957930fec47542352b7581 Mon Sep 17 00:00:00 2001 From: pankaj-basnet <165250380+pankaj-basnet@users.noreply.github.com> Date: Sun, 12 Jul 2026 13:47:45 +0545 Subject: [PATCH] test(measurements): add unit tests for chart rendering, aggregation, metric routing, and composite group cards (#1251) Validates MeasurementBarChartWidgetFl rendering, aggregatePerDay logic, buildChartForMetricType routing, and CategoriesCard group composition for multi-value metrics like blood pressure. --- .../build_chart_for_metric_type_test.dart | 67 +++++++++ .../widgets/categories_card_group_test.dart | 111 ++++++++++++++ .../measurements/widgets/charts_test.dart | 39 ++++- .../widgets/get_overview_widgets_test.dart | 135 ++++++++++++++++++ 4 files changed, 349 insertions(+), 3 deletions(-) create mode 100644 test/features/measurements/widgets/build_chart_for_metric_type_test.dart create mode 100644 test/features/measurements/widgets/categories_card_group_test.dart create mode 100644 test/features/measurements/widgets/get_overview_widgets_test.dart diff --git a/test/features/measurements/widgets/build_chart_for_metric_type_test.dart b/test/features/measurements/widgets/build_chart_for_metric_type_test.dart new file mode 100644 index 000000000..170df0afa --- /dev/null +++ b/test/features/measurements/widgets/build_chart_for_metric_type_test.dart @@ -0,0 +1,67 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/features/measurements/models/measurement_category.dart'; +import 'package:wger/features/measurements/widgets/charts.dart'; +import 'package:wger/features/measurements/widgets/helpers.dart'; +import 'package:wger/l10n/generated/app_localizations.dart'; + +Widget _wrapChart(Widget chart) => MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Scaffold( + body: SizedBox(width: 400, height: 300, child: chart), + ), +); + +void main() { + final entries = [ + MeasurementChartEntry(1000, DateTime(2026, 1, 1)), + MeasurementChartEntry(2000, DateTime(2026, 1, 2)), + ]; + + group('buildChartForMetricType routing', () { + testWidgets('steps -> MeasurementBarChartWidgetFl', (tester) async { + final widget = buildChartForMetricType(MetricType.steps, entries, [], 'steps'); + await tester.pumpWidget(_wrapChart(widget)); + await tester.pumpAndSettle(); + + expect(find.byType(MeasurementBarChartWidgetFl), findsOneWidget); + expect(find.byType(MeasurementChartWidgetFl), findsNothing); + }); + + testWidgets('custom -> MeasurementChartWidgetFl', (tester) async { + final widget = buildChartForMetricType(MetricType.custom, entries, [], 'cm'); + await tester.pumpWidget(_wrapChart(widget)); + await tester.pumpAndSettle(); + + expect(find.byType(MeasurementChartWidgetFl), findsOneWidget); + expect(find.byType(MeasurementBarChartWidgetFl), findsNothing); + }); + + testWidgets('energy -> MeasurementBarChartWidgetFl', (tester) async { + final widget = buildChartForMetricType(MetricType.energy, entries, [], 'kcal'); + await tester.pumpWidget(_wrapChart(widget)); + await tester.pumpAndSettle(); + + expect(find.byType(MeasurementBarChartWidgetFl), findsOneWidget); + }); + }); +} diff --git a/test/features/measurements/widgets/categories_card_group_test.dart b/test/features/measurements/widgets/categories_card_group_test.dart new file mode 100644 index 000000000..6e6fc30dd --- /dev/null +++ b/test/features/measurements/widgets/categories_card_group_test.dart @@ -0,0 +1,111 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/features/measurements/models/measurement_category.dart'; +import 'package:wger/features/measurements/models/measurement_entry.dart'; +import 'package:wger/features/measurements/widgets/categories_card.dart'; +import 'package:wger/l10n/generated/app_localizations.dart'; + +Widget _wrap(Widget child) => MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + routes: { + '/form': (ctx) => const Scaffold(body: Text('form screen')), + }, + home: Scaffold(body: child), +); + +MeasurementCategory _bpGroup({bool withEntries = false}) { + final sysEntries = withEntries + ? [ + MeasurementEntry( + id: 'e1', + categoryId: 'sys', + date: DateTime(2026, 1, 1), + value: 120, + notes: '', + ), + ] + : []; + final diaEntries = withEntries + ? [ + MeasurementEntry( + id: 'e2', + categoryId: 'dia', + date: DateTime(2026, 1, 1), + value: 80, + notes: '', + ), + ] + : []; + + final sys = MeasurementCategory( + id: 'sys', + name: 'Systolic', + unit: 'mmHg', + parentId: 'bp', + order: 0, + entries: sysEntries, + ); + final dia = MeasurementCategory( + id: 'dia', + name: 'Diastolic', + unit: 'mmHg', + parentId: 'bp', + order: 1, + entries: diaEntries, + ); + + return MeasurementCategory( + id: 'bp', + name: 'Blood pressure', + unit: 'mmHg', + metricType: MetricType.bloodPressure, + children: [sys, dia], + ); +} + +void main() { + group('CategoriesCard — group card', () { + testWidgets('shows one ListTile per child with latest reading', (tester) async { + await tester.pumpWidget(_wrap(CategoriesCard(_bpGroup(withEntries: true)))); + await tester.pumpAndSettle(); + + expect(find.byType(ListTile), findsNWidgets(2)); + expect(find.textContaining('120'), findsOneWidget); + expect(find.textContaining('80'), findsOneWidget); + }); + + testWidgets('shows dash when child has no entries', (tester) async { + await tester.pumpWidget(_wrap(CategoriesCard(_bpGroup(withEntries: false)))); + await tester.pumpAndSettle(); + + // Text('—') should appear for both children + expect(find.text('—'), findsNWidgets(2)); + }); + + testWidgets('add icon button is present on group card', (tester) async { + await tester.pumpWidget(_wrap(CategoriesCard(_bpGroup()))); + await tester.pumpAndSettle(); + + expect(find.byIcon(Icons.add), findsOneWidget); + }); + }); +} diff --git a/test/features/measurements/widgets/charts_test.dart b/test/features/measurements/widgets/charts_test.dart index 0043acfc3..73c85a75c 100644 --- a/test/features/measurements/widgets/charts_test.dart +++ b/test/features/measurements/widgets/charts_test.dart @@ -16,18 +16,51 @@ * along with this program. If not, see . */ +import 'package:fl_chart/fl_chart.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:wger/features/measurements/widgets/charts.dart'; +import 'package:wger/l10n/generated/app_localizations.dart'; + +Widget _wrap(Widget child) => MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Scaffold( + body: SizedBox(width: 400, height: 300, child: child), + ), +); void main() { MeasurementChartEntry entry(num value, DateTime date) => MeasurementChartEntry(value, date); - group('aggregatePerDay', () { - test('returns an empty list for no entries', () { + group('MeasurementBarChartWidgetFl', () { + testWidgets('renders without error for empty entries', (tester) async { + await tester.pumpWidget( + _wrap(const MeasurementBarChartWidgetFl([], 'steps')), + ); + await tester.pumpAndSettle(); + expect(find.byType(MeasurementBarChartWidgetFl), findsOneWidget); + expect(tester.takeException(), isNull); + }); + + // T-24 + testWidgets('BarChart is present for non-empty entries', (tester) async { + final entries = [ + entry(1000, DateTime(2026, 1, 1)), + entry(2000, DateTime(2026, 1, 2)), + entry(1500, DateTime(2026, 1, 3)), + ]; + await tester.pumpWidget(_wrap(MeasurementBarChartWidgetFl(entries, 'steps'))); + await tester.pumpAndSettle(); + + expect(find.byType(BarChart), findsOneWidget); + }); + + test('aggregatePerDay returns an empty list for no entries', () { expect(aggregatePerDay([]), isEmpty); }); - test('sums entries sharing a calendar day into one point', () { + test('aggregatePerDay sums entries sharing a calendar day into one point', () { final result = aggregatePerDay([ entry(1000, DateTime(2026, 1, 1, 8)), entry(2500, DateTime(2026, 1, 1, 20)), diff --git a/test/features/measurements/widgets/get_overview_widgets_test.dart b/test/features/measurements/widgets/get_overview_widgets_test.dart new file mode 100644 index 000000000..8a838b199 --- /dev/null +++ b/test/features/measurements/widgets/get_overview_widgets_test.dart @@ -0,0 +1,135 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 - 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/features/measurements/models/measurement_category.dart'; +import 'package:wger/features/measurements/widgets/charts.dart'; +import 'package:wger/features/measurements/widgets/helpers.dart'; +import 'package:wger/l10n/generated/app_localizations.dart'; + +void main() { + final rawEntries = [ + MeasurementChartEntry(10, DateTime(2026, 1, 1)), + MeasurementChartEntry(20, DateTime(2026, 1, 10)), + ]; + final avgEntries = moving7dAverage(rawEntries); + + group('getOverviewWidgets', () { + testWidgets('empty raw shows no-data placeholder', (tester) async { + late List widgets; + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Builder( + builder: (ctx) { + widgets = getOverviewWidgets('Test', [], [], 'cm', ctx); + return Scaffold( + body: SingleChildScrollView(child: Column(children: widgets)), + ); + }, + ), + ), + ); + await tester.pumpAndSettle(); + + expect(find.text('No data available'), findsOneWidget); + }); + + testWidgets('non-empty avg includes MeasurementOverallChangeWidget', (tester) async { + late List widgets; + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Builder( + builder: (ctx) { + widgets = getOverviewWidgets('Test', rawEntries, avgEntries, 'cm', ctx); + return Scaffold( + body: SingleChildScrollView(child: Column(children: widgets)), + ); + }, + ), + ), + ); + await tester.pumpAndSettle(); + + expect(find.byType(MeasurementOverallChangeWidget), findsOneWidget); + }); + }); + + group('getOverviewWidgetsSeries — Indicator legend', () { + testWidgets('three Indicator widgets for non-summed metric (custom)', (tester) async { + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Builder( + builder: (ctx) { + final widgets = getOverviewWidgetsSeries( + 'Weight', + rawEntries, + avgEntries, + [], + 'kg', + ctx, + metricType: MetricType.custom, + ); + return Scaffold( + body: SingleChildScrollView(child: Column(children: widgets)), + ); + }, + ), + ), + ); + await tester.pumpAndSettle(); + + expect(find.byType(Indicator), findsNWidgets(3)); + }); + + testWidgets('two Indicator widgets for summed metric (steps — no trend)', (tester) async { + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Builder( + builder: (ctx) { + final widgets = getOverviewWidgetsSeries( + 'Steps', + rawEntries, + avgEntries, + [], + 'steps', + ctx, + metricType: MetricType.steps, + ); + return Scaffold( + body: SingleChildScrollView(child: Column(children: widgets)), + ); + }, + ), + ), + ); + await tester.pumpAndSettle(); + + // isSummedPerDay -> trend indicator is hidden + expect(find.byType(Indicator), findsNWidgets(2)); + }); + }); +}