Skip to content

Commit

Permalink
rfac: fix mock network image test
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-singh7 committed Jan 3, 2022
1 parent 279c193 commit a48a9ad
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 69 deletions.
18 changes: 10 additions & 8 deletions test/ui_tests/groups/assignment_details_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:get/get.dart';
import 'package:mobile_app/locator.dart';
import 'package:mobile_app/models/assignments.dart';
import 'package:mobile_app/ui/views/groups/assignment_details_view.dart';
import 'package:mobile_app/utils/image_test_utils.dart';
import '../../utils_tests/image_test_utils.dart';
import 'package:mobile_app/utils/router.dart';
import 'package:mobile_app/viewmodels/groups/assignment_details_viewmodel.dart';
import 'package:mockito/annotations.dart';
Expand All @@ -17,9 +17,10 @@ import '../../setup/test_data/mock_assignments.dart';
import 'assignment_details_view_test.mocks.dart';

@GenerateMocks(
[AssignmentDetailsViewModel],
[],
customMocks: [
MockSpec<NavigatorObserver>(returnNullOnMissingStub: true),
MockSpec<AssignmentDetailsViewModel>(returnNullOnMissingStub: true),
],
)
void main() {
Expand All @@ -46,14 +47,15 @@ void main() {
.thenAnswer((_) => 'fetch_assignment');
when(_assignmentsDetailsViewModel.fetchAssignmentDetails(any))
.thenReturn(null);
when(_assignmentsDetailsViewModel.assignment).thenReturn(_assignment);
when(_assignmentsDetailsViewModel.isSuccess(any)).thenReturn(true);
when(_assignmentsDetailsViewModel.assignment)
.thenAnswer((_) => _assignment);
when(_assignmentsDetailsViewModel.projects)
.thenReturn(_assignment.projects!);
.thenAnswer((_) => _assignment.projects!);
when(_assignmentsDetailsViewModel.focussedProject)
.thenReturn(_assignment.projects?.first);
when(_assignmentsDetailsViewModel.grades).thenReturn(_assignment.grades!);

when(_assignmentsDetailsViewModel.isSuccess(any)).thenReturn(true);
.thenAnswer((_) => _assignment.projects?.first);
when(_assignmentsDetailsViewModel.grades)
.thenAnswer((_) => _assignment.grades!);

await tester.pumpWidget(
GetMaterialApp(
Expand Down
47 changes: 27 additions & 20 deletions test/ui_tests/groups/my_groups_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,44 @@ import 'package:shared_preferences/shared_preferences.dart';
import '../../setup/test_data/mock_groups.dart';
import '../../setup/test_data/mock_user.dart';
import '../../setup/test_helpers.dart';
import 'my_groups_view_test.mocks.dart' as mock;

@GenerateMocks(
[MyGroupsViewModel],
[MyGroupsViewModel, GroupDetailsViewModel],
customMocks: [
MockSpec<NavigatorObserver>(returnNullOnMissingStub: true),
MockSpec<DialogService>(returnNullOnMissingStub: true),
],
)
void main() {
group('MyGroupsViewTest -', () {
late NavigatorObserver mockObserver;
late mock.MockNavigatorObserver mockObserver;

setUpAll(() async {
SharedPreferences.setMockInitialValues({});
await setupLocator();
locator.allowReassignment = true;
});

setUp(() => mockObserver = NavigatorObserverMock());
setUp(() => mockObserver = mock.MockNavigatorObserver());

Future<void> _pumpMyGroupsView(WidgetTester tester) async {
var model = MockMyGroupsViewModel();
var model = mock.MockMyGroupsViewModel();
locator.registerSingleton<MyGroupsViewModel>(model);

var groups = <Group>[];
groups.add(Group.fromJson(mockGroup));

when(model.FETCH_MENTORED_GROUPS)
.thenAnswer((_) => 'fetch_mentored_groups');
when(model.FETCH_MEMBER_GROUPS).thenAnswer((_) => 'fetch_member_groups');
when(model.previousMemberGroupsBatch).thenReturn(null);
when(model.previousMentoredGroupsBatch).thenReturn(null);
when(model.fetchMentoredGroups()).thenReturn(null);
when(model.fetchMemberGroups()).thenReturn(null);

when(model.isSuccess(model.FETCH_MENTORED_GROUPS))
.thenAnswer((_) => true);
when(model.isSuccess(model.FETCH_MEMBER_GROUPS)).thenAnswer((_) => true);
when(model.isSuccess(any)).thenAnswer((_) => true);
when(model.isSuccess(any)).thenAnswer((_) => true);

when(model.mentoredGroups).thenAnswer((_) => groups);
when(model.memberGroups).thenAnswer((_) => groups);
Expand All @@ -69,7 +75,7 @@ void main() {

/// The tester.pumpWidget() call above just built our app widget
/// and triggered the pushObserver method on the mockObserver once.
// verify(mockObserver.didPush(any, any));
verify(mockObserver.didPush(any, any));
}

testWidgets('finds Generic MyGroupsView widgets',
Expand Down Expand Up @@ -124,7 +130,7 @@ void main() {
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();

// verify(mockObserver.didPush(any, any));
verify(mockObserver.didPush(any, any));
expect(find.byType(NewGroupView), findsOneWidget);
});

Expand All @@ -140,18 +146,18 @@ void main() {
.thenAnswer((_) => User.fromJson(mockUser));

// Mock View Model
var _groupDetailsViewModel = MockGroupDetailsViewModel();
var _groupDetailsViewModel = mock.MockGroupDetailsViewModel();
locator.registerSingleton<GroupDetailsViewModel>(_groupDetailsViewModel);

when(_groupDetailsViewModel.fetchGroupDetails('')).thenReturn(null);
when(_groupDetailsViewModel
.isSuccess(_groupDetailsViewModel.FETCH_GROUP_DETAILS))
.thenAnswer((_) => false);
when(_groupDetailsViewModel.FETCH_GROUP_DETAILS)
.thenAnswer((_) => 'fetch_group_details');
when(_groupDetailsViewModel.fetchGroupDetails(any)).thenReturn(null);
when(_groupDetailsViewModel.isSuccess(any)).thenAnswer((_) => false);

await tester.tap(find.widgetWithText(CardButton, 'View').first);
await tester.pumpAndSettle();

// verify(mockObserver.didPush(any, any));
verify(mockObserver.didPush(any, any));
expect(find.byType(GroupDetailsView), findsOneWidget);
});

Expand All @@ -162,13 +168,13 @@ void main() {
await tester.tap(find.widgetWithText(CardButton, 'Edit'));
await tester.pumpAndSettle();

// verify(mockObserver.didPush(any, any));
verify(mockObserver.didPush(any, any));
expect(find.byType(EditGroupView), findsOneWidget);
});

testWidgets('Delete Group Dialog is visible onTap',
(WidgetTester tester) async {
var _dialogService = MockDialogService();
var _dialogService = mock.MockDialogService();
locator.registerSingleton<DialogService>(_dialogService);

// Mock Dialog Service
Expand All @@ -185,9 +191,10 @@ void main() {

// Verify Dialog Service was called after Delete Button is pressed
verify(_dialogService.showConfirmationDialog(
title: anyNamed('title'),
description: anyNamed('description'),
)).called(1);
title: anyNamed('title'),
description: anyNamed('description'),
confirmationTitle: anyNamed('confirmationTitle')))
.called(1);
});
});
}
9 changes: 5 additions & 4 deletions test/ui_tests/groups/new_group_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void main() {
var _dialogService = test.MockDialogService();
locator.registerSingleton<DialogService>(_dialogService);

when(_dialogService.showCustomProgressDialog(title: 'title'))
when(_dialogService.showCustomProgressDialog(title: anyNamed('title')))
.thenAnswer((_) => Future.value(DialogResponse(confirmed: false)));
when(_dialogService.popDialog()).thenReturn(null);

Expand All @@ -78,8 +78,8 @@ void main() {

when(_newGroupViewModel.ADD_GROUP).thenAnswer((_) => 'add_group');
when(_newGroupViewModel.addGroup(any)).thenReturn(null);
when(_newGroupViewModel.isSuccess(_newGroupViewModel.ADD_GROUP))
.thenReturn(true);
when(_newGroupViewModel.isSuccess(any)).thenReturn(true);
when(_newGroupViewModel.newGroup).thenAnswer((_) => null);

// Pump New Group View
await _pumpNewGroupView(tester);
Expand All @@ -96,7 +96,8 @@ void main() {
await tester.pump(const Duration(seconds: 5));

// Verify Dialog Service is called to show Dialog of Updating
verify(_dialogService.showCustomProgressDialog(title: 'title')).called(1);
verify(_dialogService.showCustomProgressDialog(title: anyNamed('title')))
.called(1);
});
});
}
20 changes: 13 additions & 7 deletions test/ui_tests/profile/edit_profile_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import 'package:mobile_app/ui/components/cv_primary_button.dart';
import 'package:mobile_app/ui/components/cv_text_field.dart';
import 'package:mobile_app/ui/components/cv_typeahead_field.dart';
import 'package:mobile_app/ui/views/profile/edit_profile_view.dart';
import 'package:mobile_app/utils/image_test_utils.dart';
import '../../utils_tests/image_test_utils.dart';
import 'package:mobile_app/utils/router.dart';
import 'package:mobile_app/viewmodels/profile/edit_profile_viewmodel.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../setup/test_data/mock_user.dart';
import '../../setup/test_helpers.dart' as test;
import 'edit_profile_view_test.mocks.dart';
// import 'profile_view_test.mocks.dart';

