Files
jiu/client/test/login_screen_test.dart
T
wangjia 5dd7c07138 feat(client): 网络恢复自动刷新 + 离线嗅探 + 连通性感知登录
网络嗅探与自动恢复:
- ConnectivityNotifier:在线时 30s 检测,离线时切换为 1min 嗅探
- 新增 networkRecoveryCountProvider:网络恢复时自增,各数据 provider
  通过 ref.watch 实现自动重新加载
- inventory/product/stock_in/stock_out/partner provider 均已接入

FutureBuilder 类屏幕:
- finance_screen + batch_tracking_screen 通过 ref.listen 监听恢复事件

修复问题 1:API 超时 10s/30s → 5s/15s,减少无网络时等待
修复问题 2:offline banner 文字由"写操作已禁用"改为实际行为描述

连通性感知:
- app 启动时(_AppBootstrap)立即 forceCheck,路由跳转前状态已就绪
- 登录页:watch connectivityProvider,离线时显示警告 banner
- 登录按钮:离线状态下直接提示,不等待超时

测试:
- login_screen_test + login_flow_test 通过 ConnectivityNotifier(skipInit: true)
  覆盖 provider,避免测试环境中 pending HTTP timer

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-18 19:52:17 +08:00

141 lines
4.7 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/providers/connectivity_provider.dart';
import 'package:jiu_client/screens/auth/login_screen.dart';
import 'package:shared_preferences/shared_preferences.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(
overrides: [
// skipInit=true:避免测试环境中发起真实 HTTP 请求导致 pending timer
connectivityProvider.overrideWith(
(_) => ConnectivityNotifier(skipInit: true),
),
],
child: MaterialApp.router(routerConfig: router),
);
}
void main() {
setUp(() {
SharedPreferences.setMockInitialValues({});
});
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);
});
testWidgets('selecting history item fills shop code input', (tester) async {
SharedPreferences.setMockInitialValues({
'login_history_hotels': ['S001', 'H001'],
});
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
await tester.pumpWidget(_buildTestApp());
await tester.pumpAndSettle();
await tester.tap(find.widgetWithText(TextFormField, '请输入门店编号'));
await tester.pumpAndSettle();
expect(find.text('S001'), findsOneWidget);
await tester.tap(find.text('S001').last);
await tester.pumpAndSettle();
final field = tester.widget<TextFormField>(
find.byType(TextFormField).first,
);
expect(field.controller!.text, 'S001');
});
});
}