mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-05 23:44:02 +01:00
Read the clock instead of the wall clock in meal form and routine
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:wger/core/colors.dart';
|
||||
@@ -49,7 +50,7 @@ class MealForm extends ConsumerWidget {
|
||||
final _nameController = TextEditingController();
|
||||
|
||||
MealForm(this._planId, [meal]) {
|
||||
_meal = meal ?? Meal(plan: _planId, time: TimeOfDay.fromDateTime(DateTime.now()));
|
||||
_meal = meal ?? Meal(plan: _planId, time: TimeOfDay.fromDateTime(clock.now()));
|
||||
_nameController.text = _meal.name;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:drift/drift.dart' as drift;
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wger/core/date.dart';
|
||||
@@ -192,7 +193,7 @@ class Routine {
|
||||
}
|
||||
|
||||
int? getIteration({DateTime? date}) {
|
||||
date ??= DateTime.now();
|
||||
date ??= clock.now();
|
||||
|
||||
for (final data in dayData) {
|
||||
if (data.date.isSameDayAs(date)) {
|
||||
@@ -203,7 +204,7 @@ class Routine {
|
||||
}
|
||||
|
||||
List<DayData> get dayDataCurrentIteration {
|
||||
final iteration = getIteration(date: DateTime.now()) ?? 1;
|
||||
final iteration = getIteration(date: clock.now()) ?? 1;
|
||||
return dayData.where((data) => data.iteration == iteration).toList();
|
||||
}
|
||||
|
||||
@@ -232,7 +233,7 @@ class Routine {
|
||||
}
|
||||
|
||||
List<DayData> get dayDataCurrentIterationGym {
|
||||
final iteration = getIteration(date: DateTime.now()) ?? 1;
|
||||
final iteration = getIteration(date: clock.now()) ?? 1;
|
||||
return dayDataGym.where((data) => data.iteration == iteration).toList();
|
||||
}
|
||||
|
||||
|
||||
@@ -511,6 +511,11 @@ void main() {
|
||||
LogItem(planId: planUuid1, ingredientId: 1, amount: 100, datetime: localDateTime),
|
||||
);
|
||||
|
||||
// The row itself holds the UTC instant, as the server and sync expect
|
||||
final raw = await db.customSelect('SELECT datetime FROM nutrition_logitem').getSingle();
|
||||
expect(raw.read<String>('datetime'), localDateTime.toUtc().toIso8601String());
|
||||
expect(raw.read<String>('datetime'), endsWith('Z'));
|
||||
|
||||
final stored = (await repo.watchAllLogsHydrated().first).single.datetime;
|
||||
|
||||
expect(stored.isUtc, isFalse);
|
||||
|
||||
@@ -82,7 +82,7 @@ void main() {
|
||||
container.dispose();
|
||||
});
|
||||
|
||||
Widget createFormScreen(Meal meal, {locale = 'en'}) {
|
||||
Widget createFormScreen(Meal? meal, {locale = 'en'}) {
|
||||
final key = GlobalKey<NavigatorState>();
|
||||
|
||||
return UncontrolledProviderScope(
|
||||
@@ -133,46 +133,24 @@ void main() {
|
||||
verifyNever(mockRepo.addMealLocalDrift(any));
|
||||
});
|
||||
|
||||
testWidgets('Test creating a new nutritional plan', (WidgetTester tester) async {
|
||||
// DateTime.now() is difficult to mock as it pull directly from the platform
|
||||
// currently being run. The clock pacakge (https://pub.dev/packages/clock)
|
||||
// addresses this issue, using clock.now() allows us to set a fixed value as the
|
||||
// response when using withClock.
|
||||
final fixedDateTimeValue = DateTime(2020, 09, 01, 01, 01);
|
||||
withClock(Clock.fixed(fixedDateTimeValue), () async {
|
||||
// The time set in the meal object is what is displayed by default
|
||||
// and can be matched with the find.text function. By creating the meal
|
||||
// wrapped in the withClock it also shares the same now value.
|
||||
|
||||
// Note: it seems there is something wrong with withClock that seems to
|
||||
// get ignored, so passing the time to the constructor for now
|
||||
final fixedTimeMeal = Meal(time: const TimeOfDay(hour: 1, minute: 1));
|
||||
|
||||
await tester.pumpWidget(createFormScreen(fixedTimeMeal));
|
||||
testWidgets('a new meal defaults to the current time', (WidgetTester tester) async {
|
||||
await withClock(Clock.fixed(DateTime(2020, 09, 01, 01, 01)), () async {
|
||||
// Without a meal the form builds its own, at the time of the clock
|
||||
await tester.pumpWidget(createFormScreen(null));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
clock.now(),
|
||||
fixedDateTimeValue,
|
||||
reason: 'Current time is set to a fixed value for testing',
|
||||
);
|
||||
|
||||
expect(
|
||||
find.text('1:01 AM'),
|
||||
findsOneWidget,
|
||||
reason: 'Current time is filled in',
|
||||
);
|
||||
expect(find.text('1:01 AM'), findsOneWidget, reason: 'Current time is filled in');
|
||||
|
||||
await tester.enterText(find.byKey(const Key('field-name')), 'test meal');
|
||||
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
await tester.pump();
|
||||
|
||||
// Correct method was called
|
||||
verifyNever(mockRepo.editMealLocalDrift(any));
|
||||
verify(mockRepo.addMealLocalDrift(any));
|
||||
final meal = verify(mockRepo.addMealLocalDrift(captureAny)).captured.single as Meal;
|
||||
expect(meal.planId, plan1.id);
|
||||
expect(meal.name, 'test meal');
|
||||
expect(meal.time, const TimeOfDay(hour: 1, minute: 1));
|
||||
});
|
||||
|
||||
// Detail page
|
||||
// ...
|
||||
});
|
||||
|
||||
testWidgets('Submit stays enabled offline (PowerSync handles create/edit locally)', (
|
||||
|
||||
@@ -16,7 +16,9 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:wger/features/routines/models/day_data.dart';
|
||||
|
||||
import '../../../../test_data/exercises.dart';
|
||||
import '../../../../test_data/routines.dart';
|
||||
@@ -55,6 +57,48 @@ void main() {
|
||||
expect(routine.dayDataCurrentIterationFiltered, [routine.dayData[2]]);
|
||||
});
|
||||
|
||||
group('current iteration', () {
|
||||
// Two weeks of a routine: iteration 1 on 5/6 January, iteration 2 on
|
||||
// 12/13 January
|
||||
DayData entry(int iteration, int day) =>
|
||||
DayData(iteration: iteration, date: DateTime(2026, 1, day), day: null);
|
||||
|
||||
final routine = getTestRoutine()
|
||||
..dayData = [entry(1, 5), entry(1, 6), entry(2, 12), entry(2, 13)]
|
||||
..dayDataGym = [entry(1, 5), entry(2, 12)];
|
||||
|
||||
test('follows the day the clock is on', () {
|
||||
withClock(Clock.fixed(DateTime(2026, 1, 12, 9)), () {
|
||||
expect(routine.getIteration(), 2);
|
||||
expect(routine.dayDataCurrentIteration.map((e) => e.date.day), [12, 13]);
|
||||
expect(routine.dayDataCurrentIterationGym.map((e) => e.date.day), [12]);
|
||||
});
|
||||
});
|
||||
|
||||
test('still counts the last minute of a day', () {
|
||||
withClock(Clock.fixed(DateTime(2026, 1, 13, 23, 59)), () {
|
||||
expect(routine.getIteration(), 2);
|
||||
});
|
||||
withClock(Clock.fixed(DateTime(2026, 1, 6, 23, 59)), () {
|
||||
expect(routine.getIteration(), 1);
|
||||
});
|
||||
});
|
||||
|
||||
test('falls back to the first iteration on a day outside the plan', () {
|
||||
withClock(Clock.fixed(DateTime(2026, 1, 14)), () {
|
||||
expect(routine.getIteration(), isNull);
|
||||
expect(routine.dayDataCurrentIteration.map((e) => e.date.day), [5, 6]);
|
||||
expect(routine.dayDataCurrentIterationGym.map((e) => e.date.day), [5]);
|
||||
});
|
||||
});
|
||||
|
||||
test('an explicit date wins over the clock', () {
|
||||
withClock(Clock.fixed(DateTime(2026, 1, 5)), () {
|
||||
expect(routine.getIteration(date: DateTime(2026, 1, 13, 18)), 2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('Test the filterLogsByExercise method', () {
|
||||
final routine = getTestRoutine();
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ void main() {
|
||||
}
|
||||
|
||||
testWidgets('Test that data from session is loaded', (WidgetTester tester) async {
|
||||
withClock(Clock.fixed(DateTime(2021, 5, 1)), () async {
|
||||
await withClock(Clock.fixed(DateTime(2021, 5, 1)), () async {
|
||||
await tester.pumpWidget(renderSessionPage());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
@@ -135,20 +135,23 @@ void main() {
|
||||
workoutStart: DateTime(2021, 5, 1, 13, 35),
|
||||
);
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(renderSessionPage());
|
||||
await tester.pumpAndSettle();
|
||||
// A fixed clock: the wall clock would collide with the start time at
|
||||
// 13:35 and could tick to the next minute between render and assert
|
||||
await withClock(Clock.fixed(DateTime(2021, 5, 1, 15, 23)), () async {
|
||||
// Act
|
||||
await tester.pumpWidget(renderSessionPage());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Assert
|
||||
final timeNow = TimeOfDay.now().format(tester.element(find.byType(TextFormField).first));
|
||||
expect(find.text('1:35 PM'), findsOneWidget);
|
||||
expect(find.text(timeNow), findsOneWidget);
|
||||
final toggleButtons = tester.widget<ToggleButtons>(find.byType(ToggleButtons));
|
||||
expect(toggleButtons.isSelected[1], isTrue);
|
||||
// Assert
|
||||
expect(find.text('1:35 PM'), findsOneWidget);
|
||||
expect(find.text('3:23 PM'), findsOneWidget);
|
||||
final toggleButtons = tester.widget<ToggleButtons>(find.byType(ToggleButtons));
|
||||
expect(toggleButtons.isSelected[1], isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('Test that correct data is send to server', (WidgetTester tester) async {
|
||||
withClock(Clock.fixed(DateTime(2021, 5, 1)), () async {
|
||||
await withClock(Clock.fixed(DateTime(2021, 5, 1)), () async {
|
||||
await tester.pumpWidget(renderSessionPage());
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const ValueKey('save-button')));
|
||||
|
||||
Reference in New Issue
Block a user