Skip to content

Commit

Permalink
feat(overview): use a mock of the home viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannLafore committed Apr 8, 2024
1 parent 020f304 commit 29b0887
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
24 changes: 20 additions & 4 deletions test/component/home/static_home_test.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import "package:flutter/material.dart";
import "package:flutter_test/flutter_test.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/ui/post_overview.dart";
import "package:proxima/viewmodels/home_view_model.dart";
import "package:proxima/views/bottom_navigation_bar/navigation_bottom_bar.dart";
import "package:proxima/views/pages/home/home_page.dart";
import "package:proxima/views/pages/home/posts/home_feed.dart";
import "package:proxima/views/pages/home/posts/post_card/post_card.dart";
import "package:proxima/views/pages/home/top_bar/home_top_bar.dart";
import "../../viewmodels/mock_home_view_model.dart";
import "../utils/mock_data/home/mock_posts.dart";

void main() {
testWidgets("static home display top and bottom bar", (tester) async {
final homePageWidget = ProviderScope(
overrides: [
postOverviewProvider.overrideWith((ref) => Future.value(testPosts)),
postOverviewProvider.overrideWith(
() => MockHomeViewModel(
build: () async {
return testPosts;
},
),
),
],
child: const MaterialApp(
title: "Proxima",
Expand Down Expand Up @@ -72,7 +80,11 @@ void main() {
testWidgets("static home display no post text", (tester) async {
final homePageWidget = ProviderScope(
overrides: [
postOverviewProvider.overrideWith((ref) => Future.value(List.empty())),
postOverviewProvider.overrideWith(
() => MockHomeViewModel(
build: () => Future.value(List<PostOverview>.empty()),
),
),
],
child: const MaterialApp(
title: "Proxima",
Expand All @@ -98,8 +110,12 @@ void main() {
final homePageWidget = ProviderScope(
overrides: [
postOverviewProvider.overrideWith(
// Future.any([]) will never complete and simulate a loading state
(ref) => Future.any([]),
() => MockHomeViewModel(
build: () {
// Future.any([]) will never complete and simulate a loading state
return Future.any([]);
},
),
),
],
child: const MaterialApp(
Expand Down
10 changes: 9 additions & 1 deletion test/component/login_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "package:firebase_core/firebase_core.dart";
import "package:flutter_test/flutter_test.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/main.dart";
import "package:proxima/models/ui/post_overview.dart";
import "package:proxima/viewmodels/home_view_model.dart";
import "package:proxima/views/pages/create_account_page.dart";
import "package:proxima/views/pages/home/home_page.dart";
Expand All @@ -10,13 +11,20 @@ import "package:proxima/views/pages/login/login_page.dart";

import "../services/firebase/setup_firebase_mocks.dart";
import "../services/firebase/testing_auth_providers.dart";
import "../viewmodels/mock_home_view_model.dart";

void main() {
setupFirebaseAuthMocks();

final mockedProxima = ProviderScope(
overrides: [
postOverviewProvider.overrideWith((ref) => Future.value(List.empty())),
postOverviewProvider.overrideWith(
() => MockHomeViewModel(
build: () async {
return List<PostOverview>.empty();
},
),
),
...firebaseAuthMocksOverrides,
],
child: const ProximaApp(),
Expand Down
10 changes: 9 additions & 1 deletion test/component/profile_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "package:firebase_auth_mocks/firebase_auth_mocks.dart";
import "package:flutter/material.dart";
import "package:flutter_test/flutter_test.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/ui/post_overview.dart";
import "package:proxima/services/login_service.dart";
import "package:proxima/viewmodels/home_view_model.dart";
import "package:proxima/views/navigation/routes.dart";
Expand All @@ -14,12 +15,19 @@ import "package:proxima/views/pages/profile/profile_page.dart";
import "package:proxima/views/pages/profile/user_info/centauri_points.dart";
import "package:proxima/views/pages/profile/user_info/user_account.dart";
import "../services/firebase/setup_firebase_mocks.dart";
import "../viewmodels/mock_home_view_model.dart";

void main() {
testWidgets("Navigate to profile page", (tester) async {
final homePageWidget = ProviderScope(
overrides: [
postOverviewProvider.overrideWith((ref) => Future.value(List.empty())),
postOverviewProvider.overrideWith(
() => MockHomeViewModel(
build: () async {
return List<PostOverview>.empty();
},
),
),
],
child: const MaterialApp(
onGenerateRoute: generateRoute,
Expand Down
12 changes: 8 additions & 4 deletions test/viewmodels/home_view_model_unit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ void main() {
when(geoLocationService.getCurrentPosition()).thenAnswer(
(_) async => point,
);
when(postRepository.getNearPosts(point, kmPostRadius)).thenAnswer(
when(postRepository.getNearPosts(point, HomeViewModel.kmPostRadius))
.thenAnswer(
(_) async => [],
);

Expand Down Expand Up @@ -100,7 +101,8 @@ void main() {
when(geoLocationService.getCurrentPosition()).thenAnswer(
(_) async => point,
);
when(postRepository.getNearPosts(point, kmPostRadius)).thenAnswer(
when(postRepository.getNearPosts(point, HomeViewModel.kmPostRadius))
.thenAnswer(
(_) async => [post],
);
when(userRepository.getUser(post.data.ownerId)).thenAnswer(
Expand Down Expand Up @@ -152,7 +154,8 @@ void main() {
when(geoLocationService.getCurrentPosition()).thenAnswer(
(_) async => point,
);
when(postRepository.getNearPosts(point, kmPostRadius)).thenAnswer(
when(postRepository.getNearPosts(point, HomeViewModel.kmPostRadius))
.thenAnswer(
(_) async => posts,
);

Expand Down Expand Up @@ -207,7 +210,8 @@ void main() {
when(geoLocationService.getCurrentPosition()).thenAnswer(
(_) async => point,
);
when(postRepository.getNearPosts(point, kmPostRadius)).thenAnswer(
when(postRepository.getNearPosts(point, HomeViewModel.kmPostRadius))
.thenAnswer(
(_) async => posts,
);

Expand Down
16 changes: 16 additions & 0 deletions test/viewmodels/mock_home_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/ui/post_overview.dart";
import "package:proxima/viewmodels/home_view_model.dart";

class MockHomeViewModel extends AsyncNotifier<List<PostOverview>>
implements HomeViewModel {
// build function
final Future<List<PostOverview>> Function() _build;

MockHomeViewModel({required build}) : _build = build;

@override
Future<List<PostOverview>> build() {
return _build();
}
}

0 comments on commit 29b0887

Please sign in to comment.