Files
flutter/test/features/weight/widgets/forms_test.dart
2026-07-01 20:28:06 +02:00

141 lines
5.3 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (C) 2020, 2021 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.
*
* wger Workout Manager 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_riverpod/flutter_riverpod.dart';
import 'package:flutter_riverpod/misc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:wger/core/consts.dart';
import 'package:wger/features/weight/models/weight_entry.dart';
import 'package:wger/features/weight/providers/body_weight_repository.dart';
import 'package:wger/features/weight/widgets/forms.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/body_weight.dart';
import 'forms_test.mocks.dart';
@GenerateMocks([BodyWeightRepository])
void main() {
Widget createWeightForm({
locale = 'en',
weightEntry = WeightEntry,
List<Override> overrides = const [],
}) {
return ProviderScope(
overrides: overrides,
child: MaterialApp(
locale: Locale(locale),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(body: WeightForm(weightEntry)),
),
);
}
testWidgets('Correctly prefills and localizes the data - en', (WidgetTester tester) async {
await tester.pumpWidget(createWeightForm(weightEntry: testWeightEntry1));
await tester.pumpAndSettle();
expect(find.text('1/1/2021'), findsOneWidget);
expect(find.text('3:30 PM'), findsOneWidget);
expect(find.text('80'), findsOneWidget);
});
testWidgets('Correctly prefills and localizes the data - de', (WidgetTester tester) async {
await tester.pumpWidget(createWeightForm(weightEntry: testWeightEntry1, locale: 'de'));
await tester.pumpAndSettle();
expect(find.text('1.1.2021'), findsOneWidget);
expect(find.text('15:30'), findsOneWidget);
expect(find.text('80'), findsOneWidget);
});
testWidgets('It is possible to quick-change the weight', (WidgetTester tester) async {
await tester.pumpWidget(createWeightForm(weightEntry: testWeightEntry1));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('quickMinus')));
expect(find.text('79'), findsOneWidget);
await tester.tap(find.byKey(const Key('quickMinusSmall')));
expect(find.text('78.9'), findsOneWidget);
await tester.tap(find.byKey(const Key('quickPlus')));
expect(find.text('79.9'), findsOneWidget);
await tester.tap(find.byKey(const Key('quickPlusSmall')));
expect(find.text('80'), findsOneWidget);
});
testWidgets('Non-numeric input never reaches the field', (WidgetTester tester) async {
await tester.pumpWidget(createWeightForm(weightEntry: testWeightEntry1));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('weightInput')), 'shiba inu');
await tester.pumpAndSettle();
expect(find.text('shiba inu'), findsNothing);
// Quick-change still works on the now-empty field
await tester.tap(find.byKey(const Key('quickMinus')));
await tester.pumpAndSettle();
expect(find.text('shiba inu'), findsNothing);
});
testWidgets('Accepts a dot as decimal separator in the de locale', (WidgetTester tester) async {
await tester.pumpWidget(createWeightForm(weightEntry: null, locale: 'de'));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('weightInput')), '81.5');
await tester.pumpAndSettle();
expect(find.text('81,5'), findsOneWidget);
});
testWidgets('Widget works if there is no last entry', (WidgetTester tester) async {
await tester.pumpWidget(createWeightForm(weightEntry: null));
await tester.pumpAndSettle();
});
testWidgets('Saving keeps an afternoon (PM) time intact', (WidgetTester tester) async {
final mockRepo = MockBodyWeightRepository();
when(mockRepo.watchAllDrift()).thenAnswer((_) => Stream.value(<WeightEntry>[]));
when(mockRepo.updateLocalDrift(any)).thenAnswer((_) async {});
await tester.pumpWidget(
createWeightForm(
weightEntry: testWeightEntry1,
overrides: [bodyWeightRepositoryProvider.overrideWithValue(mockRepo)],
),
);
await tester.pumpAndSettle();
// The stored 15:30 renders as a PM time in a 12-hour locale
expect(find.text('3:30 PM'), findsOneWidget);
// Save without changing anything
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
await tester.pumpAndSettle();
final saved = verify(mockRepo.updateLocalDrift(captureAny)).captured.single as WeightEntry;
expect(saved.date.hour, 15);
expect(saved.date.minute, 30);
});
}