From 9ea66748494b0b187f84bbc0e73d6590f960ec3f Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 11 Nov 2025 23:56:19 +0100 Subject: [PATCH] Add test for calculatePages --- lib/providers/gym_state.dart | 2 - .../routines/gym_mode/session_page.dart | 2 +- test/routine/gym_mode_provider_test.dart | 97 +++++++++++++++++++ .../routine/gym_mode_session_screen_test.dart | 26 ++--- 4 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 test/routine/gym_mode_provider_test.dart diff --git a/lib/providers/gym_state.dart b/lib/providers/gym_state.dart index ba4d221a..590ce138 100644 --- a/lib/providers/gym_state.dart +++ b/lib/providers/gym_state.dart @@ -104,7 +104,6 @@ class GymState { bool? showTimerPages, int? currentPage, int? totalPages, - int? totalElements, int? dayId, DateTime? validUntil, TimeOfDay? startTime, @@ -162,7 +161,6 @@ class GymStateNotifier extends _$GymStateNotifier { @override GymState build() { _logger.finer('Initializing GymStateNotifier with default state'); - //loadPrefs(); return GymState(); } diff --git a/lib/widgets/routines/gym_mode/session_page.dart b/lib/widgets/routines/gym_mode/session_page.dart index 7df93545..e38faba2 100644 --- a/lib/widgets/routines/gym_mode/session_page.dart +++ b/lib/widgets/routines/gym_mode/session_page.dart @@ -62,7 +62,7 @@ class SessionPage extends StatelessWidget { Padding( padding: const EdgeInsets.symmetric(horizontal: 15), child: SessionForm( - _routine.id!, + _routine.id, onSaved: () => Navigator.of(context).pop(), session: _session, ), diff --git a/test/routine/gym_mode_provider_test.dart b/test/routine/gym_mode_provider_test.dart new file mode 100644 index 00000000..d6badeb6 --- /dev/null +++ b/test/routine/gym_mode_provider_test.dart @@ -0,0 +1,97 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2020, 2025 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. + * + * This program 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_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/models/workouts/day_data.dart'; +import 'package:wger/providers/gym_state.dart'; + +import '../../test_data/routines.dart'; + +void main() { + group('GymStateNotifier.calculatePages', () { + late GymStateNotifier notifier; + late ProviderContainer container; + late DayData day; + + setUp(() { + container = ProviderContainer.test(); + notifier = container.read(gymStateProvider.notifier); + day = getTestRoutine().dayData.first; + }); + + test( + 'Correctly generates pages: exercise and timer', + () { + // Arrange + notifier.state = notifier.state.copyWith(showExercisePages: true, showTimerPages: true); + + // Act + notifier.calculatePages(day); + + // Assert + final pages = notifier.state.pages; + expect(pages.length, 4, reason: '4 PageEntries (start, set 1, set 2, session)'); + + final setEntry = pages.firstWhere((p) => p.type == PageType.set); + expect(setEntry.slotPages.length, 3, reason: 'Three sets for the exercise'); + + expect(setEntry.slotPages[0].type, SlotPageType.exerciseOverview); + expect(setEntry.slotPages[1].type, SlotPageType.timer); + expect(setEntry.slotPages[2].type, SlotPageType.log); + + expect(notifier.state.totalPages, 7); + }, + ); + + test('Correctly generates pages: no exercises and no timer', () { + // Arrange + notifier.state = notifier.state.copyWith(showExercisePages: false, showTimerPages: false); + + // Act + notifier.calculatePages(day); + + // Assert + final pages = notifier.state.pages; + expect(pages.length, 4, reason: '4 PageEntries (start, set 1, set 2, session)'); + + final setEntry = pages.firstWhere((p) => p.type == PageType.set); + expect(setEntry.slotPages.length, 1); + expect(setEntry.slotPages[0].type, SlotPageType.log); + expect(notifier.state.totalPages, 3); + }); + + test('Correctly generates pages: exercises and no timer', () { + // Arrange + notifier.state = notifier.state.copyWith(showExercisePages: true, showTimerPages: false); + + // Act + notifier.calculatePages(day); + + // Assert + final pages = notifier.state.pages; + expect(pages.length, 4, reason: '4 PageEntries (start, set 1, set 2, session)'); + + final setEntry = pages.firstWhere((p) => p.type == PageType.set); + expect(setEntry.slotPages.length, 2); + expect(setEntry.slotPages[0].type, SlotPageType.exerciseOverview); + expect(setEntry.slotPages[1].type, SlotPageType.log); + expect(notifier.state.totalPages, 5); + }); + }); +} diff --git a/test/routine/gym_mode_session_screen_test.dart b/test/routine/gym_mode_session_screen_test.dart index bb5c77f4..ce30129a 100644 --- a/test/routine/gym_mode_session_screen_test.dart +++ b/test/routine/gym_mode_session_screen_test.dart @@ -18,6 +18,7 @@ import 'package:clock/clock.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; @@ -46,18 +47,19 @@ void main() { }); Widget renderSessionPage({locale = 'en'}) { - return ChangeNotifierProvider( - create: (context) => mockRoutinesProvider, - child: MaterialApp( - locale: Locale(locale), - localizationsDelegates: AppLocalizations.localizationsDelegates, - supportedLocales: AppLocalizations.supportedLocales, - home: Scaffold( - body: SessionPage( - testRoutine, - PageController(), - const TimeOfDay(hour: 13, minute: 35), - const {}, + return ProviderScope( + child: ChangeNotifierProvider( + create: (context) => mockRoutinesProvider, + child: MaterialApp( + locale: Locale(locale), + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Scaffold( + body: SessionPage( + testRoutine, + PageController(), + const TimeOfDay(hour: 13, minute: 35), + ), ), ), ),