fix(auth): improve resendCode with status tracking and return value

This commit is contained in:
qzl
2026-02-26 11:12:04 +08:00
parent d1e224ece4
commit 8235ac5cd9
3 changed files with 76 additions and 7 deletions
@@ -152,17 +152,28 @@ class RegisterCubit extends Cubit<RegisterState> {
} }
} }
Future<void> resendCode() async { Future<bool> resendCode() async {
if (state.pendingEmail == null) return; if (state.pendingEmail == null) return false;
emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
try { try {
await _repository.signupResend( await _repository.signupResend(
SignupResendRequest(email: state.pendingEmail!), SignupResendRequest(email: state.pendingEmail!),
); );
emit(state.copyWith(codeSent: true)); emit(
state.copyWith(status: FormzSubmissionStatus.success, codeSent: true),
);
return true;
} catch (e) { } catch (e) {
final message = e is ApiException ? e.message : e.toString(); final message = e is ApiException ? e.message : '验证码发送失败,请重试';
emit(state.copyWith(errorMessage: message)); emit(
state.copyWith(
status: FormzSubmissionStatus.failure,
errorMessage: message,
),
);
return false;
} }
} }
@@ -94,9 +94,9 @@ class _RegisterVerificationViewState extends State<RegisterVerificationView> {
Future<void> _handleResendCode() async { Future<void> _handleResendCode() async {
final cubit = context.read<RegisterCubit>(); final cubit = context.read<RegisterCubit>();
await cubit.resendCode(); final success = await cubit.resendCode();
if (cubit.state.codeSent && mounted) { if (success && mounted) {
_startCountdown(); _startCountdown();
} }
} }
@@ -21,6 +21,7 @@ void main() {
registerFallbackValue( registerFallbackValue(
SignupStartRequest(username: '', email: '', password: ''), SignupStartRequest(username: '', email: '', password: ''),
); );
registerFallbackValue(SignupResendRequest(email: ''));
}); });
tearDown(() { tearDown(() {
@@ -145,4 +146,61 @@ void main() {
}, },
); );
}); });
group('resendCode', () {
blocTest<RegisterCubit, RegisterState>(
'returns true and sets status on success',
build: () => cubit,
seed: () => RegisterState(pendingEmail: 'test@example.com'),
setUp: () {
when(() => mockRepository.signupResend(any())).thenAnswer(
(_) async => SignupStartResponse(
status: 'ok',
email: 'test@example.com',
message: 'Code sent',
),
);
},
act: (c) => c.resendCode(),
expect: () => [
isA<RegisterState>().having(
(s) => s.status,
'status',
FormzSubmissionStatus.inProgress,
),
isA<RegisterState>().having(
(s) => s.status,
'status',
FormzSubmissionStatus.success,
),
],
verify: (_) {
verify(() => mockRepository.signupResend(any())).called(1);
},
);
blocTest<RegisterCubit, RegisterState>(
'returns false on error',
build: () => cubit,
seed: () => RegisterState(pendingEmail: 'test@example.com'),
setUp: () {
when(
() => mockRepository.signupResend(any()),
).thenThrow(ServerException('Network error'));
},
act: (c) => c.resendCode(),
expect: () => [
isA<RegisterState>().having(
(s) => s.status,
'status',
FormzSubmissionStatus.inProgress,
),
isA<RegisterState>().having(
(s) => s.status,
'status',
FormzSubmissionStatus.failure,
),
],
);
});
} }