- 将资料编辑页重构为可交互表单,支持昵称、签名编辑与头像更换 - 新增 DioClient 本地缓存读取与写入方法,支持离线读取策略快照 - 设备策略切换后自动使本地缓存失效,避免数据不一致 - Home Tab 切换时选中态跟随可见页面,滑动过程中实时更新底栏高亮 - 系统设置页引入 loading 遮罩层,防止切换开关时重复操作 - 个人资料页接入 profileController 展示真实用户信息与加载状态 - 适配深色模式,修正 home_tab 中头像与文字颜色硬编码问题
84 lines
3.0 KiB
Dart
84 lines
3.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:ttstd_family_care/core/network/api_exception.dart';
|
|
import 'package:ttstd_family_care/features/profile/data/profile_repository_impl.dart';
|
|
import 'package:ttstd_family_care/features/profile/domain/profile_models.dart';
|
|
import 'package:ttstd_family_care/features/profile/domain/profile_repository.dart';
|
|
import 'package:ttstd_family_care/features/profile/presentation/profile_controller.dart';
|
|
|
|
class FakeProfileRepository implements ProfileRepository {
|
|
final pendingLoad = Completer<UserProfile>();
|
|
bool failSave = false;
|
|
int saves = 0;
|
|
|
|
@override
|
|
Future<UserProfile> load() => pendingLoad.future;
|
|
|
|
@override
|
|
Future<UserProfile> save(String nickname, String signature) async {
|
|
saves++;
|
|
if (failSave) throw const ApiException(code: '400', message: 'Save failed');
|
|
return UserProfile(id: '7', nickname: nickname, signature: signature);
|
|
}
|
|
|
|
@override
|
|
Future<UserProfile?> chooseAvatar() async => null;
|
|
}
|
|
|
|
void main() {
|
|
test(
|
|
'late loads cannot overwrite saved profile, failed saves retain data',
|
|
() async {
|
|
final repository = FakeProfileRepository();
|
|
final container = ProviderContainer(
|
|
overrides: [profileRepositoryProvider.overrideWithValue(repository)],
|
|
);
|
|
addTearDown(container.dispose);
|
|
container.listen(profileControllerProvider, (_, _) {});
|
|
await Future<void>.delayed(Duration.zero);
|
|
final controller = container.read(profileControllerProvider.notifier);
|
|
|
|
expect(await controller.save(' 家人 ', ' 你好 '), isTrue);
|
|
repository.pendingLoad.complete(
|
|
const UserProfile(id: '7', nickname: 'old'),
|
|
);
|
|
await Future<void>.delayed(Duration.zero);
|
|
expect(container.read(profileControllerProvider).profile?.nickname, '家人');
|
|
expect(
|
|
container.read(profileControllerProvider).profile?.signature,
|
|
'你好',
|
|
);
|
|
|
|
await controller.changeAvatar();
|
|
expect(container.read(profileControllerProvider).profile?.nickname, '家人');
|
|
expect(container.read(profileControllerProvider).saving, isFalse);
|
|
|
|
repository.failSave = true;
|
|
expect(await controller.save('new', ''), isFalse);
|
|
expect(container.read(profileControllerProvider).profile?.nickname, '家人');
|
|
expect(container.read(profileControllerProvider).alert, 'Save failed');
|
|
|
|
final calls = repository.saves;
|
|
expect(await controller.save(' ', ''), isFalse);
|
|
expect(repository.saves, calls);
|
|
expect(
|
|
container.read(profileControllerProvider).issue,
|
|
ProfileIssue.nicknameInvalid,
|
|
);
|
|
},
|
|
);
|
|
|
|
test('numeric user IDs and absent optional profile fields decode', () {
|
|
final profile = UserProfile.fromJson({
|
|
'id': 7,
|
|
'nickname': null,
|
|
'signature': null,
|
|
});
|
|
expect(profile.id, '7');
|
|
expect(profile.nickname, '');
|
|
expect(profile.signature, '');
|
|
});
|
|
}
|