fix(auth): set failure status on sendCodeSilently error

This commit is contained in:
qzl
2026-02-26 11:25:51 +08:00
parent 8294c67d27
commit 4f9889bb3f
2 changed files with 35 additions and 4 deletions
@@ -153,7 +153,15 @@ class RegisterCubit extends Cubit<RegisterState> {
} }
Future<bool> resendCode() async { Future<bool> resendCode() async {
if (state.pendingEmail == null) return false; if (state.pendingEmail == null) {
emit(
state.copyWith(
status: FormzSubmissionStatus.failure,
errorMessage: '验证码发送失败,请返回上一步重试',
),
);
return false;
}
emit(state.copyWith(status: FormzSubmissionStatus.inProgress)); emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
@@ -200,7 +208,13 @@ class RegisterCubit extends Cubit<RegisterState> {
); );
} catch (e) { } catch (e) {
final message = e is ApiException ? e.message : '验证码发送失败,请重试'; final message = e is ApiException ? e.message : '验证码发送失败,请重试';
emit(state.copyWith(isSending: false, errorMessage: message)); emit(
state.copyWith(
isSending: false,
status: FormzSubmissionStatus.failure,
errorMessage: message,
),
);
} }
} }
} }
@@ -90,7 +90,7 @@ void main() {
); );
blocTest<RegisterCubit, RegisterState>( blocTest<RegisterCubit, RegisterState>(
'restores isSending to false and sets errorMessage on error', 'restores isSending to false and sets errorMessage and failure status on error',
build: () => cubit, build: () => cubit,
seed: () => RegisterState( seed: () => RegisterState(
username: const Username.dirty('testuser'), username: const Username.dirty('testuser'),
@@ -107,7 +107,9 @@ void main() {
predicate<RegisterState>((state) => state.isSending == true), predicate<RegisterState>((state) => state.isSending == true),
predicate<RegisterState>( predicate<RegisterState>(
(state) => (state) =>
state.isSending == false && state.errorMessage == 'Network error', state.isSending == false &&
state.errorMessage == 'Network error' &&
state.status == FormzSubmissionStatus.failure,
), ),
], ],
verify: (_) { verify: (_) {
@@ -148,6 +150,21 @@ void main() {
}); });
group('resendCode', () { group('resendCode', () {
blocTest<RegisterCubit, RegisterState>(
'returns false and sets failure status when pendingEmail is null',
build: () => cubit,
seed: () => RegisterState(pendingEmail: null),
act: (c) => c.resendCode(),
expect: () => [
isA<RegisterState>()
.having((s) => s.status, 'status', FormzSubmissionStatus.failure)
.having((s) => s.errorMessage, 'errorMessage', '验证码发送失败,请返回上一步重试'),
],
verify: (_) {
verifyNever(() => mockRepository.signupResend(any()));
},
);
blocTest<RegisterCubit, RegisterState>( blocTest<RegisterCubit, RegisterState>(
'returns true and sets status on success', 'returns true and sets status on success',
build: () => cubit, build: () => cubit,