Skip to content

Instantly share code, notes, and snippets.

@felangel
Last active October 19, 2019 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felangel/6fbe112e37ba7165a70d7473879b0c23 to your computer and use it in GitHub Desktop.
Save felangel/6fbe112e37ba7165a70d7473879b0c23 to your computer and use it in GitHub Desktop.
[flutter_login_unit_tests] AuthenticationBloc Tests Complete
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:user_repository/user_repository.dart';
import 'package:flutter_login/authentication/authentication.dart';
class MockUserRepository extends Mock implements UserRepository {}
void main() {
AuthenticationBloc authenticationBloc;
MockUserRepository userRepository;
setUp(() {
userRepository = MockUserRepository();
authenticationBloc = AuthenticationBloc(userRepository: userRepository);
});
tearDown(() {
authenticationBloc?.close();
});
test('initial state is correct', () {
expect(authenticationBloc.initialState, AuthenticationUninitialized());
});
test('close does not emit new states', () {
expectLater(
authenticationBloc,
emitsInOrder([AuthenticationUninitialized(), emitsDone]),
);
authenticationBloc.close();
});
group('AppStarted', () {
test('emits [uninitialized, unauthenticated] for invalid token', () {
final expectedResponse = [
AuthenticationUninitialized(),
AuthenticationUnauthenticated()
];
when(userRepository.hasToken()).thenAnswer((_) => Future.value(false));
expectLater(
authenticationBloc,
emitsInOrder(expectedResponse),
);
authenticationBloc.add(AppStarted());
});
});
group('LoggedIn', () {
test(
'emits [uninitialized, loading, authenticated] when token is persisted',
() {
final expectedResponse = [
AuthenticationUninitialized(),
AuthenticationLoading(),
AuthenticationAuthenticated(),
];
expectLater(
authenticationBloc,
emitsInOrder(expectedResponse),
);
authenticationBloc.add(LoggedIn(
token: 'instance.token',
));
});
});
group('LoggedOut', () {
test(
'emits [uninitialized, loading, unauthenticated] when token is deleted',
() {
final expectedResponse = [
AuthenticationUninitialized(),
AuthenticationLoading(),
AuthenticationUnauthenticated(),
];
expectLater(
authenticationBloc,
emitsInOrder(expectedResponse),
);
authenticationBloc.add(LoggedOut());
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment