fix(auth): prevent concurrent sendCodeSilently and add edge case tests
This commit is contained in:
@@ -167,7 +167,7 @@ class RegisterCubit extends Cubit<RegisterState> {
|
||||
}
|
||||
|
||||
Future<void> sendCodeSilently() async {
|
||||
if (!state.isStep1Valid) return;
|
||||
if (!state.isStep1Valid || state.isSending) return;
|
||||
|
||||
emit(state.copyWith(isSending: true));
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:social_app/features/auth/data/auth_repository.dart';
|
||||
import 'package:social_app/features/auth/data/models/auth_response.dart';
|
||||
import 'package:social_app/features/auth/data/models/signup_request.dart';
|
||||
import 'package:social_app/features/auth/presentation/cubits/register_cubit.dart';
|
||||
import 'package:social_app/core/api/api_exception.dart';
|
||||
|
||||
class MockAuthRepository extends Mock implements AuthRepository {}
|
||||
|
||||
@@ -86,5 +87,62 @@ void main() {
|
||||
verify(() => mockRepository.signupStart(any())).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<RegisterCubit, RegisterState>(
|
||||
'restores isSending to false and sets errorMessage on error',
|
||||
build: () => cubit,
|
||||
seed: () => RegisterState(
|
||||
username: const Username.dirty('testuser'),
|
||||
email: const Email.dirty('test@example.com'),
|
||||
password: const Password.dirty('password123'),
|
||||
),
|
||||
setUp: () {
|
||||
when(
|
||||
() => mockRepository.signupStart(any()),
|
||||
).thenThrow(ServerException('Network error'));
|
||||
},
|
||||
act: (c) => c.sendCodeSilently(),
|
||||
expect: () => [
|
||||
predicate<RegisterState>((state) => state.isSending == true),
|
||||
predicate<RegisterState>(
|
||||
(state) =>
|
||||
state.isSending == false && state.errorMessage == 'Network error',
|
||||
),
|
||||
],
|
||||
verify: (_) {
|
||||
verify(() => mockRepository.signupStart(any())).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<RegisterCubit, RegisterState>(
|
||||
'does not call signupStart when input is invalid',
|
||||
build: () => cubit,
|
||||
seed: () => RegisterState(
|
||||
username: const Username.dirty(''),
|
||||
email: const Email.dirty('invalid'),
|
||||
password: const Password.dirty(''),
|
||||
),
|
||||
act: (c) => c.sendCodeSilently(),
|
||||
expect: () => [],
|
||||
verify: (_) {
|
||||
verifyNever(() => mockRepository.signupStart(any()));
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<RegisterCubit, RegisterState>(
|
||||
'returns early when isSending is true',
|
||||
build: () => cubit,
|
||||
seed: () => RegisterState(
|
||||
username: const Username.dirty('testuser'),
|
||||
email: const Email.dirty('test@example.com'),
|
||||
password: const Password.dirty('password123'),
|
||||
isSending: true,
|
||||
),
|
||||
act: (c) => c.sendCodeSilently(),
|
||||
expect: () => [],
|
||||
verify: (_) {
|
||||
verifyNever(() => mockRepository.signupStart(any()));
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user