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);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -21,9 +21,10 @@ import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
|
||||
class MockAuthenticationRepository extends Mock
|
||||
implements AuthenticationRepositoryInterface {}
|
||||
implements AuthenticationRepository {}
|
||||
|
||||
class MockAuthenticationCubit extends Mock implements AuthenticationCubit {}
|
||||
class MockAuthenticationCubit extends Mock
|
||||
implements AuthenticationCubit<void> {}
|
||||
|
||||
void main() {
|
||||
const String invalidEmailString = 'invalid';
|
||||
@@ -39,8 +40,8 @@ void main() {
|
||||
const Password validPassword = Password.dirty(validPasswordString);
|
||||
|
||||
group('SignInCubit', () {
|
||||
late AuthenticationRepositoryInterface authenticationRepository;
|
||||
late AuthenticationCubit authenticationCubit;
|
||||
late AuthenticationRepository authenticationRepository;
|
||||
late AuthenticationCubit<void> authenticationCubit;
|
||||
|
||||
setUp(() {
|
||||
authenticationRepository = MockAuthenticationRepository();
|
||||
@@ -62,7 +63,6 @@ void main() {
|
||||
expect(
|
||||
SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
).state,
|
||||
const SignInState(),
|
||||
);
|
||||
@@ -73,9 +73,8 @@ void main() {
|
||||
'emits [invalid] when email/password are invalid',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
),
|
||||
act: (SignInCubit cubit) => cubit.emailChanged(invalidEmailString),
|
||||
act: (cubit) => cubit.emailChanged(invalidEmailString),
|
||||
expect: () => const <SignInState>[
|
||||
SignInState(email: invalidEmail, status: FormStatus.invalid),
|
||||
],
|
||||
@@ -85,10 +84,9 @@ void main() {
|
||||
'emits [valid] when email/password are valid',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
),
|
||||
seed: () => const SignInState(password: validPassword),
|
||||
act: (SignInCubit cubit) => cubit.emailChanged(validEmailString),
|
||||
act: (cubit) => cubit.emailChanged(validEmailString),
|
||||
expect: () => const <SignInState>[
|
||||
SignInState(
|
||||
email: validEmail,
|
||||
@@ -104,10 +102,8 @@ void main() {
|
||||
'emits [invalid] when email/password are invalid',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
),
|
||||
act: (SignInCubit cubit) =>
|
||||
cubit.passwordChanged(invalidPasswordString),
|
||||
act: (cubit) => cubit.passwordChanged(invalidPasswordString),
|
||||
expect: () => const <SignInState>[
|
||||
SignInState(
|
||||
password: invalidPassword,
|
||||
@@ -120,10 +116,9 @@ void main() {
|
||||
'emits [valid] when email/password are valid',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
),
|
||||
seed: () => const SignInState(email: validEmail),
|
||||
act: (SignInCubit cubit) => cubit.passwordChanged(validPasswordString),
|
||||
act: (cubit) => cubit.passwordChanged(validPasswordString),
|
||||
expect: () => const <SignInState>[
|
||||
SignInState(
|
||||
email: validEmail,
|
||||
@@ -139,9 +134,8 @@ void main() {
|
||||
'does nothing when status is not validated',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
),
|
||||
act: (SignInCubit cubit) => cubit.signInWithEmailAndPassword(),
|
||||
act: (cubit) => cubit.signInWithEmailAndPassword(),
|
||||
expect: () => const <SignInState>[],
|
||||
);
|
||||
|
||||
@@ -149,14 +143,13 @@ void main() {
|
||||
'calls signInWithEmailAndPassword with correct email/password',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
),
|
||||
seed: () => const SignInState(
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
),
|
||||
act: (SignInCubit cubit) => cubit.signInWithEmailAndPassword(),
|
||||
act: (cubit) => cubit.signInWithEmailAndPassword(),
|
||||
verify: (_) {
|
||||
verify(
|
||||
() => authenticationRepository.signInWithEmailAndPassword(
|
||||
@@ -172,14 +165,13 @@ void main() {
|
||||
'when signInWithEmailAndPassword succeeds',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
),
|
||||
seed: () => const SignInState(
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
),
|
||||
act: (SignInCubit cubit) => cubit.signInWithEmailAndPassword(),
|
||||
act: (cubit) => cubit.signInWithEmailAndPassword(),
|
||||
expect: () => const <SignInState>[
|
||||
SignInState(
|
||||
status: FormStatus.submissionInProgress,
|
||||
@@ -207,14 +199,13 @@ void main() {
|
||||
},
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
),
|
||||
seed: () => const SignInState(
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
),
|
||||
act: (SignInCubit cubit) => cubit.signInWithEmailAndPassword(),
|
||||
act: (cubit) => cubit.signInWithEmailAndPassword(),
|
||||
expect: () => const <SignInState>[
|
||||
SignInState(
|
||||
status: FormStatus.submissionInProgress,
|
||||
|
||||
@@ -21,9 +21,10 @@ import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
|
||||
class MockAuthenticationRepository extends Mock
|
||||
implements AuthenticationRepositoryInterface {}
|
||||
implements AuthenticationRepository {}
|
||||
|
||||
class MockAuthenticationCubit extends Mock implements AuthenticationCubit {}
|
||||
class MockAuthenticationCubit extends Mock
|
||||
implements AuthenticationCubit<void> {}
|
||||
|
||||
void main() {
|
||||
const String invalidEmailString = 'invalid';
|
||||
@@ -39,8 +40,8 @@ void main() {
|
||||
const Password validPassword = Password.dirty(validPasswordString);
|
||||
|
||||
group('SignUpCubit', () {
|
||||
late AuthenticationRepositoryInterface authenticationRepository;
|
||||
late AuthenticationCubit authenticationCubit;
|
||||
late AuthenticationRepository authenticationRepository;
|
||||
late AuthenticationCubit<void> authenticationCubit;
|
||||
|
||||
setUp(() {
|
||||
authenticationRepository = MockAuthenticationRepository();
|
||||
@@ -50,9 +51,7 @@ void main() {
|
||||
email: any(named: 'email'),
|
||||
password: any(named: 'password'),
|
||||
),
|
||||
).thenAnswer((_) async {
|
||||
return 'uid';
|
||||
});
|
||||
).thenAnswer((_) async => 'uid');
|
||||
|
||||
when(
|
||||
() => authenticationCubit.start(),
|
||||
@@ -67,8 +66,7 @@ void main() {
|
||||
expect(
|
||||
SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
).state,
|
||||
const SignUpState(data: FormData.empty()),
|
||||
);
|
||||
@@ -79,10 +77,9 @@ void main() {
|
||||
'emits [invalid] when email/password are invalid',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
act: (SignUpCubit cubit) => cubit.emailChanged(invalidEmailString),
|
||||
act: (cubit) => cubit.emailChanged(invalidEmailString),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
email: invalidEmail,
|
||||
@@ -96,14 +93,13 @@ void main() {
|
||||
'emits [valid] when email/password are valid',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
act: (SignUpCubit cubit) => cubit.emailChanged(validEmailString),
|
||||
act: (cubit) => cubit.emailChanged(validEmailString),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
email: validEmail,
|
||||
@@ -120,11 +116,9 @@ void main() {
|
||||
'emits [invalid] when email/password are invalid',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
act: (SignUpCubit cubit) =>
|
||||
cubit.passwordChanged(invalidPasswordString),
|
||||
act: (cubit) => cubit.passwordChanged(invalidPasswordString),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
password: invalidPassword,
|
||||
@@ -138,14 +132,13 @@ void main() {
|
||||
'emits [valid] when email/password are valid',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
email: validEmail,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
act: (SignUpCubit cubit) => cubit.passwordChanged(validPasswordString),
|
||||
act: (cubit) => cubit.passwordChanged(validPasswordString),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
email: validEmail,
|
||||
@@ -162,10 +155,9 @@ void main() {
|
||||
'does nothing when status is not validated',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(),
|
||||
act: (cubit) => cubit.signUpFormSubmitted(),
|
||||
expect: () => const <SignUpState>[],
|
||||
);
|
||||
|
||||
@@ -173,8 +165,7 @@ void main() {
|
||||
'calls signUp with correct email/password/confirmedPassword',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
status: FormStatus.valid,
|
||||
@@ -182,7 +173,7 @@ void main() {
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(),
|
||||
act: (cubit) => cubit.signUpFormSubmitted(),
|
||||
verify: (_) {
|
||||
verify(
|
||||
() => authenticationRepository.signUp(
|
||||
@@ -198,8 +189,7 @@ void main() {
|
||||
'when signUp succeeds',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
status: FormStatus.valid,
|
||||
@@ -207,7 +197,7 @@ void main() {
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(),
|
||||
act: (cubit) => cubit.signUpFormSubmitted(),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
status: FormStatus.submissionInProgress,
|
||||
@@ -237,8 +227,7 @@ void main() {
|
||||
},
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: const FormData.empty(),
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
status: FormStatus.valid,
|
||||
@@ -246,7 +235,7 @@ void main() {
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(),
|
||||
act: (cubit) => cubit.signUpFormSubmitted(),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
status: FormStatus.submissionInProgress,
|
||||
|
||||
Reference in New Issue
Block a user