From 2424199e09f367fdc452f05772f54737c42e295f Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 23 Apr 2021 12:47:12 +0200 Subject: [PATCH] Add tests for PlanForm --- lib/models/workouts/set.dart | 2 + lib/models/workouts/workout_plan.dart | 5 +- lib/screens/workout_plans_screen.dart | 2 +- lib/widgets/dashboard/widgets.dart | 2 +- lib/widgets/nutrition/forms.dart | 3 + lib/widgets/workouts/forms.dart | 2 + test/nutritional_plan_form_test.dart | 121 +++++++++++++ test/nutritional_plan_form_test.mocks.dart | 201 +++++++++++++++++++++ test/workout_day_form_test.dart | 6 +- 9 files changed, 338 insertions(+), 6 deletions(-) create mode 100644 test/nutritional_plan_form_test.dart create mode 100644 test/nutritional_plan_form_test.mocks.dart diff --git a/lib/models/workouts/set.dart b/lib/models/workouts/set.dart index 8753a094f..d4ebb2011 100644 --- a/lib/models/workouts/set.dart +++ b/lib/models/workouts/set.dart @@ -58,6 +58,8 @@ class Set { this.order, }); + Set.empty(); + Set.withData({ this.id, sets, diff --git a/lib/models/workouts/workout_plan.dart b/lib/models/workouts/workout_plan.dart index 30d1f1bce..f6c502c81 100644 --- a/lib/models/workouts/workout_plan.dart +++ b/lib/models/workouts/workout_plan.dart @@ -53,7 +53,10 @@ class WorkoutPlan { this.logs = logs ?? []; } - WorkoutPlan.empty(); + WorkoutPlan.empty() { + creationDate = DateTime.now(); + description = ''; + } // Boilerplate factory WorkoutPlan.fromJson(Map json) => _$WorkoutPlanFromJson(json); diff --git a/lib/screens/workout_plans_screen.dart b/lib/screens/workout_plans_screen.dart index 8fc20fb77..3edbe030c 100644 --- a/lib/screens/workout_plans_screen.dart +++ b/lib/screens/workout_plans_screen.dart @@ -49,7 +49,7 @@ class _WorkoutPlansScreenState extends State { FormScreen.routeName, arguments: FormScreenArguments( AppLocalizations.of(context)!.newWorkout, - WorkoutForm(WorkoutPlan(creationDate: DateTime.now(), description: '')), + WorkoutForm(WorkoutPlan.empty()), ), ); }, diff --git a/lib/widgets/dashboard/widgets.dart b/lib/widgets/dashboard/widgets.dart index 1641147e3..75a84a82f 100644 --- a/lib/widgets/dashboard/widgets.dart +++ b/lib/widgets/dashboard/widgets.dart @@ -362,7 +362,7 @@ class _DashboardWorkoutWidgetState extends State { : NothingFound( AppLocalizations.of(context)!.noWorkoutPlans, AppLocalizations.of(context)!.newWorkout, - WorkoutForm(WorkoutPlan(creationDate: DateTime.now(), description: '')), + WorkoutForm(WorkoutPlan.empty()), ), Row( mainAxisAlignment: MainAxisAlignment.end, diff --git a/lib/widgets/nutrition/forms.dart b/lib/widgets/nutrition/forms.dart index 1071e276c..b3dd13cb1 100644 --- a/lib/widgets/nutrition/forms.dart +++ b/lib/widgets/nutrition/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/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 diff --git a/lib/widgets/workouts/forms.dart b/lib/widgets/workouts/forms.dart index c5b8ac89c..a7e2714b5 100644 --- a/lib/widgets/workouts/forms.dart +++ b/lib/widgets/workouts/forms.dart @@ -281,6 +281,7 @@ class _SetFormWidgetState extends State { child: ListView( children: [ TypeAheadFormField( + key: Key('field-typeahead'), textFieldConfiguration: TextFieldConfiguration( controller: this._exercisesController, decoration: InputDecoration( @@ -378,6 +379,7 @@ class _SetFormWidgetState extends State { ); }).toList(), ElevatedButton( + key: Key(SUBMIT_BUTTON_KEY_NAME), child: Text(AppLocalizations.of(context)!.save), onPressed: () async { final isValid = _formKey.currentState!.validate(); diff --git a/test/nutritional_plan_form_test.dart b/test/nutritional_plan_form_test.dart new file mode 100644 index 000000000..a9aedd2df --- /dev/null +++ b/test/nutritional_plan_form_test.dart @@ -0,0 +1,121 @@ +/* + * 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/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(); + + return ChangeNotifierProvider( + 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'); + }); +} diff --git a/test/nutritional_plan_form_test.mocks.dart b/test/nutritional_plan_form_test.mocks.dart new file mode 100644 index 000000000..60c42570c --- /dev/null +++ b/test/nutritional_plan_form_test.mocks.dart @@ -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 fetchAndSetAllPlans() => + (super.noSuchMethod(Invocation.method(#fetchAndSetAllPlans, []), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @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 editPlan(_i4.NutritionalPlan? plan) => + (super.noSuchMethod(Invocation.method(#editPlan, [plan]), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @override + _i10.Future deletePlan(int? id) => + (super.noSuchMethod(Invocation.method(#deletePlan, [id]), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @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 deleteMeal(_i5.Meal? meal) => + (super.noSuchMethod(Invocation.method(#deleteMeal, [meal]), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @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 deleteMealItem(_i6.MealItem? mealItem) => + (super.noSuchMethod(Invocation.method(#deleteMealItem, [mealItem]), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @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 fetchIngredientsFromCache() => + (super.noSuchMethod(Invocation.method(#fetchIngredientsFromCache, []), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @override + _i10.Future> searchIngredient(String? name, + [String? languageCode = r'en']) => + (super.noSuchMethod( + Invocation.method(#searchIngredient, [name, languageCode]), + returnValue: Future>.value([])) + as _i10.Future>); + @override + _i10.Future logMealToDiary(_i5.Meal? meal) => + (super.noSuchMethod(Invocation.method(#logMealToDiary, [meal]), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @override + _i10.Future fetchAndSetAllLogs() => + (super.noSuchMethod(Invocation.method(#fetchAndSetAllLogs, []), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @override + _i10.Future fetchAndSetLogs(_i4.NutritionalPlan? plan) => + (super.noSuchMethod(Invocation.method(#fetchAndSetLogs, [plan]), + returnValue: Future.value(null), + returnValueForMissingStub: Future.value()) as _i10.Future); + @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? query}) => + super.noSuchMethod(Invocation.method(#makeUrl, [path], + {#id: id, #objectMethod: objectMethod, #query: query})); + @override + _i10.Future> fetch(Uri? uri) => (super.noSuchMethod( + Invocation.method(#fetch, [uri]), + returnValue: Future>.value({})) + as _i10.Future>); + @override + _i10.Future> post( + Map? data, Uri? uri) => + (super.noSuchMethod(Invocation.method(#post, [data, uri]), + returnValue: + Future>.value({})) + as _i10.Future>); + @override + _i10.Future> patch( + Map? data, Uri? uri) => + (super.noSuchMethod(Invocation.method(#patch, [data, uri]), + returnValue: + Future>.value({})) + as _i10.Future>); + @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>); +} diff --git a/test/workout_day_form_test.dart b/test/workout_day_form_test.dart index 86e05cf16..6fce805ab 100644 --- a/test/workout_day_form_test.dart +++ b/test/workout_day_form_test.dart @@ -39,7 +39,7 @@ void main() { mockWorkoutPlans = MockWorkoutPlans(); }); - Widget createHomeScreen(WorkoutPlan workoutPlan, {locale = 'en'}) { + Widget createHomeScreen({locale = 'en'}) { final key = GlobalKey(); return ChangeNotifierProvider( @@ -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');