fix(account): 本地化网络异常文案
This commit is contained in:
1 parent
9b3ab9d5de
commit
27ab7cebfb
5 files changed
+90
-11
No files matched your search
@@ -24,9 +24,7 @@ class AccountDevicesLogic extends ChangeNotifier {
|
|||||||
try {
|
try {
|
||||||
devices = await _repository.devices();
|
devices = await _repository.devices();
|
||||||
} catch (failure) {
|
} catch (failure) {
|
||||||
error = failure is ApiException
|
error = failure is ApiException ? failure.message : 'networkError';
|
||||||
? failure.message
|
|
||||||
: 'accountDevicesLoadFailed';
|
|
||||||
} finally {
|
} finally {
|
||||||
loading = false;
|
loading = false;
|
||||||
_notify();
|
_notify();
|
||||||
|
|||||||
@@ -25,9 +25,7 @@ class AccountExportLogic extends ChangeNotifier {
|
|||||||
try {
|
try {
|
||||||
data = await _repository.export();
|
data = await _repository.export();
|
||||||
} catch (failure) {
|
} catch (failure) {
|
||||||
error = failure is ApiException
|
error = failure is ApiException ? failure.message : 'networkError';
|
||||||
? failure.message
|
|
||||||
: 'accountExportLoadFailed';
|
|
||||||
} finally {
|
} finally {
|
||||||
loading = false;
|
loading = false;
|
||||||
_notify();
|
_notify();
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class AccountProfileLogic extends ChangeNotifier {
|
|||||||
try {
|
try {
|
||||||
profile = await _repository.profile();
|
profile = await _repository.profile();
|
||||||
} catch (failure) {
|
} catch (failure) {
|
||||||
error = _message(failure) ?? 'accountProfileLoadFailed';
|
error = _message(failure);
|
||||||
} finally {
|
} finally {
|
||||||
loading = false;
|
loading = false;
|
||||||
_notify();
|
_notify();
|
||||||
@@ -68,7 +68,7 @@ class AccountProfileLogic extends ChangeNotifier {
|
|||||||
await onProfileUpdated(next);
|
await onProfileUpdated(next);
|
||||||
return true;
|
return true;
|
||||||
} catch (failure) {
|
} catch (failure) {
|
||||||
error = _message(failure) ?? 'accountProfileSaveFailed';
|
error = _message(failure);
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
saving = false;
|
saving = false;
|
||||||
@@ -76,8 +76,8 @@ class AccountProfileLogic extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String? _message(Object failure) =>
|
String _message(Object failure) =>
|
||||||
failure is ApiException ? failure.message : null;
|
failure is ApiException ? failure.message : 'networkError';
|
||||||
|
|
||||||
void _notify() {
|
void _notify() {
|
||||||
if (!_disposed) notifyListeners();
|
if (!_disposed) notifyListeners();
|
||||||
|
|||||||
@@ -101,6 +101,83 @@ void main() {
|
|||||||
|
|
||||||
expect(find.byKey(const Key('account-export-content')), findsOneWidget);
|
expect(find.byKey(const Key('account-export-content')), findsOneWidget);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('should_show_localized_network_copy_when_profile_load_fails', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final repository = FakeAccountRepository()..throwUnexpected = true;
|
||||||
|
final auth = await _signedInAuth();
|
||||||
|
await _pumpPage(
|
||||||
|
tester,
|
||||||
|
AccountProfilePage(),
|
||||||
|
repository: repository,
|
||||||
|
auth: auth,
|
||||||
|
locale: const Locale('zh'),
|
||||||
|
);
|
||||||
|
final l10n = await AppLocalizations.delegate.load(const Locale('zh'));
|
||||||
|
|
||||||
|
expect(find.text(l10n.networkError), findsOneWidget);
|
||||||
|
expect(find.text('accountProfileLoadFailed'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('should_show_localized_network_copy_when_profile_save_fails', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final repository = FakeAccountRepository();
|
||||||
|
final auth = await _signedInAuth();
|
||||||
|
await _pumpPage(
|
||||||
|
tester,
|
||||||
|
AccountProfilePage(),
|
||||||
|
repository: repository,
|
||||||
|
auth: auth,
|
||||||
|
);
|
||||||
|
repository.throwUnexpected = true;
|
||||||
|
await tester.enterText(
|
||||||
|
find.byKey(const Key('account-nickname')),
|
||||||
|
'Nadia Baru',
|
||||||
|
);
|
||||||
|
await tester.tap(find.byKey(const Key('account-profile-save')));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
|
||||||
|
|
||||||
|
expect(find.text(l10n.networkError), findsOneWidget);
|
||||||
|
expect(find.text('accountProfileSaveFailed'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('should_show_localized_network_copy_when_devices_fail', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final repository = FakeAccountRepository()..throwUnexpected = true;
|
||||||
|
final auth = await _signedInAuth();
|
||||||
|
await _pumpPage(
|
||||||
|
tester,
|
||||||
|
AccountDevicesPage(),
|
||||||
|
repository: repository,
|
||||||
|
auth: auth,
|
||||||
|
locale: const Locale('id'),
|
||||||
|
);
|
||||||
|
final l10n = await AppLocalizations.delegate.load(const Locale('id'));
|
||||||
|
|
||||||
|
expect(find.text(l10n.networkError), findsOneWidget);
|
||||||
|
expect(find.text('accountDevicesLoadFailed'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('should_show_localized_network_copy_when_export_fails', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final repository = FakeAccountRepository()..throwUnexpected = true;
|
||||||
|
final auth = await _signedInAuth();
|
||||||
|
await _pumpPage(
|
||||||
|
tester,
|
||||||
|
AccountExportPage(),
|
||||||
|
repository: repository,
|
||||||
|
auth: auth,
|
||||||
|
);
|
||||||
|
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
|
||||||
|
|
||||||
|
expect(find.text(l10n.networkError), findsOneWidget);
|
||||||
|
expect(find.text('accountExportLoadFailed'), findsNothing);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<AuthController> _signedInAuth() async {
|
Future<AuthController> _signedInAuth() async {
|
||||||
@@ -124,6 +201,7 @@ Future<void> _pumpPage(
|
|||||||
Widget page, {
|
Widget page, {
|
||||||
required FakeAccountRepository repository,
|
required FakeAccountRepository repository,
|
||||||
required AuthController auth,
|
required AuthController auth,
|
||||||
|
Locale locale = const Locale('en'),
|
||||||
}) async {
|
}) async {
|
||||||
tester.view.physicalSize = const Size(390, 844);
|
tester.view.physicalSize = const Size(390, 844);
|
||||||
tester.view.devicePixelRatio = 1;
|
tester.view.devicePixelRatio = 1;
|
||||||
@@ -136,7 +214,7 @@ Future<void> _pumpPage(
|
|||||||
ChangeNotifierProvider<AuthController>.value(value: auth),
|
ChangeNotifierProvider<AuthController>.value(value: auth),
|
||||||
],
|
],
|
||||||
child: MaterialApp(
|
child: MaterialApp(
|
||||||
locale: const Locale('en'),
|
locale: locale,
|
||||||
supportedLocales: AppLocalizations.supportedLocales,
|
supportedLocales: AppLocalizations.supportedLocales,
|
||||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||||
home: Theme(data: ThemeData.dark(), child: page),
|
home: Theme(data: ThemeData.dark(), child: page),
|
||||||
|
|||||||
@@ -68,10 +68,12 @@ class FakeAccountRepository implements AccountRepository {
|
|||||||
bool failUpdate = false;
|
bool failUpdate = false;
|
||||||
bool failDeactivate = false;
|
bool failDeactivate = false;
|
||||||
bool failCancel = false;
|
bool failCancel = false;
|
||||||
|
bool throwUnexpected = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<AccountProfile> profile() async {
|
Future<AccountProfile> profile() async {
|
||||||
profileCalls++;
|
profileCalls++;
|
||||||
|
if (throwUnexpected) throw StateError('offline');
|
||||||
if (failProfile) throw const ApiException(500, '资料加载失败');
|
if (failProfile) throw const ApiException(500, '资料加载失败');
|
||||||
return _profile;
|
return _profile;
|
||||||
}
|
}
|
||||||
@@ -79,6 +81,7 @@ class FakeAccountRepository implements AccountRepository {
|
|||||||
@override
|
@override
|
||||||
Future<AccountProfile> updateProfile(AccountProfileUpdate update) async {
|
Future<AccountProfile> updateProfile(AccountProfileUpdate update) async {
|
||||||
lastUpdate = update;
|
lastUpdate = update;
|
||||||
|
if (throwUnexpected) throw StateError('offline');
|
||||||
if (failUpdate) throw const ApiException(400, '昵称不可用');
|
if (failUpdate) throw const ApiException(400, '昵称不可用');
|
||||||
_profile = AccountProfile(
|
_profile = AccountProfile(
|
||||||
uid: _profile.uid,
|
uid: _profile.uid,
|
||||||
@@ -100,6 +103,7 @@ class FakeAccountRepository implements AccountRepository {
|
|||||||
@override
|
@override
|
||||||
Future<List<AccountDevice>> devices() async {
|
Future<List<AccountDevice>> devices() async {
|
||||||
deviceCalls++;
|
deviceCalls++;
|
||||||
|
if (throwUnexpected) throw StateError('offline');
|
||||||
if (failDevices) throw const ApiException(500, '设备加载失败');
|
if (failDevices) throw const ApiException(500, '设备加载失败');
|
||||||
return [..._devices];
|
return [..._devices];
|
||||||
}
|
}
|
||||||
@@ -107,6 +111,7 @@ class FakeAccountRepository implements AccountRepository {
|
|||||||
@override
|
@override
|
||||||
Future<AccountExport> export() async {
|
Future<AccountExport> export() async {
|
||||||
exportCalls++;
|
exportCalls++;
|
||||||
|
if (throwUnexpected) throw StateError('offline');
|
||||||
if (failExport) throw const ApiException(500, '导出失败');
|
if (failExport) throw const ApiException(500, '导出失败');
|
||||||
return _export;
|
return _export;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user