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;