feat(auth): add reactive repo + extra data + router examples + tests
This commit is contained in:
+47
-26
@@ -20,66 +20,87 @@ import 'package:mocktail/mocktail.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
|
||||
class MockAuthenticationRepository extends Mock
|
||||
implements AuthenticationRepositoryInterface {}
|
||||
implements AuthenticationRepository {}
|
||||
|
||||
class MockUser extends Mock implements UserInterface {}
|
||||
class MockUser extends Mock implements User {}
|
||||
|
||||
void main() {
|
||||
group('AuthenticationCubit', () {
|
||||
group('AuthenticationCubit<T>', () {
|
||||
final MockUser user = MockUser();
|
||||
late AuthenticationRepositoryInterface authenticationRepository;
|
||||
late AuthenticationRepository authenticationRepository;
|
||||
|
||||
setUp(() {
|
||||
authenticationRepository = MockAuthenticationRepository();
|
||||
when(() => authenticationRepository.user).thenAnswer(
|
||||
(_) => const Stream.empty(),
|
||||
);
|
||||
when(() => authenticationRepository.cubitStatus).thenAnswer(
|
||||
(_) => Stream.fromIterable([AuthCubitStatus.stoped]),
|
||||
);
|
||||
when(
|
||||
() => authenticationRepository.currentUser,
|
||||
).thenReturn(const UserFirebase.empty());
|
||||
).thenReturn(user);
|
||||
});
|
||||
|
||||
test('initial state is unknown', () {
|
||||
test('initial auth state is `unknown`', () {
|
||||
expect(
|
||||
AuthenticationCubit(authenticationRepository: authenticationRepository)
|
||||
.state,
|
||||
const AuthenticationState.unknown(),
|
||||
AuthenticationCubit<void>(
|
||||
authenticationRepository: authenticationRepository,
|
||||
).state,
|
||||
const AuthenticationState<Never>.unknown(),
|
||||
);
|
||||
});
|
||||
|
||||
test('initial cubit status is `stoped`', () async {
|
||||
expect(
|
||||
await AuthenticationCubit<void>(
|
||||
authenticationRepository: authenticationRepository,
|
||||
).status,
|
||||
AuthCubitStatus.stoped,
|
||||
);
|
||||
});
|
||||
|
||||
group('UserChanged', () {
|
||||
blocTest<AuthenticationCubit, AuthenticationState>(
|
||||
blocTest<AuthenticationCubit<void>, AuthenticationState<void>>(
|
||||
'emits authenticated when user is not empty',
|
||||
setUp: () {
|
||||
when(() => user.isNotEmpty).thenReturn(true);
|
||||
when(() => authenticationRepository.user).thenAnswer(
|
||||
(_) => Stream.value(user),
|
||||
);
|
||||
},
|
||||
build: () => AuthenticationCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
)..init(),
|
||||
seed: () => const AuthenticationState.unknown(),
|
||||
expect: () => [AuthenticationState.authenticated(user, null)],
|
||||
);
|
||||
|
||||
blocTest<AuthenticationCubit, AuthenticationState>(
|
||||
'emits unauthenticated when user is empty',
|
||||
setUp: () {
|
||||
when(() => authenticationRepository.user).thenAnswer(
|
||||
(_) => Stream.value(const UserFirebase.empty()),
|
||||
when(() => authenticationRepository.cubitStatus).thenAnswer(
|
||||
(_) => Stream.value(AuthCubitStatus.started),
|
||||
);
|
||||
},
|
||||
build: () => AuthenticationCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
)..init(),
|
||||
seed: () => const AuthenticationState.unknown(),
|
||||
expect: () => [const AuthenticationState.unauthenticated()],
|
||||
expect: () => [AuthenticationState<void>.authenticated(user, null)],
|
||||
);
|
||||
|
||||
blocTest<AuthenticationCubit<void>, AuthenticationState<void>>(
|
||||
'emits unauthenticated when user is empty',
|
||||
setUp: () {
|
||||
when(() => user.isEmpty).thenReturn(true);
|
||||
when(() => user.isNotEmpty).thenReturn(false);
|
||||
when(() => authenticationRepository.user).thenAnswer(
|
||||
(_) => Stream.value(user),
|
||||
);
|
||||
when(() => authenticationRepository.cubitStatus).thenAnswer(
|
||||
(_) => Stream.value(AuthCubitStatus.started),
|
||||
);
|
||||
},
|
||||
build: () => AuthenticationCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
)..init(),
|
||||
seed: () => const AuthenticationState.unknown(),
|
||||
expect: () => [const AuthenticationState<Never>.unauthenticated()],
|
||||
);
|
||||
});
|
||||
|
||||
group('LogoutRequested', () {
|
||||
blocTest<AuthenticationCubit, AuthenticationState>(
|
||||
blocTest<AuthenticationCubit<void>, AuthenticationState<void>>(
|
||||
'invokes signOut',
|
||||
setUp: () {
|
||||
when(
|
||||
@@ -89,7 +110,7 @@ void main() {
|
||||
build: () => AuthenticationCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
act: (AuthenticationCubit cubit) => cubit.logOut(),
|
||||
act: (cubit) => cubit.logOut(),
|
||||
verify: (_) {
|
||||
verify(() => authenticationRepository.signOut()).called(1);
|
||||
},
|
||||
|
||||
+16
-3
@@ -18,13 +18,14 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
|
||||
class MockUser extends Mock implements UserInterface {}
|
||||
class MockUser extends Mock implements User {}
|
||||
|
||||
void main() {
|
||||
group('AuthenticationState', () {
|
||||
group('unauthenticated', () {
|
||||
test('has correct status', () {
|
||||
const AuthenticationState state = AuthenticationState.unauthenticated();
|
||||
const AuthenticationState<void> state =
|
||||
AuthenticationState.unauthenticated();
|
||||
expect(state.status, AuthenticationStatus.unauthenticated);
|
||||
expect(state.user, null);
|
||||
});
|
||||
@@ -33,11 +34,23 @@ void main() {
|
||||
group('authenticated', () {
|
||||
test('has correct status', () {
|
||||
final MockUser user = MockUser();
|
||||
final AuthenticationState state =
|
||||
final AuthenticationState<void> state =
|
||||
AuthenticationState.authenticated(user, null);
|
||||
expect(state.status, AuthenticationStatus.authenticated);
|
||||
expect(state.user, user);
|
||||
});
|
||||
});
|
||||
|
||||
group('authenticated with extra data', () {
|
||||
test('has correct status', () {
|
||||
final MockUser user = MockUser();
|
||||
const String extra = 'AwesomeExtraData';
|
||||
final AuthenticationState<String> state =
|
||||
AuthenticationState.authenticated(user, extra);
|
||||
expect(state.status, AuthenticationStatus.authenticated);
|
||||
expect(state.user, user);
|
||||
expect(state.extra, extra);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user