Files
platforms/apps/service_app/test/ui/login_page_test.dart
2026-09-04 23:00:55 +08:00

97 lines
4.0 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 功能描述:验证工作人员端登录页字段、必填校验、提交行为和接口错误中文化。
// 版本1.2.0
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:service_app/app/dependencies.dart';
import 'package:service_app/data/services/api_client.dart';
import 'package:service_app/data/services/secure_session_store.dart';
import 'package:service_app/ui/features/auth/login_page.dart';
/// 记录登录调用,验证本地校验是否正确阻止请求。
class _RecordingStaffSession extends StaffSession {
_RecordingStaffSession() : super(SecureSessionStore());
int loginCallCount = 0;
String? lastPhone;
String? lastPassword;
@override
Future<void> login(String phone, String password) async {
loginCallCount += 1;
lastPhone = phone;
lastPassword = password;
}
}
/// 验证工作人员端登录界面的关键交互。
void main() {
testWidgets('staff login explains the single shared entry', (tester) async {
final session = StaffSession(SecureSessionStore());
await tester.pumpWidget(MaterialApp(home: LoginPage(session: session)));
expect(find.text('瓶安芯服务工作台'), findsOneWidget);
expect(find.text('配送、安装维修与安检共用入口'), findsOneWidget);
expect(find.byType(TextField), findsNWidgets(2));
});
testWidgets('手机号错误且密码为空时同时提示并阻止登录调用', (tester) async {
final session = _RecordingStaffSession();
await tester.pumpWidget(MaterialApp(home: LoginPage(session: session)));
await tester.enterText(find.byType(TextField).first, '+8613800138000');
await tester.tap(find.text('进入工作台'));
await tester.pump();
expect(find.text('请输入正确的11位手机号'), findsOneWidget);
expect(find.text('请输入密码'), findsOneWidget);
expect(find.text('Invalid Argument'), findsNothing);
expect(session.loginCallCount, 0);
expect(tester.widget<TextField>(find.byType(TextField).first).focusNode?.hasFocus, isTrue);
});
testWidgets('合法手机号配空密码时提示密码必填并阻止登录调用', (tester) async {
final session = _RecordingStaffSession();
await tester.pumpWidget(MaterialApp(home: LoginPage(session: session)));
await tester.enterText(find.byType(TextField).first, '13800138000');
await tester.tap(find.text('进入工作台'));
await tester.pump();
expect(find.text('请输入正确的11位手机号'), findsNothing);
expect(find.text('请输入密码'), findsOneWidget);
expect(session.loginCallCount, 0);
expect(tester.widget<TextField>(find.byType(TextField).at(1)).focusNode?.hasFocus, isTrue);
});
testWidgets('修改字段清除对应错误并由回车原样提交密码', (tester) async {
final session = _RecordingStaffSession();
await tester.pumpWidget(MaterialApp(home: LoginPage(session: session)));
await tester.tap(find.text('进入工作台'));
await tester.pump();
await tester.enterText(find.byType(TextField).first, '13800138000');
await tester.pump();
expect(find.text('请输入正确的11位手机号'), findsNothing);
expect(find.text('请输入密码'), findsOneWidget);
await tester.enterText(find.byType(TextField).at(1), ' 密码123 ');
await tester.pump();
expect(find.text('请输入密码'), findsNothing);
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pump();
expect(session.loginCallCount, 1);
expect(session.lastPhone, '13800138000');
expect(session.lastPassword, ' 密码123 ');
});
test('接口错误优先按错误码中文化并屏蔽未知英文', () {
expect(localizeApiErrorMessage(1704, 'Invalid Argument'), '请求参数错误');
expect(localizeApiErrorMessage(1108, 'Password Incorrect'), '密码错误');
expect(localizeApiErrorMessage(9999, '业务处理失败'), '业务处理失败');
expect(localizeApiErrorMessage(9999, 'Unexpected Error'), '操作失败,请稍后重试');
});
}