Skip to content

Commit

Permalink
[#31] Update flows
Browse files Browse the repository at this point in the history
  • Loading branch information
nkhanh44 committed Aug 24, 2023
1 parent 1605823 commit 8003a93
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
7 changes: 3 additions & 4 deletions lib/model/response/survey_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ class SurveyResponse {
id: id ?? '',
title: title ?? '',
description: description ?? '',
coverImageUrl: getHighQualityCoverImageUrl(),
coverImageUrl: highResolutionCoverImageUrl,

Check warning on line 29 in lib/model/response/survey_response.dart

View check run for this annotation

Codecov / codecov/patch

lib/model/response/survey_response.dart#L29

Added line #L29 was not covered by tests
);

String getHighQualityCoverImageUrl() {
return (coverImageUrl != null) ? "${coverImageUrl}l" : "";
}
String get highResolutionCoverImageUrl =>
(coverImageUrl != null) ? "${coverImageUrl}l" : "";

Check warning on line 33 in lib/model/response/survey_response.dart

View check run for this annotation

Codecov / codecov/patch

lib/model/response/survey_response.dart#L32-L33

Added lines #L32 - L33 were not covered by tests
}
1 change: 0 additions & 1 deletion lib/screens/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
}

Future<void> _initData() async {
ref.read(homeViewModelProvider.notifier).loadCachedSurveys();
ref.read(homeViewModelProvider.notifier).loadSurveys();
}

Expand Down
7 changes: 6 additions & 1 deletion lib/screens/home/home_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class HomeViewModel extends StateNotifier<HomeState> {
Stream<String> get error => _error.stream;

void loadSurveys() async {
_loadSurveysFromCache();
_loadSurveysFromRemote();
}

void _loadSurveysFromRemote() async {
final result = await _getSurveysUseCase.call(SurveysParams(
pageNumber: _pageNumber,
pageSize: _pageSize,
Expand All @@ -54,7 +59,7 @@ class HomeViewModel extends StateNotifier<HomeState> {
}
}

void loadCachedSurveys() async {
void _loadSurveysFromCache() async {
final result = await _getCachedSurveysUseCase.call();
if (result is Success<List<SurveyModel>>) {
final cachedSurveys =
Expand Down
24 changes: 11 additions & 13 deletions test/screens/home/home_view_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,23 @@ void main() {
});

test(
'loads surveys successfully and emits LoadSurveysSuccess state',
'loads surveys successfully and emits LoadSurveysSuccess and LoadCacheSurveysSuccess states',
() async {
when(mockGetSurveysUseCase.call(any)).thenAnswer(
(_) async => Success(surveys),
);
final surveysStream = homeViewModel.surveys;
homeViewModel.loadSurveys();
expect(surveysStream, emitsInOrder([surveys]));
},
);

test(
'loads cached surveys successfully and emits LoadCachedSurveysSuccess state',
() async {
when(mockGetCachedSurveysUseCase.call())
.thenAnswer((_) => Future.value(Success(surveys)));
final surveysStream = homeViewModel.surveys;
final stateStream = homeViewModel.stream;
homeViewModel.loadCachedSurveys();
homeViewModel.loadSurveys();
expect(surveysStream, emitsInOrder([surveys]));
expect(stateStream,
emitsInOrder([const HomeState.loadCachedSurveysSuccess()]));
expect(
stateStream,
emitsInOrder([
const HomeState.loadCachedSurveysSuccess(),
const HomeState.loadSurveysSuccess()
]));
},
);

Expand All @@ -72,6 +67,9 @@ void main() {
when(mockGetSurveysUseCase.call(any)).thenAnswer(
(_) async => Failed(exception),
);
when(mockGetCachedSurveysUseCase.call()).thenAnswer(
(_) async => Failed(exception),
);
final errorStream = homeViewModel.error;
homeViewModel.loadSurveys();
expect(
Expand Down

0 comments on commit 8003a93

Please sign in to comment.