271 lines
8.1 KiB
Dart
271 lines
8.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:prediqo_mobile/core/config/app_config.dart';
|
|
import 'package:prediqo_mobile/core/di/injection.dart';
|
|
import 'package:prediqo_mobile/core/theme/app_theme.dart';
|
|
import 'package:prediqo_mobile/data/repositories/auth_repository_impl.dart';
|
|
import 'package:prediqo_mobile/domain/repositories/repositories.dart';
|
|
import 'package:prediqo_mobile/features/wallet/presentation/profile_edit_screen.dart';
|
|
import 'package:prediqo_mobile/l10n/locale_controller.dart';
|
|
import 'package:prediqo_mobile/shared/widgets/pred_skeleton.dart';
|
|
import 'package:prediqo_mobile/shared/widgets/section_error.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'support/e2e_fixtures.dart';
|
|
|
|
class _ControllableAuth implements AuthRepository {
|
|
Completer<UserProfile>? profileHold;
|
|
Completer<List<CountryOption>>? countriesHold;
|
|
UserProfile? cachedProfile;
|
|
List<CountryOption>? cachedCountries;
|
|
var getProfileCalls = 0;
|
|
var getCountriesCalls = 0;
|
|
Object? profileFail;
|
|
Object? countriesFail;
|
|
|
|
_ControllableAuth({this.cachedProfile, this.cachedCountries});
|
|
|
|
@override
|
|
UserProfile? peekLastProfile() => cachedProfile;
|
|
|
|
@override
|
|
void invalidateProfile() => cachedProfile = null;
|
|
|
|
@override
|
|
Future<UserProfile> getProfile({bool force = false}) async {
|
|
getProfileCalls++;
|
|
if (profileFail != null) throw profileFail!;
|
|
if (profileHold != null) return profileHold!.future;
|
|
return cachedProfile ??
|
|
const UserProfile(
|
|
firstName: 'Ada',
|
|
lastName: 'Lovelace',
|
|
email: 'ada@prediqo.test',
|
|
countryOfResidence: 'FR',
|
|
gender: 'female',
|
|
);
|
|
}
|
|
|
|
@override
|
|
List<CountryOption>? peekLastCountries() => cachedCountries;
|
|
|
|
@override
|
|
void invalidateCountries() => cachedCountries = null;
|
|
|
|
@override
|
|
Future<List<CountryOption>> getCountries({bool force = false}) async {
|
|
getCountriesCalls++;
|
|
if (countriesFail != null) throw countriesFail!;
|
|
if (countriesHold != null) return countriesHold!.future;
|
|
return cachedCountries ??
|
|
const [
|
|
CountryOption(code: 'FR', name: 'France'),
|
|
CountryOption(code: 'US', name: 'United States'),
|
|
];
|
|
}
|
|
|
|
@override
|
|
Future<LoginResult> login(
|
|
String email,
|
|
String password, {
|
|
String? totpCode,
|
|
String? captchaToken,
|
|
}) async =>
|
|
throw UnimplementedError();
|
|
|
|
@override
|
|
Future<RegisterResult> register(RegisterRequest request) async =>
|
|
throw UnimplementedError();
|
|
|
|
@override
|
|
Future<({bool valid, String? reason})> validateReferralCode(String code) async =>
|
|
(valid: true, reason: null);
|
|
|
|
@override
|
|
Future<void> resendVerificationEmail(String email) async {}
|
|
|
|
@override
|
|
Future<void> logout() async {}
|
|
|
|
@override
|
|
Future<bool> isLoggedIn() async => true;
|
|
|
|
@override
|
|
Future<({String? userId, String? role})> getStoredUser() async =>
|
|
(userId: 'u1', role: 'user');
|
|
|
|
@override
|
|
Future<Map<String, dynamic>> setup2FA({String? password, String? totpCode}) async =>
|
|
{};
|
|
|
|
@override
|
|
Future<List<String>> enable2FA(String code) async => const [];
|
|
|
|
@override
|
|
Future<void> disable2FA({required String password, required String code}) async {}
|
|
|
|
@override
|
|
Future<List<String>> regenerateRecoveryCodes(String totpCode) async => const [];
|
|
|
|
@override
|
|
Future<void> updateProfile({
|
|
String? firstName,
|
|
String? middleName,
|
|
String? lastName,
|
|
String? dateOfBirth,
|
|
required String email,
|
|
String? phone,
|
|
required String countryOfResidence,
|
|
String? nationality,
|
|
required String gender,
|
|
}) async {}
|
|
|
|
@override
|
|
Future<void> changePassword({
|
|
required String currentPassword,
|
|
required String newPassword,
|
|
}) async {}
|
|
|
|
@override
|
|
Future<void> forgotPassword(String email) async {}
|
|
}
|
|
|
|
Future<void> _boot(AuthRepository auth) async {
|
|
configureE2eTestEnvironment();
|
|
if (sl.isRegistered<AppConfig>()) await sl.reset();
|
|
sl.registerSingleton<AppConfig>(
|
|
const AppConfig(apiBaseUrl: 'http://localhost', isProduction: false, wsEnabled: false),
|
|
);
|
|
final locale = LocaleController();
|
|
SharedPreferences.setMockInitialValues({'app_locale': 'en'});
|
|
await locale.load();
|
|
sl.registerSingleton<LocaleController>(locale);
|
|
sl.registerLazySingleton<AuthRepository>(() => auth);
|
|
}
|
|
|
|
void _tallSurface(WidgetTester tester) {
|
|
tester.view.physicalSize = const Size(400, 2400);
|
|
tester.view.devicePixelRatio = 1.0;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
}
|
|
|
|
Widget _wrap(AuthRepository auth) {
|
|
return RepositoryProvider<AuthRepository>.value(
|
|
value: auth,
|
|
child: MaterialApp(
|
|
theme: AppTheme.dark,
|
|
home: const ProfileEditScreen(),
|
|
),
|
|
);
|
|
}
|
|
|
|
void main() {
|
|
tearDown(() async {
|
|
if (sl.isRegistered<AppConfig>()) await sl.reset();
|
|
});
|
|
|
|
testWidgets('Profile paints fields from cache while countries still loading', (tester) async {
|
|
_tallSurface(tester);
|
|
final countriesHold = Completer<List<CountryOption>>();
|
|
final auth = _ControllableAuth(
|
|
cachedProfile: const UserProfile(
|
|
firstName: 'Ada',
|
|
lastName: 'Lovelace',
|
|
email: 'ada@prediqo.test',
|
|
countryOfResidence: 'FR',
|
|
gender: 'female',
|
|
),
|
|
)..countriesHold = countriesHold;
|
|
await _boot(auth);
|
|
|
|
await tester.pumpWidget(_wrap(auth));
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 50));
|
|
|
|
expect(find.text('Ada'), findsOneWidget);
|
|
expect(find.text('ada@prediqo.test'), findsOneWidget);
|
|
expect(find.byType(PredSkeletonCard), findsWidgets);
|
|
|
|
countriesHold.complete(const [
|
|
CountryOption(code: 'FR', name: 'France'),
|
|
]);
|
|
await tester.pumpAndSettle();
|
|
expect(find.textContaining('France'), findsWidgets);
|
|
});
|
|
|
|
testWidgets('Profile shows countries SectionError without blanking form', (tester) async {
|
|
_tallSurface(tester);
|
|
final auth = _ControllableAuth(
|
|
cachedProfile: const UserProfile(
|
|
firstName: 'Ada',
|
|
lastName: 'Lovelace',
|
|
email: 'ada@prediqo.test',
|
|
countryOfResidence: 'FR',
|
|
gender: 'female',
|
|
),
|
|
)..countriesFail = Exception('countries down');
|
|
await _boot(auth);
|
|
|
|
await tester.pumpWidget(_wrap(auth));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.byType(SectionError), findsOneWidget);
|
|
expect(find.text('Ada'), findsOneWidget);
|
|
expect(find.text('Save profile'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('Profile shows SectionError when profile fails with empty cache', (tester) async {
|
|
_tallSurface(tester);
|
|
final auth = _ControllableAuth()..profileFail = Exception('profile down');
|
|
// Countries succeed so only profile error shows.
|
|
await _boot(auth);
|
|
|
|
await tester.pumpWidget(_wrap(auth));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.byType(SectionError), findsWidgets);
|
|
expect(find.text('Save profile'), findsNothing);
|
|
});
|
|
|
|
testWidgets('Profile retry does not launch parallel duplicate profile fetches', (tester) async {
|
|
_tallSurface(tester);
|
|
final auth = _ControllableAuth()
|
|
..profileFail = Exception('profile down')
|
|
..countriesFail = Exception('countries down');
|
|
await _boot(auth);
|
|
|
|
await tester.pumpWidget(_wrap(auth));
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 50));
|
|
expect(find.byType(SectionError), findsWidgets);
|
|
final before = auth.getProfileCalls;
|
|
|
|
auth.profileFail = null;
|
|
auth.profileHold = Completer<UserProfile>();
|
|
final retry = find.byKey(const Key('section_error_retry'));
|
|
expect(retry, findsWidgets);
|
|
await tester.ensureVisible(retry.first);
|
|
await tester.tap(retry.first);
|
|
await tester.pump();
|
|
if (retry.evaluate().isNotEmpty) {
|
|
await tester.tap(retry.first);
|
|
await tester.pump();
|
|
}
|
|
expect(auth.getProfileCalls, before + 1);
|
|
|
|
auth.profileHold!.complete(const UserProfile(
|
|
firstName: 'Ada',
|
|
lastName: 'Lovelace',
|
|
email: 'ada@prediqo.test',
|
|
countryOfResidence: 'FR',
|
|
gender: 'female',
|
|
));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Ada'), findsOneWidget);
|
|
});
|
|
}
|