diff --git a/lib/helpers/consts.dart b/lib/helpers/consts.dart index 37f3318ac..8855ed65c 100644 --- a/lib/helpers/consts.dart +++ b/lib/helpers/consts.dart @@ -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'; diff --git a/lib/models/workouts/workout_plan.dart b/lib/models/workouts/workout_plan.dart index 240c25ad9..30d1f1bce 100644 --- a/lib/models/workouts/workout_plan.dart +++ b/lib/models/workouts/workout_plan.dart @@ -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 days = []; @@ -53,6 +53,8 @@ class WorkoutPlan { this.logs = logs ?? []; } + WorkoutPlan.empty(); + // Boilerplate factory WorkoutPlan.fromJson(Map json) => _$WorkoutPlanFromJson(json); Map toJson() => _$WorkoutPlanToJson(this); diff --git a/lib/widgets/workouts/forms.dart b/lib/widgets/workouts/forms.dart index 26bfddf72..c5b8ac89c 100644 --- a/lib/widgets/workouts/forms.dart +++ b/lib/widgets/workouts/forms.dart @@ -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 { @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 { 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 { 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()) { diff --git a/test/workout_day_form_test.dart b/test/workout_day_form_test.dart new file mode 100644 index 000000000..86e05cf16 --- /dev/null +++ b/test/workout_day_form_test.dart @@ -0,0 +1,85 @@ +/* + * This file is part of wger Workout Manager . + * 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 . + */ + +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(); + + return ChangeNotifierProvider( + 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)); + }); +} diff --git a/test/workout_form_test.dart b/test/workout_form_test.dart index 76aa4bacc..92b895192 100644 --- a/test/workout_form_test.dart +++ b/test/workout_form_test.dart @@ -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));