Chart a group as a whole, and let it be opened

This commit is contained in:
Roland Geider
2026-08-02 14:05:54 +02:00
parent 3a26357a2c
commit e1b5f1345b
13 changed files with 727 additions and 47 deletions

View File

@@ -71,6 +71,40 @@ MeasurementCategory _tripleGroup() {
return testMeasurementCategoryBloodPressure.copyWith(children: children);
}
/// A sleep group: the total plus two stages, all on the same night
MeasurementCategory _sleepGroup() {
MeasurementCategory child(String id, String name, MetricType type, int order, num value) =>
MeasurementCategory(
id: id,
name: name,
unit: 'min',
metricType: type,
parentId: 'sleep',
order: order,
entries: [
MeasurementEntry(
id: 'e-$id',
categoryId: id,
date: DateTime(2026, 1, 2),
value: value,
notes: '',
),
],
);
return MeasurementCategory(
id: 'sleep',
name: 'Sleep',
unit: 'min',
metricType: MetricType.sleep,
children: [
child('total', 'Total sleep', MetricType.sleepTotal, 0, 480),
child('deep', 'Deep sleep', MetricType.sleepDeep, 1, 90),
child('rem', 'REM sleep', MetricType.sleepRem, 2, 60),
],
);
}
void main() {
group('CategoriesCard group card', () {
testWidgets('shows one ListTile per child with latest reading', (tester) async {
@@ -167,6 +201,36 @@ void main() {
expect(find.byIcon(Icons.add), findsOneWidget);
});
testWidgets('the group itself can be opened, like any other category', (tester) async {
await tester.pumpWidget(_card(_bpGroup(withEntries: true)));
await tester.pumpAndSettle();
expect(find.text('Go to detail page'), findsOneWidget);
});
testWidgets('sleep stacks its stages into one bar per night', (tester) async {
await tester.pumpWidget(_card(_sleepGroup()));
await tester.pumpAndSettle();
final groups = tester.widget<BarChart>(find.byType(BarChart)).data.barGroups;
expect(groups, hasLength(1));
// The stages stack, the total does not: it already covers them
final rod = groups.single.barRods.single;
expect(rod.rodStackItems.map((s) => s.toY - s.fromY), [90, 60]);
expect(rod.toY, 150);
});
testWidgets('the roll-up row gets no colour dot', (tester) async {
await tester.pumpWidget(_card(_sleepGroup()));
await tester.pumpAndSettle();
// Total sleep is no segment of the stack, so no colour identifies it
final tiles = tester.widgetList<ListTile>(find.byType(ListTile)).toList();
expect(tiles.first.leading, isNull);
expect(tiles.skip(1).every((t) => t.leading != null), isTrue);
});
});
group('CategoriesCard daily aggregates', () {

View File

@@ -24,6 +24,7 @@ import 'package:mockito/mockito.dart';
import 'package:wger/features/measurements/models/measurement_category.dart';
import 'package:wger/features/measurements/models/measurement_entry.dart';
import 'package:wger/features/measurements/providers/measurement_repository.dart';
import 'package:wger/features/measurements/widgets/charts.dart';
import 'package:wger/features/measurements/widgets/entries.dart';
import 'package:wger/features/nutrition/providers/ingredient_repository.dart';
import 'package:wger/features/nutrition/providers/nutrition_repository.dart';
@@ -64,16 +65,7 @@ void main() {
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: SingleChildScrollView(
child: EntriesList(
MeasurementCategory(
id: category.id,
name: category.name,
unit: category.unit,
entries: category.entries,
),
),
),
body: SingleChildScrollView(child: EntriesList(category)),
),
),
);
@@ -96,4 +88,51 @@ void main() {
expect(find.byTooltip('Show menu'), findsOneWidget);
expect(find.byIcon(Icons.monitor_heart_outlined), findsOneWidget);
});
testWidgets('A group charts its components and lists the readings', (
WidgetTester tester,
) async {
// The list defaults to the last three months, so a fixed date would fall
// out of the range as time passes
final night = DateTime.now().subtract(const Duration(days: 1));
MeasurementCategory child(String id, String name, MetricType type, int order, num value) =>
MeasurementCategory(
id: id,
name: name,
unit: 'min',
metricType: type,
parentId: 'sleep',
order: order,
entries: [
MeasurementEntry(
id: 'e-$id',
categoryId: id,
date: night,
value: value,
notes: '',
),
],
);
final group = MeasurementCategory(
id: 'sleep',
name: 'Sleep',
unit: 'min',
metricType: MetricType.sleep,
children: [
child('total', 'Total sleep', MetricType.sleepTotal, 0, 480),
child('deep', 'Deep sleep', MetricType.sleepDeep, 1, 90),
],
);
await tester.pumpWidget(createEntriesList(group));
await tester.pumpAndSettle();
// The group holds no entries itself, so its own list would be empty: it
// shows the components' readings instead, the roll-up leading
expect(find.byType(MeasurementStackedBarChartWidgetFl), findsOneWidget);
expect(find.text('480 min'), findsOneWidget);
expect(find.textContaining('Deep sleep 90'), findsOneWidget);
// Each component is a way into its own screen
expect(find.byIcon(Icons.chevron_right), findsNWidgets(2));
});
}

