diff --git a/apps/lib/features/auth/presentation/cubits/register_cubit.dart b/apps/lib/features/auth/presentation/cubits/register_cubit.dart index d011a42..de17149 100644 --- a/apps/lib/features/auth/presentation/cubits/register_cubit.dart +++ b/apps/lib/features/auth/presentation/cubits/register_cubit.dart @@ -167,7 +167,7 @@ class RegisterCubit extends Cubit { } Future sendCodeSilently() async { - if (!state.isStep1Valid) return; + if (!state.isStep1Valid || state.isSending) return; emit(state.copyWith(isSending: true)); diff --git a/apps/test/features/auth/presentation/cubits/register_cubit_test.dart b/apps/test/features/auth/presentation/cubits/register_cubit_test.dart index a87784c..d85f41e 100644 --- a/apps/test/features/auth/presentation/cubits/register_cubit_test.dart +++ b/apps/test/features/auth/presentation/cubits/register_cubit_test.dart @@ -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( + '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((state) => state.isSending == true), + predicate( + (state) => + state.isSending == false && state.errorMessage == 'Network error', + ), + ], + verify: (_) { + verify(() => mockRepository.signupStart(any())).called(1); + }, + ); + + blocTest( + '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( + '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())); + }, + ); }); }