@GenerateMocks(
[EditProfileViewModel, DialogService],
[EditProfileViewModel],
customMocks: [
MockSpec<NavigatorObserver>(returnNullOnMissingStub: true),
MockSpec<LocalStorageService>(returnNullOnMissingStub: true),
Expand All @@ -44,6 +44,11 @@ void main() {
// Mock Local Storage
// var _localStorageService = test.getAndRegisterLocalStorageServiceMock();
var _localStorageService = MockLocalStorageService();
var isRegistered = locator.isRegistered<LocalStorageService>();
if (isRegistered) {
locator.unregister<LocalStorageService>();
}
locator.registerSingleton<LocalStorageService>(_localStorageService);

var user = User.fromJson(mockUser);
when(_localStorageService.currentUser).thenReturn(user);
Expand Down Expand Up @@ -87,7 +92,7 @@ void main() {

testWidgets('on Save Details is Tapped', (WidgetTester tester) async {
// Mock Dialog Service
var _dialogService = MockDialogService();
var _dialogService = test.MockDialogService();
locator.registerSingleton<DialogService>(_dialogService);

when(_dialogService.showCustomProgressDialog(title: anyNamed('title')))
Expand All @@ -98,11 +103,12 @@ void main() {
var _editProfileViewModel = MockEditProfileViewModel();
locator.registerSingleton<EditProfileViewModel>(_editProfileViewModel);

when(_editProfileViewModel.UPDATE_PROFILE)
.thenAnswer((_) => 'update_profile');
when(_editProfileViewModel.updateProfile(any, any, any, any))
.thenReturn(null);
when(_editProfileViewModel
.isSuccess(_editProfileViewModel.UPDATE_PROFILE))
.thenReturn(true);
when(_editProfileViewModel.isSuccess(any)).thenReturn(true);
when(_editProfileViewModel.updatedUser).thenAnswer((_) => null);

// Pump Edit Profile View
await _pumpEditProfileView(tester);
Expand Down
18 changes: 10 additions & 8 deletions test/ui_tests/profile/profile_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:mobile_app/locator.dart';
import 'package:mobile_app/models/user.dart';
import 'package:mobile_app/services/local_storage_service.dart';
import 'package:mobile_app/ui/views/profile/profile_view.dart';
import 'package:mobile_app/utils/image_test_utils.dart';
import '../../utils_tests/image_test_utils.dart';
import 'package:mobile_app/utils/router.dart';
import 'package:mobile_app/viewmodels/profile/profile_viewmodel.dart';
import 'package:mobile_app/viewmodels/profile/user_projects_viewmodel.dart';
Expand All @@ -18,13 +18,12 @@ import '../../setup/test_data/mock_user.dart';
import 'profile_view_test.mocks.dart';

@GenerateMocks(
[
UserProjectsViewModel,
],
[],
customMocks: [
MockSpec<NavigatorObserver>(returnNullOnMissingStub: true),
MockSpec<LocalStorageService>(returnNullOnMissingStub: true),
MockSpec<ProfileViewModel>(returnNullOnMissingStub: true),
MockSpec<UserProjectsViewModel>(returnNullOnMissingStub: true),
],
)
void main() {
Expand All @@ -42,6 +41,11 @@ void main() {
Future<void> _pumpProfileView(WidgetTester tester) async {
// Mock Local Storage
var _localStorageService = MockLocalStorageService();
var isRegistered = locator.isRegistered<LocalStorageService>();
if (isRegistered) {
locator.unregister<LocalStorageService>();
}
locator.registerSingleton<LocalStorageService>(_localStorageService);

var user = User.fromJson(mockUser);
when(_localStorageService.currentUser).thenReturn(user);
Expand All @@ -67,9 +71,7 @@ void main() {
when(_userProjectsViewModel.FETCH_USER_PROJECTS)
.thenAnswer((_) => 'fetch_user_projects');
when(_userProjectsViewModel.fetchUserProjects()).thenReturn(null);
when(_userProjectsViewModel
.isSuccess(_userProjectsViewModel.FETCH_USER_PROJECTS))
.thenReturn(false);
when(_userProjectsViewModel.isSuccess(any)).thenReturn(false);

await tester.pumpWidget(
GetMaterialApp(
Expand All @@ -81,7 +83,7 @@ void main() {

/// The tester.pumpWidget() call above just built our app widget
/// and triggered the pushObserver method on the mockObserver once.
verifyNever(mockObserver.didPush(any, any));
verify(mockObserver.didPush(any, any));
}

testWidgets('finds Generic ProfileView widgets',
Expand Down
15 changes: 8 additions & 7 deletions test/ui_tests/profile/user_favourites_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:mobile_app/models/projects.dart';
import 'package:mobile_app/ui/views/profile/user_favourites_view.dart';
import 'package:mobile_app/ui/views/projects/components/project_card.dart';
import 'package:mobile_app/ui/views/projects/project_details_view.dart';
import 'package:mobile_app/utils/image_test_utils.dart';
import '../../utils_tests/image_test_utils.dart';
import 'package:mobile_app/utils/router.dart';
import 'package:mobile_app/viewmodels/profile/user_favourites_viewmodel.dart';
import 'package:mobile_app/viewmodels/projects/project_details_viewmodel.dart';
Expand All @@ -15,14 +15,12 @@ import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../setup/test_data/mock_projects.dart';
// import '../../setup/test_helpers.dart';
import 'user_favourites_view_test.mocks.dart';

@GenerateMocks(
[ProjectDetailsViewModel],
[UserFavouritesViewModel, ProjectDetailsViewModel],
customMocks: [
MockSpec<NavigatorObserver>(returnNullOnMissingStub: true),
MockSpec<UserFavouritesViewModel>(returnNullOnMissingStub: true),
],
)
void main() {
Expand Down Expand Up @@ -51,15 +49,15 @@ void main() {
when(_userFavoritesViewModel.fetchUserFavourites()).thenReturn(null);
when(_userFavoritesViewModel.isSuccess(any)).thenReturn(true);
when(_userFavoritesViewModel.userFavourites).thenAnswer((_) => projects);
when(_userFavoritesViewModel.previousUserFavouritesBatch)
.thenAnswer((_) => null);

await tester.pumpWidget(
GetMaterialApp(
onGenerateRoute: CVRouter.generateRoute,
navigatorObservers: [mockObserver],
home: const Scaffold(
body: UserFavouritesView(
userId: 'user_id',
),
body: UserFavouritesView(),
),
),
);
Expand Down Expand Up @@ -90,6 +88,9 @@ void main() {
locator.registerSingleton<ProjectDetailsViewModel>(
projectDetailsViewModel);

when(projectDetailsViewModel.starCount).thenAnswer((_) => 0);
when(projectDetailsViewModel.FETCH_PROJECT_DETAILS)
.thenAnswer((_) => 'fetch_project_details');
when(projectDetailsViewModel.fetchProjectDetails(any)).thenReturn(null);
when(projectDetailsViewModel.isSuccess(any)).thenReturn(false);

Expand Down
Loading

0 comments on commit a48a9ad

Please sign in to comment.