View File

@@ -154,6 +154,159 @@ void main() {
});
});
group('group charts', () {
MeasurementEntry entry(String categoryId, DateTime date, num value) =>
MeasurementEntry(categoryId: categoryId, date: date, value: value, notes: '');
/// A sleep group as the importer writes it: the total plus its stages.
MeasurementCategory sleepGroup({bool withStages = true}) => MeasurementCategory(
id: 'g',
name: 'Sleep',
unit: 'min',
metricType: MetricType.sleep,
children: [
MeasurementCategory(
id: 'total',
name: 'Total sleep',
unit: 'min',
metricType: MetricType.sleepTotal,
parentId: 'g',
entries: [entry('total', DateTime(2026, 1, 2), 480)],
),
MeasurementCategory(
id: 'deep',
name: 'Deep sleep',
unit: 'min',
metricType: MetricType.sleepDeep,
parentId: 'g',
order: 1,
entries: withStages ? [entry('deep', DateTime(2026, 1, 2), 90)] : const [],
),
MeasurementCategory(
id: 'rem',
name: 'REM sleep',
unit: 'min',
metricType: MetricType.sleepRem,
parentId: 'g',
order: 2,
entries: withStages ? [entry('rem', DateTime(2026, 1, 2), 60)] : const [],
),
],
);
MeasurementCategory bloodPressure() => MeasurementCategory(
id: 'bp',
name: 'Blood pressure',
unit: 'mmHg',
metricType: MetricType.bloodPressure,
children: [
MeasurementCategory(
id: 'sys',
name: 'Systolic',
unit: 'mmHg',
metricType: MetricType.bloodPressureSystolic,
parentId: 'bp',
entries: [entry('sys', DateTime(2026, 1, 2, 8), 120)],
),
MeasurementCategory(
id: 'dia',
name: 'Diastolic',
unit: 'mmHg',
metricType: MetricType.bloodPressureDiastolic,
parentId: 'bp',
order: 1,
entries: [entry('dia', DateTime(2026, 1, 2, 8), 80)],
),
],
);
test('the roll-up component is left out of the stack', () {
// Total sleep covers the stages, so stacking it would count the night
// twice
expect(
stackableComponents(sleepGroup()).map((c) => c.metricType),
[MetricType.sleepDeep, MetricType.sleepRem],
);
expect(stackableComponents(bloodPressure()), hasLength(2));
});
test('stacked entries carry one value per component and day', () {
final components = stackableComponents(sleepGroup());
final stacked = groupStackedEntries(components);
expect(stacked, hasLength(1));
expect(stacked.single.date, DateTime(2026, 1, 2));
expect(stacked.single.values, [90, 60]);
expect(stacked.single.total, 150);
});
test('several entries of one day add up within their component', () {
// A nap next to the night: the bar shows the day, not the segment
final group = MeasurementCategory(
id: 'g',
name: 'Sleep',
unit: 'min',
metricType: MetricType.sleep,
children: [
MeasurementCategory(
id: 'deep',
name: 'Deep sleep',
unit: 'min',
metricType: MetricType.sleepDeep,
parentId: 'g',
entries: [
entry('deep', DateTime(2026, 1, 2, 3), 90),
entry('deep', DateTime(2026, 1, 2, 14), 20),
],
),
],
);
expect(groupStackedEntries(group.children).single.values, [110]);
});
test('readings pair the components on their shared timestamp', () {
final readings = groupReadings(bloodPressure());
expect(readings, hasLength(1));
expect(readings.single.$1, DateTime(2026, 1, 2, 8));
expect(readings.single.$2, {'Systolic': 120, 'Diastolic': 80});
});
testWidgets('a summed group stacks its components', (tester) async {
await tester.pumpWidget(
_wrapChart(Builder(builder: (ctx) => buildGroupChart(ctx, sleepGroup()))),
);
await tester.pumpAndSettle();
expect(find.byType(MeasurementStackedBarChartWidgetFl), findsOneWidget);
});
testWidgets('a two-component group is a floating bar per reading', (tester) async {
await tester.pumpWidget(
_wrapChart(Builder(builder: (ctx) => buildGroupChart(ctx, bloodPressure()))),
);
await tester.pumpAndSettle();
expect(find.byType(MeasurementBarChartWidgetFl), findsOneWidget);
expect(find.byType(MeasurementStackedBarChartWidgetFl), findsNothing);
});
testWidgets('without stage data the group falls back to lines', (tester) async {
// Only the total reported, so there is nothing to stack. Falling through
// to the line chart keeps the card from going blank while data exists
await tester.pumpWidget(
_wrapChart(
Builder(builder: (ctx) => buildGroupChart(ctx, sleepGroup(withStages: false))),
),
);
await tester.pumpAndSettle();
expect(find.byType(MeasurementStackedBarChartWidgetFl), findsNothing);
expect(find.byType(MeasurementChartWidgetFl), findsOneWidget);
});
});
group('getOverviewWidgets', () {
testWidgets('empty raw shows no-data placeholder', (tester) async {
await _pumpWidgetList(tester, (ctx) => getOverviewWidgets('Test', [], [], 'cm', ctx));