Skip to content

Commit

Permalink
added more helper methods for widget and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffrey0606 committed Mar 27, 2023
1 parent c17ebc5 commit 6235ca3
Show file tree
Hide file tree
Showing 27 changed files with 823 additions and 293 deletions.
12 changes: 12 additions & 0 deletions integration_test/app_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

import 'bottom_nav_bar_test.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('Development App', () {
bottomNavBarTest();
});
}
85 changes: 85 additions & 0 deletions integration_test/bottom_nav_bar_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fpb/core/domain/user.dart';
import 'package:fpb/core/shared/helpers/value_injector.dart';
import 'package:fpb/home/home_screen.dart';
import 'package:fpb/home/view/budget_screen.dart';
import 'package:fpb/home/view/dashboard.dart';
import 'package:fpb/home/view/home_container.dart';
import 'package:fpb/home/view/widgets/bottom_nav_bar.dart';
import 'package:fpb/savings/view/savings_page.dart';

import '../test/helpers/helpers.dart';

void bottomNavBarTest() {
late MockCached cached;
late MockIFacebookRepositoryFacade mockIFacebookRepositoryFacade;
late MockIGoogleRepositoryFacade mockIGoogleRepositoryFacade;
late MockIAuthFacade mockIAuthFacade;

setUp(() {
cached = MockCached();
mockIAuthFacade = MockIAuthFacade();
mockIFacebookRepositoryFacade = MockIFacebookRepositoryFacade();
mockIGoogleRepositoryFacade = MockIGoogleRepositoryFacade();
});

testWidgets('renders home bottom nav bar buttons', (tester) async {
arrangeAuthRepositoryReturnsStreamWithUser(mockIAuthFacade);
arrangeAuthRepositoryReturnsCurrentUser(mockIAuthFacade);
arrangeCachedReturnsLastView(cached, 0);

await tester.pumpApp(
ValueInjector<User>(
value: testUser,
child: HomeBody(
user: testUser,
),
),
mockCachedForHomeBloc: cached,
mockIAuthFacadeForAuthBloc: mockIAuthFacade,
);

await tester.pumpAndSettle();

// await tester.pump();

expect(find.byType(HomeContainer), findsOneWidget);
expect(find.byType(BottomNavBar), findsOneWidget);
expect(find.byType(DashBoard), findsOneWidget);

await tester.tap(
find.byKey(
Key("BNB-savings-page-button"),
),
);

await tester.pumpAndSettle();

expect(find.byType(SavingsPage), findsOneWidget);

await tester.tap(
find.byKey(
Key("BNB-budget-page-button"),
),
);

await tester.pumpAndSettle();

expect(find.byType(BudgetScreen), findsOneWidget);

// await tester.tap(
// find.byKey(
// Key("BNB-userSearch-page-button"),
// ),
// );

// await tester.pumpAndSettle();

// expect(find.byType(UserSearchScreen), findsOneWidget);

await Future.delayed(
const Duration(seconds: 3),
);
});
}
3 changes: 1 addition & 2 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fpb/core/presentation/theming/themes/theme.dart';

import 'package:fpb/authentication_mock_without_backend/application/bloc/authentication_bloc.dart';
import 'package:fpb/authentication_with_firebase/application/bloc/auth_bloc.dart';
import 'package:fpb/core/application/internet_and_time_bloc/internet_and_time_bloc.dart';
import 'package:fpb/core/presentation/theming/themes/theme.dart';
import 'package:fpb/injection.dart';
import 'package:fpb/l10n/l10n.dart';
import 'package:fpb/router/app_route.gr.dart';
Expand Down
34 changes: 24 additions & 10 deletions lib/home/application/home_view_bloc/home_view_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// ignore_for_file: inference_failure_on_untyped_parameter

import 'package:bloc/bloc.dart';
import 'package:fpb/core/settings/app_settings_helper.dart';
import 'package:fpb/core/settings/cached.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';

part 'home_view_bloc.freezed.dart';
part 'home_view_event.dart';
part 'home_view_state.dart';
part 'home_view_bloc.freezed.dart';

