Files
jiu/client/test/login_screen_test.dart
T
wangjia 37112d6599 feat(client): 登录历史下拉、macOS 兼容修复、UI 优化
- 替换 flutter_secure_storage 为 shared_preferences,解决 macOS Keychain 签名报错
- 登录页门店编号/用户名支持历史下拉(最多5条,LRU 淘汰),使用 Overlay + CompositedTransformFollower 实现浮层
- logout 仅删除 auth token,保留登录历史
- AppShell 时钟抽为独立 _ClockWidget,避免每秒 setState 导致 TabBarView 叠影
- ShellRoute 子路由改用 NoTransitionPage,消除切换页面时的过渡动画叠影
- 新增 auth_state_test、auth_repository_test、login_screen_test 共 14 个单元/Widget 测试

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-05 01:21:40 +08:00

104 lines
3.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
import 'package:jiu_client/screens/auth/login_screen.dart';
/// Wrap the screen in the minimum required providers.
Widget _buildTestApp() {
final router = GoRouter(
initialLocation: '/login',
routes: [
GoRoute(
path: '/login',
builder: (_, __) => const LoginScreen(),
),
GoRoute(
path: '/stock-in',
builder: (_, __) => const Scaffold(body: Text('stock-in')),
),
],
);
return ProviderScope(
child: MaterialApp.router(routerConfig: router),
);
}
void main() {
group('LoginScreen UI', () {
testWidgets('renders all three input fields and login button',
(tester) async {
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
await tester.pumpWidget(_buildTestApp());
await tester.pump();
expect(find.byType(TextFormField), findsNWidgets(3));
expect(find.text('门店编号'), findsOneWidget);
expect(find.text('用户名'), findsOneWidget);
expect(find.text('密码'), findsOneWidget);
expect(find.text('登 录'), findsOneWidget);
});
testWidgets('shows validation errors when submitting empty form',
(tester) async {
// Use a desktop-sized surface to avoid overflow
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
await tester.pumpWidget(_buildTestApp());
await tester.pump();
await tester.tap(find.text('登 录'));
await tester.pump();
// Hint text and validator error share the same string, so both appear
expect(find.text('请输入门店编号'), findsAtLeastNWidgets(1));
expect(find.text('请输入用户名'), findsAtLeastNWidgets(1));
expect(find.text('请输入密码'), findsAtLeastNWidgets(1));
});
testWidgets('password toggle changes obscure state', (tester) async {
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
await tester.pumpWidget(_buildTestApp());
await tester.pump();
// Initially password is obscured — visibility_off icon shown
expect(find.byIcon(Icons.visibility_off), findsOneWidget);
await tester.tap(find.byIcon(Icons.visibility_off));
await tester.pump();
// After tap — visibility icon shown
expect(find.byIcon(Icons.visibility), findsOneWidget);
});
testWidgets('button is present and tappable with filled form', (tester) async {
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
await tester.pumpWidget(_buildTestApp());
await tester.pump();
await tester.enterText(
find.widgetWithText(TextFormField, '请输入门店编号'), 'H001');
await tester.enterText(
find.widgetWithText(TextFormField, '请输入用户名'), 'admin');
await tester.enterText(
find.widgetWithText(TextFormField, '请输入密码'), 'password123');
// Button exists and is enabled
final btn = tester.widget<ElevatedButton>(find.byType(ElevatedButton));
expect(btn.onPressed, isNotNull);
});
});
}