Skip to content

Commit e86fcb6

Browse files
committed
test(measurements): add widget tests for GroupMeasurementEntryForm
1 parent e81bb99 commit e86fcb6

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* This file is part of wger Workout Manager <https://github.com/wger-project>.
3+
* Copyright (c) 2026 wger Team
4+
*
5+
* wger Workout Manager is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
import 'package:flutter/material.dart';
20+
import 'package:flutter_riverpod/flutter_riverpod.dart';
21+
import 'package:flutter_test/flutter_test.dart';
22+
import 'package:mockito/mockito.dart';
23+
import 'package:wger/core/widgets/decimal_input.dart';
24+
import 'package:wger/features/measurements/providers/measurement_repository.dart';
25+
import 'package:wger/features/measurements/widgets/forms.dart';
26+
import 'package:wger/l10n/generated/app_localizations.dart';
27+
28+
import '../../../../test_data/measurements.dart';
29+
import '../providers/measurement_notifier_test.mocks.dart';
30+
31+
Widget _wrap(MockMeasurementRepository mockRepo) {
32+
return ProviderScope(
33+
overrides: [
34+
measurementRepositoryProvider.overrideWithValue(mockRepo),
35+
],
36+
child: MaterialApp(
37+
localizationsDelegates: AppLocalizations.localizationsDelegates,
38+
supportedLocales: AppLocalizations.supportedLocales,
39+
home: Scaffold(
40+
body: SingleChildScrollView(
41+
child: GroupMeasurementEntryForm(testMeasurementCategoryBloodPressure),
42+
),
43+
),
44+
),
45+
);
46+
}
47+
48+
void main() {
49+
late MockMeasurementRepository mockRepo;
50+
51+
setUp(() {
52+
mockRepo = MockMeasurementRepository();
53+
when(mockRepo.watchAll()).thenAnswer((_) => Stream.value([]));
54+
when(mockRepo.addLocalDriftGroupEntries(any)).thenAnswer((_) async {});
55+
});
56+
57+
group('GroupMeasurementEntryForm', () {
58+
testWidgets('renders one DecimalInputWidget per child component', (tester) async {
59+
await tester.pumpWidget(_wrap(mockRepo));
60+
await tester.pumpAndSettle();
61+
62+
expect(find.byType(DecimalInputWidget), findsNWidgets(2));
63+
});
64+
65+
testWidgets('empty value fields fail validation', (tester) async {
66+
await tester.pumpWidget(_wrap(mockRepo));
67+
await tester.pumpAndSettle();
68+
69+
// Tap save without entering values
70+
await tester.tap(find.byType(ElevatedButton));
71+
await tester.pumpAndSettle();
72+
73+
// Form doesnot pop
74+
expect(find.byType(GroupMeasurementEntryForm), findsOneWidget);
75+
});
76+
77+
testWidgets('valid submission calls addGroupEntries with correct count', (tester) async {
78+
await tester.pumpWidget(_wrap(mockRepo));
79+
await tester.pumpAndSettle();
80+
81+
final fields = find.byType(TextFormField);
82+
// Fields : Date, Time, Systolic value, Diastolic value
83+
await tester.enterText(fields.at(2), '120');
84+
await tester.enterText(fields.at(3), '80');
85+
86+
await tester.tap(find.byType(ElevatedButton));
87+
await tester.pumpAndSettle();
88+
89+
verify(
90+
mockRepo.addLocalDriftGroupEntries(
91+
argThat(hasLength(2)),
92+
),
93+
).called(1);
94+
});
95+
});
96+
}

test_data/measurements.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ final testMeasurementCategoryBloodPressure = MeasurementCategory(
8484
name: 'Blood pressure',
8585
unit: 'mmHg',
8686
metricType: MetricType.bloodPressure,
87+
children: [testMeasurementCategorySystolic, testMeasurementCategoryDiastolic],
8788
);
8889

8990
final testMeasurementCategorySystolic = MeasurementCategory(

0 commit comments

Comments
 (0)