mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-05 04:57:37 +01:00
Add tests for DayFormWidget
This commit is contained in:
@@ -31,3 +31,6 @@ const DEFAULT_REPETITION_UNIT = 1;
|
||||
|
||||
/// Time to locally cache values such as ingredients, etc
|
||||
const DAYS_TO_CACHE = 20;
|
||||
|
||||
/// Name of the submit button in forms
|
||||
const SUBMIT_BUTTON_KEY_NAME = 'submit-button';
|
||||
|
||||
@@ -31,10 +31,10 @@ class WorkoutPlan {
|
||||
int? id;
|
||||
|
||||
@JsonKey(required: true, name: 'creation_date')
|
||||
DateTime creationDate;
|
||||
late DateTime creationDate;
|
||||
|
||||
@JsonKey(required: true, name: 'comment')
|
||||
String description;
|
||||
late String description;
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
List<Day> days = [];
|
||||
@@ -53,6 +53,8 @@ class WorkoutPlan {
|
||||
this.logs = logs ?? [];
|
||||
}
|
||||
|
||||
WorkoutPlan.empty();
|
||||
|
||||
// Boilerplate
|
||||
factory WorkoutPlan.fromJson(Map<String, dynamic> json) => _$WorkoutPlanFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$WorkoutPlanToJson(this);
|
||||
|
||||
@@ -20,6 +20,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:flutter_typeahead/flutter_typeahead.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
import 'package:wger/models/workouts/repetition_unit.dart';
|
||||
@@ -68,7 +69,7 @@ class WorkoutForm extends StatelessWidget {
|
||||
},
|
||||
),
|
||||
ElevatedButton(
|
||||
key: Key('submit-button'),
|
||||
key: Key(SUBMIT_BUTTON_KEY_NAME),
|
||||
child: Text(AppLocalizations.of(context)!.save),
|
||||
onPressed: () async {
|
||||
// Validate and save
|
||||
@@ -111,6 +112,7 @@ class _DayCheckboxState extends State<DayCheckbox> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CheckboxListTile(
|
||||
key: Key('field-checkbox-${widget._dayNr}'),
|
||||
title: Text(widget._day.getDayName(widget._dayNr)),
|
||||
value: _isSelected,
|
||||
onChanged: (bool? newValue) {
|
||||
@@ -151,6 +153,7 @@ class _DayFormWidgetState extends State<DayFormWidget> {
|
||||
child: ListView(
|
||||
children: [
|
||||
TextFormField(
|
||||
key: Key('field-description'),
|
||||
decoration: InputDecoration(
|
||||
labelText: AppLocalizations.of(context)!.description,
|
||||
helperText: AppLocalizations.of(context)!.dayDescriptionHelp,
|
||||
@@ -166,12 +169,17 @@ class _DayFormWidgetState extends State<DayFormWidget> {
|
||||
if (value!.isEmpty || value.length < minLength || value.length > maxLength) {
|
||||
return AppLocalizations.of(context)!.enterCharacters(minLength, maxLength);
|
||||
}
|
||||
|
||||
if (widget._day.daysOfWeek.length == 0) {
|
||||
return 'You need to select at least one day';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
...Day.weekdays.keys.map((dayNr) => DayCheckbox(dayNr, widget._day)).toList(),
|
||||
ElevatedButton(
|
||||
key: Key(SUBMIT_BUTTON_KEY_NAME),
|
||||
child: Text(AppLocalizations.of(context)!.save),
|
||||
onPressed: () async {
|
||||
if (!_form.currentState!.validate()) {
|
||||
|
||||
85
test/workout_day_form_test.dart
Normal file
85
test/workout_day_form_test.dart
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
import 'package:wger/models/workouts/workout_plan.dart';
|
||||
import 'package:wger/providers/workout_plans.dart';
|
||||
import 'package:wger/widgets/workouts/forms.dart';
|
||||
|
||||
import '../test_data/workouts.dart';
|
||||
import 'workout_form_test.mocks.dart';
|
||||
|
||||
void main() {
|
||||
var mockWorkoutPlans = MockWorkoutPlans();
|
||||
WorkoutPlan workoutPlan = WorkoutPlan.empty();
|
||||
|
||||
setUp(() {
|
||||
workoutPlan = getWorkout();
|
||||
mockWorkoutPlans = MockWorkoutPlans();
|
||||
});
|
||||
|
||||
Widget createHomeScreen(WorkoutPlan workoutPlan, {locale = 'en'}) {
|
||||
final key = GlobalKey<NavigatorState>();
|
||||
|
||||
return ChangeNotifierProvider<WorkoutPlans>(
|
||||
create: (context) => mockWorkoutPlans,
|
||||
child: MaterialApp(
|
||||
locale: Locale(locale),
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
navigatorKey: key,
|
||||
home: Scaffold(
|
||||
body: DayFormWidget(workoutPlan),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('Test the widgets on the DayFormWidget', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(createHomeScreen(workoutPlan));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(TextFormField), findsOneWidget);
|
||||
expect(find.byType(CheckboxListTile), findsNWidgets(7));
|
||||
expect(find.byType(ElevatedButton), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Test creating a new day', (WidgetTester tester) async {
|
||||
when(mockWorkoutPlans.addDay(any, any)).thenAnswer((_) => Future.value(Day()));
|
||||
|
||||
await tester.pumpWidget(createHomeScreen(workoutPlan));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(('')), findsOneWidget, reason: 'New day has no description');
|
||||
await tester.enterText(find.byKey(Key('field-description')), 'Leg day!');
|
||||
await tester.tap(find.byKey(Key('field-checkbox-1')));
|
||||
await tester.tap(find.byKey(Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
|
||||
verify(mockWorkoutPlans.addDay(any, any));
|
||||
|
||||
// Successful redirect to workout plan detail page
|
||||
//await tester.pumpAndSettle();
|
||||
//verify(mockObserver.didPop(any, any));
|
||||
});
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/models/workouts/workout_plan.dart';
|
||||
import 'package:wger/providers/workout_plans.dart';
|
||||
import 'package:wger/screens/workout_plan_screen.dart';
|
||||
@@ -76,7 +77,7 @@ void main() {
|
||||
|
||||
expect(find.text(('test 1')), findsOneWidget, reason: 'Description of existing workout plan');
|
||||
await tester.enterText(find.byKey(Key('field-description')), 'New description');
|
||||
await tester.tap(find.byKey(Key('submit-button')));
|
||||
await tester.tap(find.byKey(Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
|
||||
// Correct ed
|
||||
verify(mockWorkoutPlans.editWorkout(any));
|
||||
@@ -96,7 +97,7 @@ void main() {
|
||||
|
||||
expect(find.text(('')), findsOneWidget, reason: 'New workout has no description');
|
||||
await tester.enterText(find.byKey(Key('field-description')), 'New cool workout');
|
||||
await tester.tap(find.byKey(Key('submit-button')));
|
||||
await tester.tap(find.byKey(Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
|
||||
verifyNever(mockWorkoutPlans.editWorkout(any));
|
||||
verify(mockWorkoutPlans.addWorkout(any));
|
||||
|
||||
Reference in New Issue
Block a user