Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/blocs/auth/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> with Loggable {
}

Future<void> onLogout(AuthLogout event, Emitter<AuthState> emit) async {
await _authRepository.logout();
await _preferenceRepository.updateUnreadCommentsIds(<int>[]);
await _sembastRepository.deleteCachedComments();
emit(
state.copyWith(
user: const User.empty(),
isLoggedIn: false,
agreedToEULA: false,
),
);
await _authRepository.logout();
await _preferenceRepository.updateUnreadCommentsIds(<int>[]);
await _sembastRepository.deleteCachedComments();
logInfo('user logged out');
}

Expand Down
45 changes: 29 additions & 16 deletions lib/screens/settings/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,37 @@ class _SettingsViewState extends State<SettingsView>
return SingleChildScrollView(
child: Column(
children: <Widget>[
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<AuthBloc>().state.username)
: null,
onTap: () {
if (isLoggedIn) {
onLogoutTapped();
} else {
onLoginTapped();
BlocListener<AuthBloc, AuthState>(
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<AuthBloc>().state.username)
: null,
onTap: () {
if (isLoggedIn) {
onLogoutTapped();
} else {
onLoginTapped();
}
},
),
),
const EnterOfflineModeListTile(),
const OfflineListTile(),
Expand Down
124 changes: 124 additions & 0 deletions test/blocs/auth/auth_bloc_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -144,4 +146,126 @@ 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<Completer<void>> operationStarted =
List<Completer<void>>.generate(3, (int _) => Completer<void>());
final List<Completer<void>> operationCompleted =
List<Completer<void>>.generate(3, (int _) => Completer<void>());

Future<void> waitForOperation(int index) {
operationStarted[index].complete();
return operationCompleted[index].future;
}

blocTest<AuthBloc, AuthState>(
'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<void> operation in operationCompleted) {
if (!operation.isCompleted) operation.complete();
}
}
},
expect: () => <AuthState>[loggedInState, const AuthState.init()],
verify: (AuthBloc _) {
verifyInOrder(<Future<void> Function()>[
mockAuthRepository.logout,
() => mockPreferenceRepository.updateUnreadCommentsIds(<int>[]),
mockSembastRepository.deleteCachedComments,
]);
},
);

const List<String> operationNames = <String>[
'logout',
'unread comments cleanup',
'cached comments cleanup',
];
for (final int failingOperation in <int>[0, 1, 2]) {
final StateError error = StateError('Logout operation failed');

Future<void> completeOperation(int index) async {
if (index == failingOperation) throw error;
}

blocTest<AuthBloc, AuthState>(
'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: () => <AuthState>[loggedInState],
errors: () => <Object>[error],
verify: (AuthBloc bloc) => expect(bloc.state, loggedInState),
);
}
});
}
Loading