test(auth): add tests for all features
This commit is contained in:
@@ -17,163 +17,298 @@
|
||||
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('SignUpCubit', () {
|
||||
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.signUpForm,
|
||||
);
|
||||
|
||||
late MockFormRepository formRepository;
|
||||
late AuthenticationRepository<int> authenticationRepository;
|
||||
|
||||
setUp(() {
|
||||
authenticationRepository = MockAuthenticationRepository();
|
||||
authenticationCubit = MockAuthenticationCubit();
|
||||
formRepository = MockFormRepository();
|
||||
|
||||
when(
|
||||
() => authenticationRepository.signUp(
|
||||
email: any(named: 'email'),
|
||||
password: any(named: 'password'),
|
||||
),
|
||||
).thenAnswer((_) async => 'uid');
|
||||
).thenAnswer((_) async => Ok(account));
|
||||
|
||||
when(
|
||||
() => authenticationCubit.start(),
|
||||
).thenReturn(true);
|
||||
() => authenticationRepository.formRepository,
|
||||
).thenAnswer((_) => formRepository);
|
||||
|
||||
when(
|
||||
() => authenticationCubit.stop(),
|
||||
).thenReturn(true);
|
||||
() => formRepository.accessForm(AuthFormName.signUpForm),
|
||||
).thenAnswer((_) => form);
|
||||
});
|
||||
|
||||
test('initial state is SignUpState', () {
|
||||
expect(
|
||||
SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
).state,
|
||||
const SignUpState(data: FormData.empty()),
|
||||
SignUpState(form: form),
|
||||
);
|
||||
});
|
||||
|
||||
group('emailChanged', () {
|
||||
blocTest<SignUpCubit, SignUpState>(
|
||||
blocTest<SignUpCubit<int>, SignUpState>(
|
||||
'emits [invalid] when email/password are invalid',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
act: (cubit) => cubit.emailChanged(invalidEmailString),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
email: invalidEmail,
|
||||
SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(invalidEmailString),
|
||||
),
|
||||
FormInput(AuthFormField.password, const Password.pure())
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.invalid,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<SignUpCubit, SignUpState>(
|
||||
blocTest<SignUpCubit<int>, SignUpState>(
|
||||
'emits [valid] when email/password are valid',
|
||||
setUp: () {
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signUpForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.pure(),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
seed: () => SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.pure(),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.invalid,
|
||||
),
|
||||
act: (cubit) => cubit.emailChanged(validEmailString),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.valid,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
group('passwordChanged', () {
|
||||
blocTest<SignUpCubit, SignUpState>(
|
||||
blocTest<SignUpCubit<int>, SignUpState>(
|
||||
'emits [invalid] when email/password are invalid',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
act: (cubit) => cubit.passwordChanged(invalidPasswordString),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
password: invalidPassword,
|
||||
SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.pure(),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(invalidPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.invalid,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<SignUpCubit, SignUpState>(
|
||||
blocTest<SignUpCubit<int>, SignUpState>(
|
||||
'emits [valid] when email/password are valid',
|
||||
setUp: () {
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signUpForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.pure(),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
email: validEmail,
|
||||
data: FormData.empty(),
|
||||
seed: () => SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.pure(),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.invalid,
|
||||
),
|
||||
act: (cubit) => cubit.passwordChanged(validPasswordString),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.valid,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
group('signUpFormSubmitted', () {
|
||||
blocTest<SignUpCubit, SignUpState>(
|
||||
group('submit', () {
|
||||
blocTest<SignUpCubit<int>, SignUpState>(
|
||||
'does nothing when status is not validated',
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
act: (cubit) => cubit.signUpFormSubmitted(),
|
||||
act: (cubit) => cubit.submit(),
|
||||
expect: () => const <SignUpState>[],
|
||||
);
|
||||
|
||||
blocTest<SignUpCubit, SignUpState>(
|
||||
'calls signUp with correct email/password/confirmedPassword',
|
||||
blocTest<SignUpCubit<int>, SignUpState>(
|
||||
'calls signUp with correct email/password',
|
||||
setUp: () {
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signUpForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
seed: () => SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
act: (cubit) => cubit.signUpFormSubmitted(),
|
||||
act: (cubit) => cubit.submit(),
|
||||
verify: (_) {
|
||||
verify(
|
||||
() => authenticationRepository.signUp(
|
||||
@@ -184,37 +319,85 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<SignUpCubit, SignUpState>(
|
||||
blocTest<SignUpCubit<int>, SignUpState>(
|
||||
'emits [submissionInProgress, submissionSuccess] '
|
||||
'when signUp succeeds',
|
||||
setUp: () {
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signUpForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
act: (cubit) => cubit.signUpFormSubmitted(),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
status: FormStatus.submissionInProgress,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
seed: () => SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
const SignUpState(
|
||||
status: FormStatus.valid,
|
||||
),
|
||||
act: (cubit) => cubit.submit(),
|
||||
expect: () => <SignUpState>[
|
||||
SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.submissionInProgress,
|
||||
),
|
||||
SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.submissionSuccess,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
)
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<SignUpCubit, SignUpState>(
|
||||
blocTest<SignUpCubit<int>, SignUpState>(
|
||||
'emits [submissionInProgress, submissionFailure] '
|
||||
'when signUp fails',
|
||||
setUp: () {
|
||||
@@ -223,31 +406,77 @@ void main() {
|
||||
email: any(named: 'email'),
|
||||
password: any(named: 'password'),
|
||||
),
|
||||
).thenThrow(Exception('oops'));
|
||||
).thenAnswer((_) async => Err(ServerException()));
|
||||
when(
|
||||
() => formRepository.accessForm(AuthFormName.signUpForm),
|
||||
).thenAnswer(
|
||||
(_) => WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
);
|
||||
},
|
||||
build: () => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: const FormData.empty(),
|
||||
),
|
||||
seed: () => const SignUpState(
|
||||
status: FormStatus.valid,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
act: (cubit) => cubit.signUpFormSubmitted(),
|
||||
expect: () => <SignUpState>[
|
||||
const SignUpState(
|
||||
status: FormStatus.submissionInProgress,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
seed: () => SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
const SignUpState(
|
||||
status: FormStatus.valid,
|
||||
),
|
||||
act: (cubit) => cubit.submit(),
|
||||
expect: () => <SignUpState>[
|
||||
SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.submissionInProgress,
|
||||
),
|
||||
SignUpState(
|
||||
form: WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
const Email.dirty(validEmailString),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
const Password.dirty(validPasswordString),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
status: FormStatus.submissionFailure,
|
||||
email: validEmail,
|
||||
password: validPassword,
|
||||
data: FormData.empty(),
|
||||
)
|
||||
],
|
||||
);
|
||||
|
||||
@@ -19,93 +19,37 @@ 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 String passwordString = 'password';
|
||||
const Password password = Password.dirty(passwordString);
|
||||
final WyattForm form = WyattFormImpl(
|
||||
[
|
||||
FormInput(AuthFormField.email, const Email.pure()),
|
||||
FormInput(AuthFormField.password, const Password.pure())
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
);
|
||||
|
||||
group('SignUpState', () {
|
||||
test('supports value comparisons', () {
|
||||
expect(
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
),
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
SignUpState(
|
||||
form: form,
|
||||
),
|
||||
SignUpState(form: form),
|
||||
);
|
||||
});
|
||||
|
||||
test('returns same object when no properties are passed', () {
|
||||
expect(
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
).copyWith(),
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
),
|
||||
SignUpState(form: form).copyWith(),
|
||||
SignUpState(form: form),
|
||||
);
|
||||
});
|
||||
|
||||
test('returns object with updated status when status is passed', () {
|
||||
expect(
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
).copyWith(status: FormStatus.pure),
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('returns object with updated email when email is passed', () {
|
||||
expect(
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
).copyWith(email: email),
|
||||
const SignUpState(
|
||||
email: email,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('returns object with updated password when password is passed', () {
|
||||
expect(
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
).copyWith(password: password),
|
||||
const SignUpState(
|
||||
password: password,
|
||||
data: FormData.empty(),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test(
|
||||
'returns object with updated data'
|
||||
' when data is passed', () {
|
||||
expect(
|
||||
const SignUpState(
|
||||
data: FormData.empty(),
|
||||
).copyWith(
|
||||
data: const FormData(
|
||||
[
|
||||
FormInput(
|
||||
'field',
|
||||
Name.pure(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SignUpState(
|
||||
data: FormData(
|
||||
[
|
||||
FormInput(
|
||||
'field',
|
||||
Name.pure(),
|
||||
),
|
||||
],
|
||||
),
|
||||
SignUpState(form: form).copyWith(status: FormStatus.invalid),
|
||||
SignUpState(
|
||||
form: form,
|
||||
status: FormStatus.invalid,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user