Skip to content

Commit bc0672d

Browse files
committed
Correctly initialize the data in form from existing session
Also, add some simple tests for this
1 parent 8acde9a commit bc0672d

File tree

6 files changed

+606
-11
lines changed

6 files changed

+606
-11
lines changed

lib/models/workouts/session.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class WorkoutSession {
3636
@JsonKey(required: true, toJson: dateToYYYYMMDD)
3737
late DateTime date;
3838

39-
@JsonKey(required: true, fromJson: stringToNum, toJson: numToString)
40-
late num impression;
39+
@JsonKey(required: true, fromJson: int.parse, toJson: numToString)
40+
late int impression;
4141

4242
@JsonKey(required: false, defaultValue: '')
4343
late String notes;

lib/models/workouts/session.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/widgets/routines/gym_mode/session_page.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* You should have received a copy of the GNU Affero General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18+
import 'package:clock/clock.dart';
1819
import 'package:flutter/material.dart';
1920
import 'package:provider/provider.dart' as provider;
2021
import 'package:wger/exceptions/http_exception.dart';
@@ -33,23 +34,22 @@ class SessionPage extends StatefulWidget {
3334
final Routine _routine;
3435
late WorkoutSession _session;
3536
final PageController _controller;
36-
final TimeOfDay _start;
3737
final Map<Exercise, int> _exercisePages;
3838

3939
SessionPage(
4040
this._routine,
4141
this._controller,
42-
this._start,
42+
start,
4343
this._exercisePages,
4444
) {
4545
_session = _routine.sessions.map((sessionApi) => sessionApi.session).firstWhere(
46-
(session) => session.date.isSameDayAs(DateTime.now()),
46+
(session) => session.date.isSameDayAs(clock.now()),
4747
orElse: () => WorkoutSession(
4848
routineId: _routine.id!,
4949
impression: DEFAULT_IMPRESSION,
50-
date: DateTime.now(),
50+
date: clock.now(),
5151
timeEnd: TimeOfDay.now(),
52-
timeStart: _start,
52+
timeStart: start,
5353
),
5454
);
5555
}
@@ -65,17 +65,18 @@ class _SessionPageState extends State<SessionPage> {
6565
final timeStartController = TextEditingController();
6666
final timeEndController = TextEditingController();
6767

68-
// final _session = WorkoutSession.now();
69-
7068
/// Selected impression: bad, neutral, good
71-
var selectedImpression = [false, true, false];
69+
var selectedImpression = [false, false, false];
7270

7371
@override
7472
void initState() {
7573
super.initState();
7674

7775
timeStartController.text = timeToString(widget._session.timeStart)!;
7876
timeEndController.text = timeToString(widget._session.timeEnd)!;
77+
notesController.text = widget._session.notes;
78+
79+
selectedImpression[widget._session.impression - 1] = true;
7980
}
8081

8182
@override
@@ -212,6 +213,7 @@ class _SessionPageState extends State<SessionPage> {
212213
],
213214
),
214215
ElevatedButton(
216+
key: const ValueKey('save-button'),
215217
child: Text(AppLocalizations.of(context).save),
216218
onPressed: () async {
217219
// Validate and save the current values to the weightEntry

test/workout/gym_mode_screen_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ void main() {
269269
findsNWidgets(2),
270270
reason: 'start and end time are the same',
271271
);
272+
final toggleButtons = tester.widget<ToggleButtons>(find.byType(ToggleButtons));
273+
expect(toggleButtons.isSelected[1], isTrue);
272274
expect(find.byIcon(Icons.chevron_left), findsOneWidget);
273275
expect(find.byIcon(Icons.close), findsOneWidget);
274276
expect(find.byIcon(Icons.chevron_right), findsNothing);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This file is part of wger Workout Manager <https://github.com/wger-project>.
3+
* Copyright (C) wger Team
4+
*
5+
* wger Workout Manager is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* wger Workout Manager is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
import 'package:clock/clock.dart';
20+
import 'package:flutter/material.dart';
21+
import 'package:flutter_test/flutter_test.dart';
22+
import 'package:mockito/annotations.dart';
23+
import 'package:mockito/mockito.dart';
24+
import 'package:provider/provider.dart';
25+
import 'package:wger/l10n/generated/app_localizations.dart';
26+
import 'package:wger/providers/routines.dart';
27+
import 'package:wger/widgets/routines/gym_mode/session_page.dart';
28+
29+
import '../../test_data/routines.dart';
30+
import 'gym_mode_session_screen_test.mocks.dart';
31+
32+
@GenerateMocks([RoutinesProvider])
33+
void main() {
34+
final mockRoutinesProvider = MockRoutinesProvider();
35+
36+
final testRoutine = getTestRoutine();
37+
38+
Widget renderSessionPage({locale = 'en'}) {
39+
return ChangeNotifierProvider<RoutinesProvider>(
40+
create: (context) => mockRoutinesProvider,
41+
child: MaterialApp(
42+
locale: Locale(locale),
43+
localizationsDelegates: AppLocalizations.localizationsDelegates,
44+
supportedLocales: AppLocalizations.supportedLocales,
45+
home: Scaffold(
46+
body: SessionPage(
47+
testRoutine,
48+
PageController(),
49+
const TimeOfDay(hour: 12, minute: 34),
50+
const {},
51+
),
52+
),
53+
),
54+
);
55+
}
56+
57+
testWidgets('Test that data from session is loaded', (WidgetTester tester) async {
58+
withClock(Clock.fixed(DateTime(2021, 5, 1)), () async {
59+
await tester.pumpWidget(renderSessionPage());
60+
expect(find.text('10:00'), findsOneWidget);
61+
expect(find.text('12:34'), findsOneWidget);
62+
expect(find.text('This is a note'), findsOneWidget);
63+
final toggleButtons = tester.widget<ToggleButtons>(find.byType(ToggleButtons));
64+
expect(toggleButtons.isSelected[2], isTrue);
65+
});
66+
});
67+
68+
testWidgets('Test that correct data is send to server', (WidgetTester tester) async {
69+
withClock(Clock.fixed(DateTime(2021, 5, 1)), () async {
70+
await tester.pumpWidget(renderSessionPage());
71+
await tester.tap(find.byKey(const ValueKey('save-button')));
72+
final captured = verify(mockRoutinesProvider.editSession(captureAny)).captured.single;
73+
74+
expect(captured.id, 1);
75+
expect(captured.impression, 3);
76+
expect(captured.notes, equals('This is a note'));
77+
expect(captured.timeStart, equals(const TimeOfDay(hour: 10, minute: 0)));
78+
expect(captured.timeEnd, equals(const TimeOfDay(hour: 12, minute: 34)));
79+
});
80+
});
81+
}

0 commit comments

Comments
 (0)