mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-02 10:09:11 +01:00
Add tests for PlanForm
This commit is contained in:
@@ -58,6 +58,8 @@ class Set {
|
||||
this.order,
|
||||
});
|
||||
|
||||
Set.empty();
|
||||
|
||||
Set.withData({
|
||||
this.id,
|
||||
sets,
|
||||
|
||||
@@ -53,7 +53,10 @@ class WorkoutPlan {
|
||||
this.logs = logs ?? [];
|
||||
}
|
||||
|
||||
WorkoutPlan.empty();
|
||||
WorkoutPlan.empty() {
|
||||
creationDate = DateTime.now();
|
||||
description = '';
|
||||
}
|
||||
|
||||
// Boilerplate
|
||||
factory WorkoutPlan.fromJson(Map<String, dynamic> json) => _$WorkoutPlanFromJson(json);
|
||||
|
||||
@@ -49,7 +49,7 @@ class _WorkoutPlansScreenState extends State<WorkoutPlansScreen> {
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context)!.newWorkout,
|
||||
WorkoutForm(WorkoutPlan(creationDate: DateTime.now(), description: '')),
|
||||
WorkoutForm(WorkoutPlan.empty()),
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -362,7 +362,7 @@ class _DashboardWorkoutWidgetState extends State<DashboardWorkoutWidget> {
|
||||
: NothingFound(
|
||||
AppLocalizations.of(context)!.noWorkoutPlans,
|
||||
AppLocalizations.of(context)!.newWorkout,
|
||||
WorkoutForm(WorkoutPlan(creationDate: DateTime.now(), description: '')),
|
||||
WorkoutForm(WorkoutPlan.empty()),
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
|
||||
@@ -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/helpers/json.dart';
|
||||
import 'package:wger/helpers/ui.dart';
|
||||
import 'package:wger/models/http_exception.dart';
|
||||
@@ -208,6 +209,7 @@ class PlanForm extends StatelessWidget {
|
||||
children: [
|
||||
// Description
|
||||
TextFormField(
|
||||
key: Key('field-description'),
|
||||
decoration: InputDecoration(labelText: AppLocalizations.of(context)!.description),
|
||||
controller: _descriptionController,
|
||||
onFieldSubmitted: (_) {},
|
||||
@@ -216,6 +218,7 @@ class PlanForm extends StatelessWidget {
|
||||
},
|
||||
),
|
||||
ElevatedButton(
|
||||
key: Key(SUBMIT_BUTTON_KEY_NAME),
|
||||
child: Text(AppLocalizations.of(context)!.save),
|
||||
onPressed: () async {
|
||||
// Validate and save the current values to the weightEntry
|
||||
|
||||
@@ -281,6 +281,7 @@ class _SetFormWidgetState extends State<SetFormWidget> {
|
||||
child: ListView(
|
||||
children: [
|
||||
TypeAheadFormField(
|
||||
key: Key('field-typeahead'),
|
||||
textFieldConfiguration: TextFieldConfiguration(
|
||||
controller: this._exercisesController,
|
||||
decoration: InputDecoration(
|
||||
@@ -378,6 +379,7 @@ class _SetFormWidgetState extends State<SetFormWidget> {
|
||||
);
|
||||
}).toList(),
|
||||
ElevatedButton(
|
||||
key: Key(SUBMIT_BUTTON_KEY_NAME),
|
||||
child: Text(AppLocalizations.of(context)!.save),
|
||||
onPressed: () async {
|
||||
final isValid = _formKey.currentState!.validate();
|
||||
|
||||
121
test/nutritional_plan_form_test.dart
Normal file
121
test/nutritional_plan_form_test.dart
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* 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/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/models/nutrition/nutritional_plan.dart';
|
||||
import 'package:wger/providers/nutrition.dart';
|
||||
import 'package:wger/screens/nutritional_plan_screen.dart';
|
||||
import 'package:wger/widgets/nutrition/forms.dart';
|
||||
|
||||
import 'nutritional_plan_form_test.mocks.dart';
|
||||
|
||||
@GenerateMocks([Nutrition])
|
||||
void main() {
|
||||
var mockNutrition = MockNutrition();
|
||||
|
||||
final plan1 = NutritionalPlan(
|
||||
id: 1,
|
||||
creationDate: DateTime(2021, 1, 1),
|
||||
description: 'test plan 1',
|
||||
);
|
||||
final plan2 = NutritionalPlan.empty();
|
||||
|
||||
when(mockNutrition.editPlan(any)).thenAnswer((_) => Future.value(plan1));
|
||||
when(mockNutrition.addPlan(any)).thenAnswer((_) => Future.value(plan2));
|
||||
|
||||
setUp(() {
|
||||
mockNutrition = MockNutrition();
|
||||
});
|
||||
|
||||
Widget createHomeScreen(NutritionalPlan plan, {locale = 'en'}) {
|
||||
final key = GlobalKey<NavigatorState>();
|
||||
|
||||
return ChangeNotifierProvider<Nutrition>(
|
||||
create: (context) => mockNutrition,
|
||||
child: MaterialApp(
|
||||
locale: Locale(locale),
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
navigatorKey: key,
|
||||
home: Scaffold(
|
||||
body: PlanForm(plan),
|
||||
),
|
||||
routes: {
|
||||
NutritionalPlanScreen.routeName: (ctx) => NutritionalPlanScreen(),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('Test the widgets on the nutritional plan form', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(createHomeScreen(plan1));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(TextFormField), findsOneWidget);
|
||||
expect(find.byType(ElevatedButton), findsOneWidget);
|
||||
expect(find.byKey(Key(SUBMIT_BUTTON_KEY_NAME)), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Test editing an existing nutritional plan', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(createHomeScreen(plan1));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
find.text(('test plan 1')),
|
||||
findsOneWidget,
|
||||
reason: 'Description of existing nutritional plan is filled in',
|
||||
);
|
||||
await tester.enterText(find.byKey(Key('field-description')), 'New description');
|
||||
await tester.tap(find.byKey(Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
|
||||
// Correct method was called
|
||||
verify(mockNutrition.editPlan(any));
|
||||
verifyNever(mockNutrition.addPlan(any));
|
||||
|
||||
// Detail page
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.text(('New description')),
|
||||
findsOneWidget,
|
||||
reason: 'Nutritional plan detail page',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('Test creating a new nutritional plan', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(createHomeScreen(plan2));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(('')), findsOneWidget, reason: 'New nutritional plan has no description');
|
||||
await tester.enterText(find.byKey(Key('field-description')), 'New cool plan');
|
||||
await tester.tap(find.byKey(Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
|
||||
// Correct method was called
|
||||
verifyNever(mockNutrition.editPlan(any));
|
||||
verify(mockNutrition.addPlan(any));
|
||||
|
||||
// Detail page
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text(('New cool plan')), findsOneWidget, reason: 'Nutritional plan detail page');
|
||||
});
|
||||
}
|
||||
201
test/nutritional_plan_form_test.mocks.dart
Normal file
201
test/nutritional_plan_form_test.mocks.dart
Normal file
@@ -0,0 +1,201 @@
|
||||
// Mocks generated by Mockito 5.0.5 from annotations
|
||||
// in wger/test/nutritional_plan_form_test.dart.
|
||||
// Do not manually edit this file.
|
||||
|
||||
import 'dart:async' as _i10;
|
||||
import 'dart:ui' as _i11;
|
||||
|
||||
import 'package:http/src/client.dart' as _i3;
|
||||
import 'package:http/src/response.dart' as _i8;
|
||||
import 'package:mockito/mockito.dart' as _i1;
|
||||
import 'package:wger/models/nutrition/ingredient.dart' as _i7;
|
||||
import 'package:wger/models/nutrition/meal.dart' as _i5;
|
||||
import 'package:wger/models/nutrition/meal_item.dart' as _i6;
|
||||
import 'package:wger/models/nutrition/nutritional_plan.dart' as _i4;
|
||||
import 'package:wger/providers/auth.dart' as _i2;
|
||||
import 'package:wger/providers/nutrition.dart' as _i9;
|
||||
|
||||
// ignore_for_file: comment_references
|
||||
// ignore_for_file: unnecessary_parenthesis
|
||||
|
||||
class _FakeAuth extends _i1.Fake implements _i2.Auth {}
|
||||
|
||||
class _FakeClient extends _i1.Fake implements _i3.Client {}
|
||||
|
||||
class _FakeNutritionalPlan extends _i1.Fake implements _i4.NutritionalPlan {}
|
||||
|
||||
class _FakeMeal extends _i1.Fake implements _i5.Meal {}
|
||||
|
||||
class _FakeMealItem extends _i1.Fake implements _i6.MealItem {}
|
||||
|
||||
class _FakeIngredient extends _i1.Fake implements _i7.Ingredient {}
|
||||
|
||||
class _FakeResponse extends _i1.Fake implements _i8.Response {}
|
||||
|
||||
/// A class which mocks [Nutrition].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockNutrition extends _i1.Mock implements _i9.Nutrition {
|
||||
MockNutrition() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
List<_i4.NutritionalPlan> get items =>
|
||||
(super.noSuchMethod(Invocation.getter(#items),
|
||||
returnValue: <_i4.NutritionalPlan>[]) as List<_i4.NutritionalPlan>);
|
||||
@override
|
||||
bool get hasListeners =>
|
||||
(super.noSuchMethod(Invocation.getter(#hasListeners), returnValue: false)
|
||||
as bool);
|
||||
@override
|
||||
_i2.Auth get auth =>
|
||||
(super.noSuchMethod(Invocation.getter(#auth), returnValue: _FakeAuth())
|
||||
as _i2.Auth);
|
||||
@override
|
||||
set auth(_i2.Auth? _auth) =>
|
||||
super.noSuchMethod(Invocation.setter(#auth, _auth),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
_i3.Client get client => (super.noSuchMethod(Invocation.getter(#client),
|
||||
returnValue: _FakeClient()) as _i3.Client);
|
||||
@override
|
||||
set client(_i3.Client? _client) =>
|
||||
super.noSuchMethod(Invocation.setter(#client, _client),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
_i4.NutritionalPlan findById(int? id) =>
|
||||
(super.noSuchMethod(Invocation.method(#findById, [id]),
|
||||
returnValue: _FakeNutritionalPlan()) as _i4.NutritionalPlan);
|
||||
@override
|
||||
_i5.Meal? findMealById(int? id) =>
|
||||
(super.noSuchMethod(Invocation.method(#findMealById, [id])) as _i5.Meal?);
|
||||
@override
|
||||
_i10.Future<void> fetchAndSetAllPlans() =>
|
||||
(super.noSuchMethod(Invocation.method(#fetchAndSetAllPlans, []),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
_i10.Future<_i4.NutritionalPlan> fetchAndSetPlan(int? planId) =>
|
||||
(super.noSuchMethod(Invocation.method(#fetchAndSetPlan, [planId]),
|
||||
returnValue:
|
||||
Future<_i4.NutritionalPlan>.value(_FakeNutritionalPlan()))
|
||||
as _i10.Future<_i4.NutritionalPlan>);
|
||||
@override
|
||||
_i10.Future<_i4.NutritionalPlan> addPlan(_i4.NutritionalPlan? planData) =>
|
||||
(super.noSuchMethod(Invocation.method(#addPlan, [planData]),
|
||||
returnValue:
|
||||
Future<_i4.NutritionalPlan>.value(_FakeNutritionalPlan()))
|
||||
as _i10.Future<_i4.NutritionalPlan>);
|
||||
@override
|
||||
_i10.Future<void> editPlan(_i4.NutritionalPlan? plan) =>
|
||||
(super.noSuchMethod(Invocation.method(#editPlan, [plan]),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
_i10.Future<void> deletePlan(int? id) =>
|
||||
(super.noSuchMethod(Invocation.method(#deletePlan, [id]),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
_i10.Future<_i5.Meal> addMeal(_i5.Meal? meal, int? planId) =>
|
||||
(super.noSuchMethod(Invocation.method(#addMeal, [meal, planId]),
|
||||
returnValue: Future<_i5.Meal>.value(_FakeMeal()))
|
||||
as _i10.Future<_i5.Meal>);
|
||||
@override
|
||||
_i10.Future<_i5.Meal> editMeal(_i5.Meal? meal) =>
|
||||
(super.noSuchMethod(Invocation.method(#editMeal, [meal]),
|
||||
returnValue: Future<_i5.Meal>.value(_FakeMeal()))
|
||||
as _i10.Future<_i5.Meal>);
|
||||
@override
|
||||
_i10.Future<void> deleteMeal(_i5.Meal? meal) =>
|
||||
(super.noSuchMethod(Invocation.method(#deleteMeal, [meal]),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
_i10.Future<_i6.MealItem> addMealItem(_i6.MealItem? mealItem, int? mealId) =>
|
||||
(super.noSuchMethod(Invocation.method(#addMealItem, [mealItem, mealId]),
|
||||
returnValue: Future<_i6.MealItem>.value(_FakeMealItem()))
|
||||
as _i10.Future<_i6.MealItem>);
|
||||
@override
|
||||
_i10.Future<void> deleteMealItem(_i6.MealItem? mealItem) =>
|
||||
(super.noSuchMethod(Invocation.method(#deleteMealItem, [mealItem]),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
_i10.Future<_i7.Ingredient> fetchIngredient(int? ingredientId) =>
|
||||
(super.noSuchMethod(Invocation.method(#fetchIngredient, [ingredientId]),
|
||||
returnValue: Future<_i7.Ingredient>.value(_FakeIngredient()))
|
||||
as _i10.Future<_i7.Ingredient>);
|
||||
@override
|
||||
_i10.Future<void> fetchIngredientsFromCache() =>
|
||||
(super.noSuchMethod(Invocation.method(#fetchIngredientsFromCache, []),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
_i10.Future<List<dynamic>> searchIngredient(String? name,
|
||||
[String? languageCode = r'en']) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#searchIngredient, [name, languageCode]),
|
||||
returnValue: Future<List<dynamic>>.value(<dynamic>[]))
|
||||
as _i10.Future<List<dynamic>>);
|
||||
@override
|
||||
_i10.Future<void> logMealToDiary(_i5.Meal? meal) =>
|
||||
(super.noSuchMethod(Invocation.method(#logMealToDiary, [meal]),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
_i10.Future<void> fetchAndSetAllLogs() =>
|
||||
(super.noSuchMethod(Invocation.method(#fetchAndSetAllLogs, []),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
_i10.Future<void> fetchAndSetLogs(_i4.NutritionalPlan? plan) =>
|
||||
(super.noSuchMethod(Invocation.method(#fetchAndSetLogs, [plan]),
|
||||
returnValue: Future<void>.value(null),
|
||||
returnValueForMissingStub: Future.value()) as _i10.Future<void>);
|
||||
@override
|
||||
void addListener(_i11.VoidCallback? listener) =>
|
||||
super.noSuchMethod(Invocation.method(#addListener, [listener]),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
void removeListener(_i11.VoidCallback? listener) =>
|
||||
super.noSuchMethod(Invocation.method(#removeListener, [listener]),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
void dispose() => super.noSuchMethod(Invocation.method(#dispose, []),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
void notifyListeners() =>
|
||||
super.noSuchMethod(Invocation.method(#notifyListeners, []),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
dynamic makeUrl(String? path,
|
||||
{int? id, String? objectMethod, Map<String, dynamic>? query}) =>
|
||||
super.noSuchMethod(Invocation.method(#makeUrl, [path],
|
||||
{#id: id, #objectMethod: objectMethod, #query: query}));
|
||||
@override
|
||||
_i10.Future<Map<String, dynamic>> fetch(Uri? uri) => (super.noSuchMethod(
|
||||
Invocation.method(#fetch, [uri]),
|
||||
returnValue: Future<Map<String, dynamic>>.value(<String, dynamic>{}))
|
||||
as _i10.Future<Map<String, dynamic>>);
|
||||
@override
|
||||
_i10.Future<Map<String, dynamic>> post(
|
||||
Map<String, dynamic>? data, Uri? uri) =>
|
||||
(super.noSuchMethod(Invocation.method(#post, [data, uri]),
|
||||
returnValue:
|
||||
Future<Map<String, dynamic>>.value(<String, dynamic>{}))
|
||||
as _i10.Future<Map<String, dynamic>>);
|
||||
@override
|
||||
_i10.Future<Map<String, dynamic>> patch(
|
||||
Map<String, dynamic>? data, Uri? uri) =>
|
||||
(super.noSuchMethod(Invocation.method(#patch, [data, uri]),
|
||||
returnValue:
|
||||
Future<Map<String, dynamic>>.value(<String, dynamic>{}))
|
||||
as _i10.Future<Map<String, dynamic>>);
|
||||
@override
|
||||
_i10.Future<_i8.Response> deleteRequest(String? url, int? id) =>
|
||||
(super.noSuchMethod(Invocation.method(#deleteRequest, [url, id]),
|
||||
returnValue: Future<_i8.Response>.value(_FakeResponse()))
|
||||
as _i10.Future<_i8.Response>);
|
||||
}
|
||||
@@ -39,7 +39,7 @@ void main() {
|
||||
mockWorkoutPlans = MockWorkoutPlans();
|
||||
});
|
||||
|
||||
Widget createHomeScreen(WorkoutPlan workoutPlan, {locale = 'en'}) {
|
||||
Widget createHomeScreen({locale = 'en'}) {
|
||||
final key = GlobalKey<NavigatorState>();
|
||||
|
||||
return ChangeNotifierProvider<WorkoutPlans>(
|
||||
@@ -57,7 +57,7 @@ void main() {
|
||||
}
|
||||
|
||||
testWidgets('Test the widgets on the DayFormWidget', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(createHomeScreen(workoutPlan));
|
||||
await tester.pumpWidget(createHomeScreen());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(TextFormField), findsOneWidget);
|
||||
@@ -68,7 +68,7 @@ void main() {
|
||||
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.pumpWidget(createHomeScreen());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(('')), findsOneWidget, reason: 'New day has no description');
|
||||
|
||||
Reference in New Issue
Block a user