@injectable
class HomeViewBloc extends Bloc<HomeViewEvent, HomeViewState> {
final AppSettingsHelper _appSettingsHelper;
HomeViewBloc(this._appSettingsHelper) : super(HomeViewState.home()) {
final Cached _cached;
HomeViewBloc(this._cached) : super(HomeViewState.home()) {
on<_HomeE>(_home);
on<_SavingsE>(_savings);
on<_QuickCashE>(_quickCash);
Expand All @@ -21,33 +21,41 @@ class HomeViewBloc extends Bloc<HomeViewEvent, HomeViewState> {
on<_SearchE>(_search);
}

int? lastView;

void _search(event, Emitter<HomeViewState> emit) {
emit(HomeViewState.search());
_appSettingsHelper.appCache.setLastView = 4;
lastView = 4;
}

void _home(event, Emitter<HomeViewState> emit) {
emit(HomeViewState.home());
_appSettingsHelper.appCache.setLastView = 0;
lastView = 0;
}

void _savings(event, Emitter<HomeViewState> emit) {
emit(HomeViewState.savings());
_appSettingsHelper.appCache.setLastView = 1;
lastView = 1;
}

void _quickCash(event, Emitter<HomeViewState> emit) {
emit(HomeViewState.quickCash());
_appSettingsHelper.appCache.setLastView = 2;
lastView = 2;
}

void _budget(event, Emitter<HomeViewState> emit) {
emit(HomeViewState.budget());
_appSettingsHelper.appCache.setLastView = 3;
lastView = 3;
}

void _lastState(event, Emitter<HomeViewState> emit) {
final lastState = _appSettingsHelper.appCache.getLastView;
late int lastState;
if (lastView != null) {
lastState = lastView!;
} else {
lastState = _cached.getLastView;
}

switch (lastState) {
case 0:
_home(event, emit);
Expand All @@ -69,4 +77,10 @@ class HomeViewBloc extends Bloc<HomeViewEvent, HomeViewState> {
break;
}
}

@override
Future<void> close() {
if (lastView != null) _cached.setLastView = lastView!;
return super.close();
}
}
1 change: 0 additions & 1 deletion lib/home/view/dashboard.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:fpb/authentication_with_firebase/application/bloc/auth_bloc.dart';
import 'package:fpb/core/domain/user.dart';
import 'package:fpb/core/presentation/animations/slide_up_route_transition.dart';
Expand Down
5 changes: 5 additions & 0 deletions lib/home/view/widgets/bottom_nav_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class _BottomNavBarState extends State<BottomNavBar> {
IconButton(
enableFeedback: false,
padding: EdgeInsets.zero,
key: Key("BNB-home-page-button"),
onPressed: () {
setState(() {
pageIndex = 0;
Expand Down Expand Up @@ -56,6 +57,7 @@ class _BottomNavBarState extends State<BottomNavBar> {
IconButton(
enableFeedback: false,
padding: EdgeInsets.zero,
key: Key("BNB-savings-page-button"),
onPressed: () {
setState(() {
pageIndex = 1;
Expand Down Expand Up @@ -86,6 +88,7 @@ class _BottomNavBarState extends State<BottomNavBar> {
IconButton(
enableFeedback: false,
padding: EdgeInsets.zero,
key: Key("BNB-quickCash-page-button"),
onPressed: () {
setState(() {
pageIndex = 2;
Expand Down Expand Up @@ -116,6 +119,7 @@ class _BottomNavBarState extends State<BottomNavBar> {
IconButton(
enableFeedback: false,
padding: EdgeInsets.zero,
key: Key("BNB-budget-page-button"),
onPressed: () {
setState(() {
pageIndex = 3;
Expand Down Expand Up @@ -147,6 +151,7 @@ class _BottomNavBarState extends State<BottomNavBar> {
IconButton(
enableFeedback: false,
padding: EdgeInsets.zero,
key: Key("BNB-userSearch-page-button"),
onPressed: () {
setState(() {
pageIndex = 4;
Expand Down
Loading

0 comments on commit 6235ca3

Please sign in to comment.