From 4d11236d27a2ccb790919dad1246643bae402dc1 Mon Sep 17 00:00:00 2001 From: J F Date: Fri, 27 Nov 2026 16:05:18 -0800 Subject: [PATCH 1/4] feat: add success feedback after logout --- lib/blocs/auth/auth_bloc.dart | 6 +- lib/screens/settings/settings_screen.dart | 44 +++++--- test/blocs/auth/auth_bloc_test.dart | 129 ++++++++++++++++++++++ 3 files changed, 160 insertions(+), 19 deletions(-) diff --git a/lib/blocs/auth/auth_bloc.dart b/lib/blocs/auth/auth_bloc.dart index 5e68f5858..91c713fec 100644 --- a/lib/blocs/auth/auth_bloc.dart +++ b/lib/blocs/auth/auth_bloc.dart @@ -100,6 +100,9 @@ class AuthBloc extends Bloc with Loggable { } Future onLogout(AuthLogout event, Emitter emit) async { + await _authRepository.logout(); + await _preferenceRepository.updateUnreadCommentsIds([]); + await _sembastRepository.deleteCachedComments(); emit( state.copyWith( user: const User.empty(), @@ -107,9 +110,6 @@ class AuthBloc extends Bloc with Loggable { agreedToEULA: false, ), ); - await _authRepository.logout(); - await _preferenceRepository.updateUnreadCommentsIds([]); - await _sembastRepository.deleteCachedComments(); logInfo('user logged out'); } diff --git a/lib/screens/settings/settings_screen.dart b/lib/screens/settings/settings_screen.dart index 94aaea8b5..e5ae667bf 100644 --- a/lib/screens/settings/settings_screen.dart +++ b/lib/screens/settings/settings_screen.dart @@ -73,24 +73,36 @@ class _SettingsViewState extends State return SingleChildScrollView( child: Column( children: [ - ListTile( - leading: Icon( - Icons.person, - color: isLoggedIn - ? Theme.of(context).colorScheme.primary - : null, - ), - title: Text(isLoggedIn ? 'Log Out' : 'Log In'), - subtitle: isLoggedIn - ? Text(context.read().state.username) - : null, - onTap: () { - if (isLoggedIn) { - onLogoutTapped(); - } else { - onLoginTapped(); + BlocListener( + listenWhen: (AuthState previous, AuthState current) => + previous.isLoggedIn && !current.isLoggedIn, + listener: (BuildContext context, AuthState state) { + if (ModalRoute.of(context)?.isCurrent ?? false) { + showSnackBar( + content: 'Logged out successfully! ${Constants.happyFace}', + ); + HapticFeedbackUtils.success(); } }, + child: ListTile( + leading: Icon( + Icons.person, + color: isLoggedIn + ? Theme.of(context).colorScheme.primary + : null, + ), + title: Text(isLoggedIn ? 'Log Out' : 'Log In'), + subtitle: isLoggedIn + ? Text(context.read().state.username) + : null, + onTap: () { + if (isLoggedIn) { + onLogoutTapped(); + } else { + onLoginTapped(); + } + }, + ), ), const EnterOfflineModeListTile(), const OfflineListTile(), diff --git a/test/blocs/auth/auth_bloc_test.dart b/test/blocs/auth/auth_bloc_test.dart index 02b2c6842..b1dedf1a8 100644 --- a/test/blocs/auth/auth_bloc_test.dart +++ b/test/blocs/auth/auth_bloc_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:bloc_test/bloc_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:hacki/blocs/blocs.dart'; @@ -144,4 +146,131 @@ void main() { }, ); }); + + group('AuthLogout', () { + const AuthState loggedInState = AuthState( + user: tUser, + isLoggedIn: true, + status: Status.success, + agreedToEULA: false, + ); + + setUp(() { + reset(mockAuthRepository); + reset(mockPreferenceRepository); + reset(mockHackerNewsRepository); + reset(mockSembastRepository); + when( + () => mockAuthRepository.loggedIn, + ).thenAnswer((Invocation _) async => true); + when( + () => mockAuthRepository.username, + ).thenAnswer((Invocation _) async => id); + when( + () => mockHackerNewsRepository.fetchUser(id: id), + ).thenAnswer((Invocation _) async => tUser); + }); + + final List> operationStarted = List>.generate( + 3, + (int _) => Completer(), + ); + final List> operationCompleted = + List>.generate(3, (int _) => Completer()); + + Future waitForOperation(int index) { + operationStarted[index].complete(); + return operationCompleted[index].future; + } + + blocTest( + 'emits logged-out state only after logout and cleanup complete', + build: () { + when( + () => mockAuthRepository.logout(), + ).thenAnswer((Invocation _) => waitForOperation(0)); + when( + () => mockPreferenceRepository.updateUnreadCommentsIds(any()), + ).thenAnswer((Invocation _) => waitForOperation(1)); + when( + () => mockSembastRepository.deleteCachedComments(), + ).thenAnswer((Invocation _) => waitForOperation(2)); + return AuthBloc( + authRepository: mockAuthRepository, + preferenceRepository: mockPreferenceRepository, + hackerNewsRepository: mockHackerNewsRepository, + sembastRepository: mockSembastRepository, + ); + }, + act: (AuthBloc bloc) async { + await bloc.stream.firstWhere((AuthState state) => state.isLoggedIn); + bloc.add(AuthLogout()); + try { + for (int index = 0; index < operationStarted.length; index++) { + await operationStarted[index].future; + expect(bloc.state, loggedInState); + operationCompleted[index].complete(); + } + await bloc.stream.firstWhere((AuthState state) => !state.isLoggedIn); + } finally { + for (final Completer operation in operationCompleted) { + if (!operation.isCompleted) operation.complete(); + } + } + }, + expect: () => [ + loggedInState, + const AuthState.init(), + ], + verify: (AuthBloc _) { + verifyInOrder( Function()>[ + () => mockAuthRepository.logout(), + () => mockPreferenceRepository.updateUnreadCommentsIds([]), + () => mockSembastRepository.deleteCachedComments(), + ]); + }, + ); + + const List operationNames = [ + 'logout', + 'unread comments cleanup', + 'cached comments cleanup', + ]; + for (final int failingOperation in [0, 1, 2]) { + final StateError error = StateError('Logout operation failed'); + + Future completeOperation(int index) async { + if (index == failingOperation) throw error; + } + + blocTest( + 'does not emit logged-out state when ' + '${operationNames[failingOperation]} fails', + build: () { + when( + () => mockAuthRepository.logout(), + ).thenAnswer((Invocation _) => completeOperation(0)); + when( + () => mockPreferenceRepository.updateUnreadCommentsIds(any()), + ).thenAnswer((Invocation _) => completeOperation(1)); + when( + () => mockSembastRepository.deleteCachedComments(), + ).thenAnswer((Invocation _) => completeOperation(2)); + return AuthBloc( + authRepository: mockAuthRepository, + preferenceRepository: mockPreferenceRepository, + hackerNewsRepository: mockHackerNewsRepository, + sembastRepository: mockSembastRepository, + ); + }, + act: (AuthBloc bloc) async { + await bloc.stream.firstWhere((AuthState state) => state.isLoggedIn); + bloc.add(AuthLogout()); + }, + expect: () => [loggedInState], + errors: () => [error], + verify: (AuthBloc bloc) => expect(bloc.state, loggedInState), + ); + } + }); } From 1bd8fad47848447d593e342d7d55ccdbc2707df9 Mon Sep 17 00:00:00 2001 From: J F Date: Fri, 27 Nov 2026 16:06:15 -0800 Subject: [PATCH 2/4] style: wrap logout feedback and test declarations --- lib/screens/settings/settings_screen.dart | 3 ++- test/blocs/auth/auth_bloc_test.dart | 6 ++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/screens/settings/settings_screen.dart b/lib/screens/settings/settings_screen.dart index e5ae667bf..7eae92f38 100644 --- a/lib/screens/settings/settings_screen.dart +++ b/lib/screens/settings/settings_screen.dart @@ -79,7 +79,8 @@ class _SettingsViewState extends State listener: (BuildContext context, AuthState state) { if (ModalRoute.of(context)?.isCurrent ?? false) { showSnackBar( - content: 'Logged out successfully! ${Constants.happyFace}', + content: + 'Logged out successfully! ${Constants.happyFace}', ); HapticFeedbackUtils.success(); } diff --git a/test/blocs/auth/auth_bloc_test.dart b/test/blocs/auth/auth_bloc_test.dart index b1dedf1a8..a0ef644fb 100644 --- a/test/blocs/auth/auth_bloc_test.dart +++ b/test/blocs/auth/auth_bloc_test.dart @@ -171,10 +171,8 @@ void main() { ).thenAnswer((Invocation _) async => tUser); }); - final List> operationStarted = List>.generate( - 3, - (int _) => Completer(), - ); + final List> operationStarted = + List>.generate(3, (int _) => Completer()); final List> operationCompleted = List>.generate(3, (int _) => Completer()); From 1a874d7a63506af360cb700a30b39d1da874efa8 Mon Sep 17 00:00:00 2001 From: J F Date: Fri, 27 Nov 2026 16:10:52 -0800 Subject: [PATCH 3/4] style: format logout state expectations --- test/blocs/auth/auth_bloc_test.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/blocs/auth/auth_bloc_test.dart b/test/blocs/auth/auth_bloc_test.dart index a0ef644fb..da6f1b088 100644 --- a/test/blocs/auth/auth_bloc_test.dart +++ b/test/blocs/auth/auth_bloc_test.dart @@ -216,10 +216,7 @@ void main() { } } }, - expect: () => [ - loggedInState, - const AuthState.init(), - ], + expect: () => [loggedInState, const AuthState.init()], verify: (AuthBloc _) { verifyInOrder( Function()>[ () => mockAuthRepository.logout(), From 5daf6ecdbd791c4cac33ece2d810374e42713d2c Mon Sep 17 00:00:00 2001 From: J F Date: Fri, 27 Nov 2026 16:15:41 -0800 Subject: [PATCH 4/4] test: use method tear-offs in logout mocks --- test/blocs/auth/auth_bloc_test.dart | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/blocs/auth/auth_bloc_test.dart b/test/blocs/auth/auth_bloc_test.dart index da6f1b088..55f0c426a 100644 --- a/test/blocs/auth/auth_bloc_test.dart +++ b/test/blocs/auth/auth_bloc_test.dart @@ -185,13 +185,13 @@ void main() { 'emits logged-out state only after logout and cleanup complete', build: () { when( - () => mockAuthRepository.logout(), + mockAuthRepository.logout, ).thenAnswer((Invocation _) => waitForOperation(0)); when( () => mockPreferenceRepository.updateUnreadCommentsIds(any()), ).thenAnswer((Invocation _) => waitForOperation(1)); when( - () => mockSembastRepository.deleteCachedComments(), + mockSembastRepository.deleteCachedComments, ).thenAnswer((Invocation _) => waitForOperation(2)); return AuthBloc( authRepository: mockAuthRepository, @@ -219,9 +219,9 @@ void main() { expect: () => [loggedInState, const AuthState.init()], verify: (AuthBloc _) { verifyInOrder( Function()>[ - () => mockAuthRepository.logout(), + mockAuthRepository.logout, () => mockPreferenceRepository.updateUnreadCommentsIds([]), - () => mockSembastRepository.deleteCachedComments(), + mockSembastRepository.deleteCachedComments, ]); }, ); @@ -243,13 +243,13 @@ void main() { '${operationNames[failingOperation]} fails', build: () { when( - () => mockAuthRepository.logout(), + mockAuthRepository.logout, ).thenAnswer((Invocation _) => completeOperation(0)); when( () => mockPreferenceRepository.updateUnreadCommentsIds(any()), ).thenAnswer((Invocation _) => completeOperation(1)); when( - () => mockSembastRepository.deleteCachedComments(), + mockSembastRepository.deleteCachedComments, ).thenAnswer((Invocation _) => completeOperation(2)); return AuthBloc( authRepository: mockAuthRepository,