fix(auth): 登录异常改为居中吐司

This commit is contained in:
Zhou Weidong committed 2026-10-22 09:57:57 +08:00
1 parent 11e37a92b5
commit d076c57283
16 files changed
+341 -175

No files matched your search

+35 -29
View File
@@ -78,7 +78,7 @@ void main() {
expect(find.text('Enter your email'), findsOneWidget);
expect(find.text('Welcome back'), findsOneWidget);
expect(find.byKey(const Key('centered-error-dialog')), findsNothing);
expect(find.byKey(const Key('centered-toast')), findsNothing);
});
testWidgets(
@@ -95,11 +95,11 @@ void main() {
find.text('Password needs both letters and numbers'),
findsOneWidget,
);
expect(find.byKey(const Key('centered-error-dialog')), findsNothing);
expect(find.byKey(const Key('centered-toast')), findsNothing);
},
);
testWidgets('should_show_centered_dialog_when_login_api_returns_an_error', (
testWidgets('should_show_centered_toast_when_login_api_returns_an_error', (
tester,
) async {
final api = _FailingAuthApi(loginMessage: 'Invalid email or password');
@@ -110,13 +110,13 @@ void main() {
await tester.tap(find.byKey(const Key('auth-submit')));
await tester.pumpAndSettle();
final dialog = find.byKey(const Key('centered-error-dialog'));
expect(dialog, findsOneWidget);
expect(find.text('Login failed'), findsOneWidget);
final toast = find.byKey(const Key('centered-toast'));
expect(toast, findsOneWidget);
expect(find.text('Invalid email or password'), findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
expect(api.loginCalls, 1);
expect(
tester.getCenter(dialog),
tester.getCenter(toast),
offsetMoreOrLessEquals(
tester.getCenter(find.byType(Scaffold)),
epsilon: 1,
@@ -129,32 +129,29 @@ void main() {
isNotNull,
);
await tester.tap(find.byKey(const Key('centered-error-confirm')));
await tester.pumpAndSettle();
expect(dialog, findsNothing);
await _pumpToastAway(tester);
expect(toast, findsNothing);
});
testWidgets(
'should_show_registration_failed_title_when_register_api_returns_an_error',
(tester) async {
final api = _FailingAuthApi(registerMessage: 'Email already exists');
await pumpAuthPage(tester, signUp: true, api: api);
await _enterValidCredentials(tester);
testWidgets('should_show_centered_toast_when_register_api_returns_an_error', (
tester,
) async {
final api = _FailingAuthApi(registerMessage: 'Email already exists');
await pumpAuthPage(tester, signUp: true, api: api);
await _enterValidCredentials(tester);
await tester.ensureVisible(find.byKey(const Key('auth-submit')));
await tester.tap(find.byKey(const Key('auth-submit')));
await tester.pumpAndSettle();
await tester.ensureVisible(find.byKey(const Key('auth-submit')));
await tester.tap(find.byKey(const Key('auth-submit')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('centered-error-dialog')), findsOneWidget);
expect(find.text('Registration failed'), findsOneWidget);
expect(find.text('Email already exists'), findsOneWidget);
expect(api.registerCalls, 1);
expect(find.byKey(const Key('centered-toast')), findsOneWidget);
expect(find.text('Email already exists'), findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
expect(api.registerCalls, 1);
await tester.tapAt(const Offset(8, 8));
await tester.pumpAndSettle();
expect(find.byKey(const Key('centered-error-dialog')), findsNothing);
},
);
await _pumpToastAway(tester);
expect(find.byKey(const Key('centered-toast')), findsNothing);
});
testWidgets(
'should_show_network_error_when_login_throws_an_unknown_exception',
@@ -167,8 +164,12 @@ void main() {
await tester.tap(find.byKey(const Key('auth-submit')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('centered-error-dialog')), findsOneWidget);
expect(find.byKey(const Key('centered-toast')), findsOneWidget);
expect(find.text('Network error, please try again'), findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
await _pumpToastAway(tester);
expect(find.byKey(const Key('centered-toast')), findsNothing);
},
);
@@ -422,6 +423,11 @@ Future<void> _enterValidCredentials(WidgetTester tester) async {
await tester.enterText(find.byKey(const Key('auth-password')), 'abcd1234');
}
Future<void> _pumpToastAway(WidgetTester tester) async {
await tester.pump(const Duration(seconds: 3));
await tester.pumpAndSettle();
}
void _expectAboveKeyboard(WidgetTester tester, Key fieldKey, double inset) {
final screenHeight =
tester.view.physicalSize.height / tester.view.devicePixelRatio;
+18 -7
View File
@@ -98,10 +98,10 @@ void main() {
expect(find.text('Enter a valid phone number'), findsOneWidget);
expect(adapter.requests, isEmpty);
expect(find.byKey(const Key('centered-error-dialog')), findsNothing);
expect(find.byKey(const Key('centered-toast')), findsNothing);
});
testWidgets('should_show_centered_dialog_when_otp_request_fails', (
testWidgets('should_show_centered_toast_when_otp_request_fails', (
tester,
) async {
final api = _FailingPhoneAuthApi(sendOtpMessage: 'Too many OTP requests');
@@ -117,9 +117,9 @@ void main() {
await tester.tap(find.byKey(const Key('auth-send-code')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('centered-error-dialog')), findsOneWidget);
expect(find.text('Login failed'), findsOneWidget);
expect(find.byKey(const Key('centered-toast')), findsOneWidget);
expect(find.text('Too many OTP requests'), findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
expect(api.sendOtpCalls, 1);
expect(
tester
@@ -127,9 +127,12 @@ void main() {
.onPressed,
isNotNull,
);
await _pumpToastAway(tester);
expect(find.byKey(const Key('centered-toast')), findsNothing);
});
testWidgets('should_show_centered_dialog_when_phone_login_fails', (
testWidgets('should_show_centered_toast_when_phone_login_fails', (
tester,
) async {
final api = _FailingPhoneAuthApi(loginMessage: 'Invalid verification code');
@@ -146,9 +149,9 @@ void main() {
await tester.tap(find.byKey(const Key('auth-phone-submit')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('centered-error-dialog')), findsOneWidget);
expect(find.text('Login failed'), findsOneWidget);
expect(find.byKey(const Key('centered-toast')), findsOneWidget);
expect(find.text('Invalid verification code'), findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
expect(api.loginWithPhoneCalls, 1);
expect(
tester
@@ -156,6 +159,9 @@ void main() {
.onPressed,
isNotNull,
);
await _pumpToastAway(tester);
expect(find.byKey(const Key('centered-toast')), findsNothing);
});
testWidgets('should_keep_code_above_ios_keyboard', (tester) async {
@@ -191,6 +197,11 @@ void main() {
});
}
Future<void> _pumpToastAway(WidgetTester tester) async {
await tester.pump(const Duration(seconds: 3));
await tester.pumpAndSettle();
}
class _FailingPhoneAuthApi extends AuthApi {
_FailingPhoneAuthApi({
this.sendOtpMessage = 'otp failed',