test(auth): add tests for all features
This commit is contained in:
@@ -17,46 +17,61 @@
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:wyatt_architecture/wyatt_architecture.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
|
||||
|
||||
class MockAuthenticationRepository extends Mock
|
||||
implements AuthenticationRepository {}
|
||||
implements AuthenticationRepository<int> {}
|
||||
|
||||
class MockAuthenticationCubit extends Mock
|
||||
implements AuthenticationCubit<void> {}
|
||||
class MockAuthenticationCubit extends Mock implements AuthenticationCubit<int> {
|
||||
}
|
||||
|
||||
class MockAccount extends Mock implements Account {}
|
||||
|
||||
class MockFormRepository extends Mock implements FormRepository {}
|
||||
|
||||
void main() {
|
||||
const String invalidEmailString = 'invalid';
|
||||
const Email invalidEmail = Email.dirty(invalidEmailString);
|
||||
|
||||
const String validEmailString = 'test@gmail.com';
|
||||
const Email validEmail = Email.dirty(validEmailString);
|
||||
|
||||
const String invalidPasswordString = 'invalid';
|
||||
const Password invalidPassword = Password.dirty(invalidPasswordString);
|
||||
|
||||
const String validPasswordString = 't0pS3cret1234';
|
||||
const Password validPassword = Password.dirty(validPasswordString);
|
||||
|
||||
group('SignInCubit', () {
|
||||
late AuthenticationRepository authenticationRepository;
|
||||
late AuthenticationCubit<void> authenticationCubit;
|
||||
final MockAccount account = MockAccount();
|
||||
final WyattForm form = WyattFormImpl(
|
||||
[
|
||||
FormInput(AuthFormField.email, const Email.pure()),
|
||||
FormInput(AuthFormField.password, const Password.pure())
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
);
|
||||
|
||||
late MockFormRepository formRepository;
|
||||
late AuthenticationRepository<int> authenticationRepository;
|
||||
|
||||
setUp(() {
|
||||
authenticationRepository = MockAuthenticationRepository();
|
||||
authenticationCubit = MockAuthenticationCubit();
|
||||
formRepository = MockFormRepository();
|
||||
|
||||
when(
|
||||
() => authenticationRepository.signInWithEmailAndPassword(
|
||||
email: any(named: 'email'),
|
||||
password: any(named: 'password'),
|
||||
),
|
||||
).thenAnswer((_) async {});
|
||||
).thenAnswer((_) async => Ok(account));
|
||||
|
||||
when(
|
||||
() => authenticationCubit.start(),
|
||||
).thenReturn(true);
|
||||
() => authenticationRepository.formRepository,
|
||||
).thenAnswer((_) => formRepository);
|
||||
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signInForm),
|
||||
).thenAnswer((_) => form);
|
||||
});
|
||||
|
||||
test('initial state is SignInState', () {
|
||||
@@ -64,33 +79,90 @@ void main() {
|
||||
SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
).state,
|
||||
const SignInState(),
|
||||
SignInState(form: form),
|
||||
);
|
||||
});
|
||||
|
||||
group('emailChanged', () {
|
||||
blocTest<SignInCubit, SignInState>(
|
||||
blocTest<SignInCubit<int>, SignInState>(
|
||||
'emits [invalid] when email/password are invalid',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
act: (cubit) => cubit.emailChanged(invalidEmailString),
|
||||
expect: () => const <SignInState>[
|
||||
SignInState(email: invalidEmail, status: FormStatus.invalid),
|
||||
expect: () => <SignInState>[
|
||||
SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(invalidEmailString),
|
||||
),
|
||||
FormInput(AuthFormField.password, const Password.pure())
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.invalid,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<SignInCubit, SignInState>(
|
||||
blocTest<SignInCubit<int>, SignInState>(
|
||||
'emits [valid] when email/password are valid',
|
||||
setUp: () {
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signInForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.pure(),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
seed: () => const SignInState(password: validPassword),
|
||||
seed: () => SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.pure(),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.invalid,
|
||||
),
|
||||
act: (cubit) => cubit.emailChanged(validEmailString),
|
||||
expect: () => const <SignInState>[
|
||||
expect: () => <SignInState>[
|
||||
SignInState(
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.valid,
|
||||
),
|
||||
],
|
||||
@@ -98,58 +170,145 @@ void main() {
|
||||
});
|
||||
|
||||
group('passwordChanged', () {
|
||||
blocTest<SignInCubit, SignInState>(
|
||||
blocTest<SignInCubit<int>, SignInState>(
|
||||
'emits [invalid] when email/password are invalid',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
act: (cubit) => cubit.passwordChanged(invalidPasswordString),
|
||||
expect: () => const <SignInState>[
|
||||
expect: () => <SignInState>[
|
||||
SignInState(
|
||||
password: invalidPassword,
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.pure(),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(invalidPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.invalid,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<SignInCubit, SignInState>(
|
||||
blocTest<SignInCubit<int>, SignInState>(
|
||||
'emits [valid] when email/password are valid',
|
||||
setUp: () {
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signInForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.pure(),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
seed: () => const SignInState(email: validEmail),
|
||||
seed: () => SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.pure(),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.invalid,
|
||||
),
|
||||
act: (cubit) => cubit.passwordChanged(validPasswordString),
|
||||
expect: () => const <SignInState>[
|
||||
expect: () => <SignInState>[
|
||||
SignInState(
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.valid,
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
group('logInWithCredentials', () {
|
||||
blocTest<SignInCubit, SignInState>(
|
||||
group('submit', () {
|
||||
blocTest<SignInCubit<int>, SignInState>(
|
||||
'does nothing when status is not validated',
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
act: (cubit) => cubit.signInWithEmailAndPassword(),
|
||||
act: (cubit) => cubit.submit(),
|
||||
expect: () => const <SignInState>[],
|
||||
);
|
||||
|
||||
blocTest<SignInCubit, SignInState>(
|
||||
blocTest<SignInCubit<int>, SignInState>(
|
||||
'calls signInWithEmailAndPassword with correct email/password',
|
||||
setUp: () {
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signInForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
seed: () => const SignInState(
|
||||
seed: () => SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
),
|
||||
act: (cubit) => cubit.signInWithEmailAndPassword(),
|
||||
act: (cubit) => cubit.submit(),
|
||||
verify: (_) {
|
||||
verify(
|
||||
() => authenticationRepository.signInWithEmailAndPassword(
|
||||
@@ -160,33 +319,85 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<SignInCubit, SignInState>(
|
||||
blocTest<SignInCubit<int>, SignInState>(
|
||||
'emits [submissionInProgress, submissionSuccess] '
|
||||
'when signInWithEmailAndPassword succeeds',
|
||||
setUp: () {
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signInForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
seed: () => const SignInState(
|
||||
seed: () => SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
),
|
||||
act: (cubit) => cubit.signInWithEmailAndPassword(),
|
||||
expect: () => const <SignInState>[
|
||||
act: (cubit) => cubit.submit(),
|
||||
expect: () => <SignInState>[
|
||||
SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.submissionInProgress,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
),
|
||||
SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.submissionSuccess,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
)
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<SignInCubit, SignInState>(
|
||||
blocTest<SignInCubit<int>, SignInState>(
|
||||
'emits [submissionInProgress, submissionFailure] '
|
||||
'when signInWithEmailAndPassword fails',
|
||||
setUp: () {
|
||||
@@ -195,27 +406,77 @@ void main() {
|
||||
email: any(named: 'email'),
|
||||
password: any(named: 'password'),
|
||||
),
|
||||
).thenThrow(Exception('oops'));
|
||||
).thenAnswer((_) async => Err(ServerException()));
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signInForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
seed: () => const SignInState(
|
||||
seed: () => SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
),
|
||||
act: (cubit) => cubit.signInWithEmailAndPassword(),
|
||||
expect: () => const <SignInState>[
|
||||
act: (cubit) => cubit.submit(),
|
||||
expect: () => <SignInState>[
|
||||
SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.submissionInProgress,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
),
|
||||
SignInState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
status: FormStatus.submissionFailure,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
)
|
||||
],
|
||||
);
|
||||
|
||||
@@ -19,36 +19,38 @@ import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
|
||||
void main() {
|
||||
const Email email = Email.dirty('email');
|
||||
const Password password = Password.dirty('password');
|
||||
final WyattForm form = WyattFormImpl(
|
||||
[
|
||||
FormInput(AuthFormField.email, const Email.pure()),
|
||||
FormInput(AuthFormField.password, const Password.pure())
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
);
|
||||
|
||||
group('SignInState', () {
|
||||
test('supports value comparisons', () {
|
||||
expect(const SignInState(), const SignInState());
|
||||
expect(
|
||||
SignInState(
|
||||
form: form,
|
||||
),
|
||||
SignInState(form: form),
|
||||
);
|
||||
});
|
||||
|
||||
test('returns same object when no properties are passed', () {
|
||||
expect(const SignInState().copyWith(), const SignInState());
|
||||
expect(
|
||||
SignInState(form: form).copyWith(),
|
||||
SignInState(form: form),
|
||||
);
|
||||
});
|
||||
|
||||
test('returns object with updated status when status is passed', () {
|
||||
expect(
|
||||
const SignInState().copyWith(status: FormStatus.pure),
|
||||
const SignInState(),
|
||||
);
|
||||
});
|
||||
|
||||
test('returns object with updated email when email is passed', () {
|
||||
expect(
|
||||
const SignInState().copyWith(email: email),
|
||||
const SignInState(email: email),
|
||||
);
|
||||
});
|
||||
|
||||
test('returns object with updated password when password is passed', () {
|
||||
expect(
|
||||
const SignInState().copyWith(password: password),
|
||||
const SignInState(password: password),
|
||||
SignInState(form: form).copyWith(status: FormStatus.invalid),
|
||||
SignInState(
|
||||
form: form,
|
||||
status: FormStatus.invalid,
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user