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.
This commit is contained in:
pankaj-basnet
2026-07-12 13:47:45 +05:45
parent c088af2223
commit e81bb990ea
4 changed files with 349 additions and 3 deletions

View File

@@ -0,0 +1,67 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* 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 <http://www.gnu.org/licenses/>.
*/
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);
});
});
}

View File

@@ -0,0 +1,111 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* 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 <http://www.gnu.org/licenses/>.
*/
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: '',
),
]
: <MeasurementEntry>[];
final diaEntries = withEntries
? [
MeasurementEntry(
id: 'e2',
categoryId: 'dia',
date: DateTime(2026, 1, 1),
value: 80,
notes: '',
),
]
: <MeasurementEntry>[];
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);
});
});
}

View File

@@ -16,18 +16,51 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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)),

View File

@@ -0,0 +1,135 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* 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 <http://www.gnu.org/licenses/>.
*/
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<Widget> 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<Widget> 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));
});
});
}