diff --git a/test/component/home/static_home_test.dart b/test/component/home/static_home_test.dart index 57162bed..a946dde9 100644 --- a/test/component/home/static_home_test.dart +++ b/test/component/home/static_home_test.dart @@ -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", @@ -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.empty()), + ), + ), ], child: const MaterialApp( title: "Proxima", @@ -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( diff --git a/test/component/login_test.dart b/test/component/login_test.dart index 8960ef6e..2a483f75 100644 --- a/test/component/login_test.dart +++ b/test/component/login_test.dart @@ -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"; @@ -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.empty(); + }, + ), + ), ...firebaseAuthMocksOverrides, ], child: const ProximaApp(), diff --git a/test/component/profile_page_test.dart b/test/component/profile_page_test.dart index 2c6344fa..815f16a1 100644 --- a/test/component/profile_page_test.dart +++ b/test/component/profile_page_test.dart @@ -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"; @@ -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.empty(); + }, + ), + ), ], child: const MaterialApp( onGenerateRoute: generateRoute, diff --git a/test/viewmodels/home_view_model_unit_test.dart b/test/viewmodels/home_view_model_unit_test.dart index 94be37f1..1fde3004 100644 --- a/test/viewmodels/home_view_model_unit_test.dart +++ b/test/viewmodels/home_view_model_unit_test.dart @@ -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 => [], ); @@ -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( @@ -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, ); @@ -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, ); diff --git a/test/viewmodels/mock_home_view_model.dart b/test/viewmodels/mock_home_view_model.dart new file mode 100644 index 00000000..0e54c2ab --- /dev/null +++ b/test/viewmodels/mock_home_view_model.dart @@ -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> + implements HomeViewModel { + // build function + final Future> Function() _build; + + MockHomeViewModel({required build}) : _build = build; + + @override + Future> build() { + return _build(); + } +}