From 9393dea538909b1c66c2c7bb59e7daea17cfa7e2 Mon Sep 17 00:00:00 2001 From: Xavier Paquet-Rapold Date: Sun, 21 Jan 2024 21:02:00 -0500 Subject: [PATCH] Fix ScheduleViewModel tests --- lib/core/viewmodels/schedule_viewmodel.dart | 69 +++++++++----------- test/viewmodels/schedule_viewmodel_test.dart | 6 +- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/lib/core/viewmodels/schedule_viewmodel.dart b/lib/core/viewmodels/schedule_viewmodel.dart index 9d01bfe98..22c90f411 100644 --- a/lib/core/viewmodels/schedule_viewmodel.dart +++ b/lib/core/viewmodels/schedule_viewmodel.dart @@ -175,43 +175,37 @@ class ScheduleViewModel extends FutureViewModel> { settings[PreferencesFlag.scheduleListView] as bool; @override - Future> futureToRun() => - _courseRepository.getCoursesActivities(fromCacheOnly: true).then((value) { - setBusyForObject(isLoadingEvents, true); - _courseRepository - .getCoursesActivities() - // ignore: return_type_invalid_for_catch_error - .catchError(onError) - .then((value1) async { - if (value1 != null) { - // Reload the list of activities - coursesActivities; - - await _courseRepository - .getCourses(fromCacheOnly: true) - .then((value2) { - courses = value2; - }); - if (_coursesActivities.isNotEmpty) { - if (calendarFormat == CalendarFormat.week) { - calendarEvents = selectedWeekCalendarEvents(); - } else { - calendarEvents = selectedMonthCalendarEvents(); - } - } + Future> futureToRun() async { + List? activities = await _courseRepository.getCoursesActivities(fromCacheOnly: true); + try { + setBusyForObject(isLoadingEvents, true); + + final fetchedCourseActivities = await _courseRepository.getCoursesActivities(); + if (fetchedCourseActivities != null) { + activities = fetchedCourseActivities; + // Reload the list of activities + coursesActivities; + + courses = await _courseRepository.getCourses(fromCacheOnly: true); + + if (_coursesActivities.isNotEmpty) { + if (calendarFormat == CalendarFormat.week) { + calendarEvents = selectedWeekCalendarEvents(); + } else { + calendarEvents = selectedMonthCalendarEvents(); } - _courseRepository - .getScheduleActivities() - // ignore: return_type_invalid_for_catch_error - .catchError(onError) - .then((value) async { - await assignScheduleActivities(value); - }).whenComplete(() { - setBusyForObject(isLoadingEvents, false); - }); - }); - return value ?? []; - }); + } + } + final scheduleActivities = await _courseRepository.getScheduleActivities(); + await assignScheduleActivities(scheduleActivities); + } catch(e) { + onError(e); + } finally { + setBusyForObject(isLoadingEvents, false); + } + return activities ?? []; + } + Future assignScheduleActivities( List listOfSchedules) async { @@ -351,10 +345,11 @@ class ScheduleViewModel extends FutureViewModel> { try { setBusyForObject(isLoadingEvents, true); await _courseRepository.getCoursesActivities(); - setBusyForObject(isLoadingEvents, false); notifyListeners(); } on Exception catch (error) { onError(error); + } finally { + setBusyForObject(isLoadingEvents, false); } } diff --git a/test/viewmodels/schedule_viewmodel_test.dart b/test/viewmodels/schedule_viewmodel_test.dart index cbf5cd48d..51200d3ae 100644 --- a/test/viewmodels/schedule_viewmodel_test.dart +++ b/test/viewmodels/schedule_viewmodel_test.dart @@ -274,7 +274,11 @@ void main() { verifyInOrder([ courseRepositoryMock.getCoursesActivities(fromCacheOnly: true), - courseRepositoryMock.getCoursesActivities() + courseRepositoryMock.getCoursesActivities(), + courseRepositoryMock.coursesActivities, + courseRepositoryMock.coursesActivities, + courseRepositoryMock.getCourses(fromCacheOnly: true), + courseRepositoryMock.getScheduleActivities(), ]); verifyNoMoreInteractions(courseRepositoryMock);